From 093d8f5cb63637fadc4ac01e7994d18e8c526475 Mon Sep 17 00:00:00 2001 From: g-cont Date: Fri, 26 Dec 2008 00:00:00 +0300 Subject: [PATCH] 26 Dec 2008 --- backup.lst | 10 +- client/client.def | 5 + client/client.dsp | 260 + sv_dll/server.plg => client/client.plg | 4 +- client/global/dll_int.cpp | 91 + client/global/enginecallback.h | 92 + client/global/extdll.h | 41 + client/global/tempents.cpp | 103 + client/global/triapi.cpp | 15 + client/global/vector.h | 243 + client/global/view.cpp | 79 + client/hud/hud.cpp | 341 ++ client/hud/hud.h | 680 +++ client/hud/hud_ammo.cpp | 1189 +++++ client/hud/hud_ammo.h | 59 + client/hud/hud_ammohistory.cpp | 186 + client/hud/hud_ammohistory.h | 134 + client/hud/hud_battery.cpp | 135 + client/hud/hud_death.cpp | 232 + client/hud/hud_flashlight.cpp | 140 + client/hud/hud_geiger.cpp | 177 + client/hud/hud_health.cpp | 448 ++ client/hud/hud_health.h | 106 + client/hud/hud_icons.cpp | 150 + client/hud/hud_iface.h | 154 + client/hud/hud_menu.cpp | 172 + client/hud/hud_message.cpp | 514 ++ client/hud/hud_motd.cpp | 132 + client/hud/hud_msg.cpp | 426 ++ client/hud/hud_saytext.cpp | 226 + client/hud/hud_scoreboard.cpp | 505 ++ client/hud/hud_sound.cpp | 357 ++ client/hud/hud_statusbar.cpp | 246 + client/hud/hud_text.cpp | 204 + client/hud/hud_train.cpp | 79 + client/hud/hud_utils.cpp | 356 ++ client/hud/hud_warhead.cpp | 141 + client/hud/hud_zoom.cpp | 80 + client/r_main.h | 348 ++ client/r_particle.cpp | 960 ++++ client/r_particle.h | 208 + client/r_util.cpp | 818 ++++ client/r_view.cpp | 1117 +++++ client/r_view.h | 45 + client/r_weather.cpp | 680 +++ client/r_weather.h | 66 + client/readme.txt | 107 + common/common.dsp | 2 +- debug.bat | 8 +- engine/client/cl_demo.c | 20 +- engine/client/cl_edict.h | 79 - engine/client/cl_effects.c | 6 +- engine/client/cl_frame.c | 86 +- engine/client/cl_game.c | 924 ++++ engine/client/cl_main.c | 16 +- engine/client/cl_parse.c | 26 +- engine/client/{cl_pred.c => cl_phys.c} | 94 +- engine/client/cl_progs.c | 590 --- engine/client/cl_scrn.c | 7 +- engine/client/cl_view.c | 1 - engine/client/client.h | 102 +- engine/common.h | 14 +- engine/common/con_main.c | 2 +- engine/common/engfuncs.c | 81 +- engine/common/menu.c | 6 - engine/engine.dsp | 10 +- engine/host.c | 2 +- engine/server/server.h | 18 +- engine/server/sv_client.c | 2 +- engine/server/sv_cmds.c | 8 +- engine/server/sv_frame.c | 2 +- engine/server/sv_game.c | 156 +- engine/server/sv_init.c | 2 +- engine/server/sv_main.c | 6 +- engine/server/sv_phys.c | 2 +- launch/cvar.c | 10 +- public/clgame_api.h | 235 + public/const.h | 67 + public/{svprog_def.h => entity_def.h} | 56 +- public/svgame_api.h | 68 +- public/vsound_api.h | 2 +- release.bat | 7 +- {sv_dll => server}/cbase.h | 0 server/ents/basebeams.h | 106 + server/ents/basebrush.cpp | 846 ++++ server/ents/basebrush.h | 207 + server/ents/baseentity.cpp | 959 ++++ server/ents/baseentity.h | 324 ++ server/ents/basefunc.cpp | 2361 +++++++++ server/ents/basefx.cpp | 2900 +++++++++++ server/ents/baseinfo.cpp | 229 + server/ents/baseinfo.h | 44 + server/ents/baseitem.cpp | 628 +++ server/ents/baseitem.h | 68 + server/ents/baselogic.cpp | 533 ++ server/ents/baselogic.h | 145 + server/ents/basemover.cpp | 1834 +++++++ server/ents/basemover.cpp.save | 1352 +++++ server/ents/basemover.h | 252 + server/ents/baseother.cpp | 270 + server/ents/basepath.cpp | 427 ++ server/ents/baserockets.cpp | 961 ++++ server/ents/baserockets.h | 110 + server/ents/basesprite.h | 141 + server/ents/basetank.cpp | 1154 +++++ server/ents/basetank.h | 143 + server/ents/basetrigger.cpp | 1375 ++++++ server/ents/baseutil.cpp | 405 ++ server/ents/baseweapon.cpp | 1993 ++++++++ server/ents/baseweapon.h | 362 ++ server/ents/baseworld.cpp | 59 + server/ents/baseworld.h | 20 + server/game/game.cpp | 44 + server/game/game.h | 26 + server/game/gamerules.cpp | 164 + server/game/gamerules.h | 307 ++ server/game/lights.cpp | 280 ++ server/game/multiplay_gamerules.cpp | 1345 +++++ server/game/singleplay_gamerules.cpp | 264 + server/game/sound.cpp | 1948 ++++++++ server/game/spectator.h | 27 + server/game/teamplay_gamerules.cpp | 614 +++ server/game/teamplay_gamerules.h | 56 + server/global/bullets.h | 22 + server/global/client.cpp | 1204 +++++ server/global/client.h | 120 + server/global/decals.cpp | 51 + server/global/decals.h | 63 + server/global/defaults.h | 447 ++ server/global/dll_int.cpp | 434 ++ server/global/enginecallback.h | 134 + server/global/extdll.h | 81 + server/global/globals.cpp | 81 + server/global/globals.h | 95 + server/global/hierarchy.h | 29 + server/global/parent.cpp | 324 ++ server/global/plane.h | 176 + server/global/saverestore.cpp | 1235 +++++ server/global/saverestore.h | 165 + server/global/sfx.cpp | 149 + server/global/sfx.h | 56 + server/global/shake.h | 41 + server/global/soundent.h | 99 + server/global/te_temp.h | 448 ++ server/global/utils.cpp | 3048 ++++++++++++ server/global/utils.h | 765 +++ server/global/vector.h | 281 ++ {sv_dll => server}/legacy.cpp | 0 server/monsters/activity.h | 132 + server/monsters/activitymap.h | 121 + server/monsters/ai_sound.cpp | 379 ++ server/monsters/animating.cpp | 342 ++ server/monsters/animation.cpp | 562 +++ server/monsters/animation.h | 52 + server/monsters/apache.cpp | 941 ++++ server/monsters/barnacle.cpp | 434 ++ server/monsters/barney.cpp | 948 ++++ server/monsters/baseanimating.h | 50 + server/monsters/basemonster.cpp | 6233 ++++++++++++++++++++++++ server/monsters/basemonster.h | 352 ++ server/monsters/combat.cpp | 260 + server/monsters/damage.h | 21 + server/monsters/defaultai.cpp | 1260 +++++ server/monsters/defaultai.h | 98 + server/monsters/flyingmonster.cpp | 281 ++ server/monsters/flyingmonster.h | 53 + server/monsters/generic.cpp | 395 ++ server/monsters/gman.cpp | 243 + server/monsters/hassassin.cpp | 1068 ++++ server/monsters/headcrab.cpp | 569 +++ server/monsters/hgrunt.cpp | 2617 ++++++++++ server/monsters/leech.cpp | 743 +++ server/monsters/monsterevent.h | 34 + server/monsters/monsters.h | 163 + server/monsters/nodes.cpp | 3660 ++++++++++++++ server/monsters/nodes.h | 374 ++ server/monsters/osprey.cpp | 822 ++++ server/monsters/player.cpp | 5176 ++++++++++++++++++++ server/monsters/player.h | 382 ++ server/monsters/rat.cpp | 104 + server/monsters/roach.cpp | 466 ++ server/monsters/schedule.h | 292 ++ server/monsters/scientist.cpp | 1458 ++++++ server/monsters/scripted.cpp | 1243 +++++ server/monsters/scripted.h | 128 + server/monsters/scriptevent.h | 29 + server/monsters/squad.h | 20 + server/monsters/squadmonster.cpp | 642 +++ server/monsters/squadmonster.h | 121 + server/monsters/talkmonster.cpp | 1557 ++++++ server/monsters/talkmonster.h | 186 + server/monsters/turret.cpp | 1335 +++++ server/monsters/zombie.cpp | 381 ++ {sv_dll => server}/server.def | 0 {sv_dll => server}/server.dsp | 2 +- {sv_dll => server}/weapon_generic.cpp | 0 todo.log | 2 +- vsound/s_main.c | 20 +- vsound/sound.h | 2 +- xash.dsw | 14 +- 200 files changed, 86086 insertions(+), 1090 deletions(-) create mode 100644 client/client.def create mode 100644 client/client.dsp rename sv_dll/server.plg => client/client.plg (52%) create mode 100644 client/global/dll_int.cpp create mode 100644 client/global/enginecallback.h create mode 100644 client/global/extdll.h create mode 100644 client/global/tempents.cpp create mode 100644 client/global/triapi.cpp create mode 100644 client/global/vector.h create mode 100644 client/global/view.cpp create mode 100644 client/hud/hud.cpp create mode 100644 client/hud/hud.h create mode 100644 client/hud/hud_ammo.cpp create mode 100644 client/hud/hud_ammo.h create mode 100644 client/hud/hud_ammohistory.cpp create mode 100644 client/hud/hud_ammohistory.h create mode 100644 client/hud/hud_battery.cpp create mode 100644 client/hud/hud_death.cpp create mode 100644 client/hud/hud_flashlight.cpp create mode 100644 client/hud/hud_geiger.cpp create mode 100644 client/hud/hud_health.cpp create mode 100644 client/hud/hud_health.h create mode 100644 client/hud/hud_icons.cpp create mode 100644 client/hud/hud_iface.h create mode 100644 client/hud/hud_menu.cpp create mode 100644 client/hud/hud_message.cpp create mode 100644 client/hud/hud_motd.cpp create mode 100644 client/hud/hud_msg.cpp create mode 100644 client/hud/hud_saytext.cpp create mode 100644 client/hud/hud_scoreboard.cpp create mode 100644 client/hud/hud_sound.cpp create mode 100644 client/hud/hud_statusbar.cpp create mode 100644 client/hud/hud_text.cpp create mode 100644 client/hud/hud_train.cpp create mode 100644 client/hud/hud_utils.cpp create mode 100644 client/hud/hud_warhead.cpp create mode 100644 client/hud/hud_zoom.cpp create mode 100644 client/r_main.h create mode 100644 client/r_particle.cpp create mode 100644 client/r_particle.h create mode 100644 client/r_util.cpp create mode 100644 client/r_view.cpp create mode 100644 client/r_view.h create mode 100644 client/r_weather.cpp create mode 100644 client/r_weather.h create mode 100644 client/readme.txt delete mode 100644 engine/client/cl_edict.h create mode 100644 engine/client/cl_game.c rename engine/client/{cl_pred.c => cl_phys.c} (70%) create mode 100644 public/clgame_api.h rename public/{svprog_def.h => entity_def.h} (75%) rename {sv_dll => server}/cbase.h (100%) create mode 100644 server/ents/basebeams.h create mode 100644 server/ents/basebrush.cpp create mode 100644 server/ents/basebrush.h create mode 100644 server/ents/baseentity.cpp create mode 100644 server/ents/baseentity.h create mode 100644 server/ents/basefunc.cpp create mode 100644 server/ents/basefx.cpp create mode 100644 server/ents/baseinfo.cpp create mode 100644 server/ents/baseinfo.h create mode 100644 server/ents/baseitem.cpp create mode 100644 server/ents/baseitem.h create mode 100644 server/ents/baselogic.cpp create mode 100644 server/ents/baselogic.h create mode 100644 server/ents/basemover.cpp create mode 100644 server/ents/basemover.cpp.save create mode 100644 server/ents/basemover.h create mode 100644 server/ents/baseother.cpp create mode 100644 server/ents/basepath.cpp create mode 100644 server/ents/baserockets.cpp create mode 100644 server/ents/baserockets.h create mode 100644 server/ents/basesprite.h create mode 100644 server/ents/basetank.cpp create mode 100644 server/ents/basetank.h create mode 100644 server/ents/basetrigger.cpp create mode 100644 server/ents/baseutil.cpp create mode 100644 server/ents/baseweapon.cpp create mode 100644 server/ents/baseweapon.h create mode 100644 server/ents/baseworld.cpp create mode 100644 server/ents/baseworld.h create mode 100644 server/game/game.cpp create mode 100644 server/game/game.h create mode 100644 server/game/gamerules.cpp create mode 100644 server/game/gamerules.h create mode 100644 server/game/lights.cpp create mode 100644 server/game/multiplay_gamerules.cpp create mode 100644 server/game/singleplay_gamerules.cpp create mode 100644 server/game/sound.cpp create mode 100644 server/game/spectator.h create mode 100644 server/game/teamplay_gamerules.cpp create mode 100644 server/game/teamplay_gamerules.h create mode 100644 server/global/bullets.h create mode 100644 server/global/client.cpp create mode 100644 server/global/client.h create mode 100644 server/global/decals.cpp create mode 100644 server/global/decals.h create mode 100644 server/global/defaults.h create mode 100644 server/global/dll_int.cpp create mode 100644 server/global/enginecallback.h create mode 100644 server/global/extdll.h create mode 100644 server/global/globals.cpp create mode 100644 server/global/globals.h create mode 100644 server/global/hierarchy.h create mode 100644 server/global/parent.cpp create mode 100644 server/global/plane.h create mode 100644 server/global/saverestore.cpp create mode 100644 server/global/saverestore.h create mode 100644 server/global/sfx.cpp create mode 100644 server/global/sfx.h create mode 100644 server/global/shake.h create mode 100644 server/global/soundent.h create mode 100644 server/global/te_temp.h create mode 100644 server/global/utils.cpp create mode 100644 server/global/utils.h create mode 100644 server/global/vector.h rename {sv_dll => server}/legacy.cpp (100%) create mode 100644 server/monsters/activity.h create mode 100644 server/monsters/activitymap.h create mode 100644 server/monsters/ai_sound.cpp create mode 100644 server/monsters/animating.cpp create mode 100644 server/monsters/animation.cpp create mode 100644 server/monsters/animation.h create mode 100644 server/monsters/apache.cpp create mode 100644 server/monsters/barnacle.cpp create mode 100644 server/monsters/barney.cpp create mode 100644 server/monsters/baseanimating.h create mode 100644 server/monsters/basemonster.cpp create mode 100644 server/monsters/basemonster.h create mode 100644 server/monsters/combat.cpp create mode 100644 server/monsters/damage.h create mode 100644 server/monsters/defaultai.cpp create mode 100644 server/monsters/defaultai.h create mode 100644 server/monsters/flyingmonster.cpp create mode 100644 server/monsters/flyingmonster.h create mode 100644 server/monsters/generic.cpp create mode 100644 server/monsters/gman.cpp create mode 100644 server/monsters/hassassin.cpp create mode 100644 server/monsters/headcrab.cpp create mode 100644 server/monsters/hgrunt.cpp create mode 100644 server/monsters/leech.cpp create mode 100644 server/monsters/monsterevent.h create mode 100644 server/monsters/monsters.h create mode 100644 server/monsters/nodes.cpp create mode 100644 server/monsters/nodes.h create mode 100644 server/monsters/osprey.cpp create mode 100644 server/monsters/player.cpp create mode 100644 server/monsters/player.h create mode 100644 server/monsters/rat.cpp create mode 100644 server/monsters/roach.cpp create mode 100644 server/monsters/schedule.h create mode 100644 server/monsters/scientist.cpp create mode 100644 server/monsters/scripted.cpp create mode 100644 server/monsters/scripted.h create mode 100644 server/monsters/scriptevent.h create mode 100644 server/monsters/squad.h create mode 100644 server/monsters/squadmonster.cpp create mode 100644 server/monsters/squadmonster.h create mode 100644 server/monsters/talkmonster.cpp create mode 100644 server/monsters/talkmonster.h create mode 100644 server/monsters/turret.cpp create mode 100644 server/monsters/zombie.cpp rename {sv_dll => server}/server.def (100%) rename {sv_dll => server}/server.dsp (95%) rename {sv_dll => server}/weapon_generic.cpp (100%) diff --git a/backup.lst b/backup.lst index ac2dd11e..07c9d72b 100644 --- a/backup.lst +++ b/backup.lst @@ -13,10 +13,16 @@ launchers.bat todo.log baserc\ -cl_dll\ +client\ +client\hud\ +client\global\ public\ physic\ -sv_dll\ +server\ +server\ents\ +server\game\ +server\global\ +server\monsters\ render\ vprogs\ vsound\ diff --git a/client/client.def b/client/client.def new file mode 100644 index 00000000..ec19fdc4 --- /dev/null +++ b/client/client.def @@ -0,0 +1,5 @@ +LIBRARY client +EXPORTS + CreateAPI @1 +SECTIONS + .data READ WRITE diff --git a/client/client.dsp b/client/client.dsp new file mode 100644 index 00000000..93fa4a24 --- /dev/null +++ b/client/client.dsp @@ -0,0 +1,260 @@ +# Microsoft Developer Studio Project File - Name="client" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 + +CFG=client - Win32 Release +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "client.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "client.mak" CFG="client - Win32 Release" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "client - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE "client - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName ""$/GoldSrc/client", HGEBAAAA" +# PROP Scc_LocalPath "." +CPP=cl.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "client - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir ".\!Release" +# PROP BASE Intermediate_Dir ".\!Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "..\temp\client\!release" +# PROP Intermediate_Dir "..\temp\client\!release" +# PROP Ignore_Export_Lib 1 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /YX /c +# ADD CPP /nologo /MD /W3 /GX /O2 /I "./" /I "../public" /I "./global" /I "./hud" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /FD /c +# SUBTRACT CPP /Fr /YX +# ADD BASE MTL /nologo /D "NDEBUG" /win32 +# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /dll /machine:I386 +# ADD LINK32 msvcrt.lib /nologo /subsystem:windows /dll /machine:I386 /nodefaultlib:"libc" /def:".\client.def" /libpath:"..\common\libs" +# SUBTRACT LINK32 /map +# Begin Custom Build +TargetDir=\Xash3D\src_main\temp\client\!release +InputPath=\Xash3D\src_main\temp\client\!release\client.dll +SOURCE="$(InputPath)" + +"D:\Xash3D\tmpQuArK\bin\client.dll" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + copy $(TargetDir)\client.dll "D:\Xash3D\tmpQuArK\bin\client.dll" + +# End Custom Build + +!ELSEIF "$(CFG)" == "client - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "client___Win32_Debug" +# PROP BASE Intermediate_Dir "client___Win32_Debug" +# PROP BASE Ignore_Export_Lib 0 +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "..\temp\client\!debug" +# PROP Intermediate_Dir "..\temp\client\!debug" +# PROP Ignore_Export_Lib 1 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /MT /W3 /GX /Zi /O2 /I "..\common\vgui" /I "..\client" /I "..\client\render" /I ".\hud" /I "..\common\engine" /I "..\common" /I "..\server" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "CLIENT_DLL" /YX /FD /c +# SUBTRACT BASE CPP /Fr +# ADD CPP /nologo /MDd /W3 /Gm /Gi /GX /ZI /Od /I "./" /I "../public" /I "./global" /I "./hud" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /FR /FD /c +# SUBTRACT CPP /YX +# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 +# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib opengl32.lib glu32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib winmm.lib vgui.lib wsock32.lib cvaLib.lib /nologo /subsystem:windows /dll /machine:I386 /nodefaultlib:"libc" /def:".\client.def" /libpath:"..\common\libs" +# SUBTRACT BASE LINK32 /map +# ADD LINK32 msvcrtd.lib /nologo /subsystem:windows /dll /incremental:yes /debug /machine:I386 /nodefaultlib:"libc.lib" /def:".\client.def" /pdbtype:sept /libpath:"..\common\libs" +# SUBTRACT LINK32 /map +# Begin Custom Build +TargetDir=\Xash3D\src_main\temp\client\!debug +InputPath=\Xash3D\src_main\temp\client\!debug\client.dll +SOURCE="$(InputPath)" + +"D:\Xash3D\tmpQuArK\bin\client.dll" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + copy $(TargetDir)\client.dll "D:\Xash3D\tmpQuArK\bin\client.dll" + +# End Custom Build + +!ENDIF + +# Begin Target + +# Name "client - Win32 Release" +# Name "client - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat;for;f90" +# Begin Source File + +SOURCE=.\global\dll_int.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_ammo.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_ammohistory.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_battery.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_death.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_flashlight.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_geiger.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_health.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_icons.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_menu.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_message.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_motd.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_msg.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_saytext.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_scoreboard.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_sound.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_statusbar.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_text.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_train.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_utils.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_warhead.cpp +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_zoom.cpp +# End Source File +# Begin Source File + +SOURCE=.\global\tempents.cpp +# End Source File +# Begin Source File + +SOURCE=.\global\triapi.cpp +# End Source File +# Begin Source File + +SOURCE=.\global\view.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\global\enginecallback.h +# End Source File +# Begin Source File + +SOURCE=.\global\extdll.h +# End Source File +# Begin Source File + +SOURCE=.\hud\hud.h +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_ammo.h +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_ammohistory.h +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_health.h +# End Source File +# Begin Source File + +SOURCE=.\hud\hud_iface.h +# End Source File +# Begin Source File + +SOURCE=.\global\vector.h +# End Source File +# End Group +# End Target +# End Project diff --git a/sv_dll/server.plg b/client/client.plg similarity index 52% rename from sv_dll/server.plg rename to client/client.plg index 35b71426..33666b63 100644 --- a/sv_dll/server.plg +++ b/client/client.plg @@ -3,14 +3,14 @@
 

Build Log

---------------------Configuration: server - Win32 Debug-------------------- +--------------------Configuration: client - Win32 Debug--------------------

Command Lines

Results

-server.dll - 0 error(s), 0 warning(s) +client.dll - 0 error(s), 0 warning(s)
diff --git a/client/global/dll_int.cpp b/client/global/dll_int.cpp new file mode 100644 index 00000000..217cd332 --- /dev/null +++ b/client/global/dll_int.cpp @@ -0,0 +1,91 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// dll_int.cpp - dll entry points +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +cl_enginefuncs_t g_engfuncs; +CHud gHUD; + +// main DLL entry point +BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ) +{ + return TRUE; +} + +static HUD_FUNCTIONS gFunctionTable = +{ + sizeof( HUD_FUNCTIONS ), + HUD_VidInit, + HUD_Init, + HUD_Redraw, + HUD_UpdateClientData, + HUD_Reset, + HUD_Frame, + HUD_Shutdown, + HUD_DrawNormalTriangles, + HUD_DrawTransparentTriangles, + HUD_CreateEntities, + HUD_StudioEvent, + V_CalcRefdef, +}; + +//======================================================================= +// GetApi +//======================================================================= +int CreateAPI( HUD_FUNCTIONS *pFunctionTable, cl_enginefuncs_t* pEngfuncsFromEngine, int interfaceVersion ) +{ + if( !pFunctionTable || !pEngfuncsFromEngine || interfaceVersion != INTERFACE_VERSION ) + { + return FALSE; + } + + // copy HUD_FUNCTIONS table to engine, copy engfuncs table from engine + memcpy( pFunctionTable, &gFunctionTable, sizeof( HUD_FUNCTIONS )); + memcpy( &g_engfuncs, pEngfuncsFromEngine, sizeof( cl_enginefuncs_t )); + + return TRUE; +} + +int HUD_VidInit( void ) +{ + gHUD.VidInit(); + + return 1; +} + +void HUD_Init( void ) +{ + gHUD.Init(); +} + +int HUD_Redraw( float flTime, int intermission ) +{ + gHUD.Redraw( flTime, intermission ); + DrawCrosshair(); + + return 1; +} + +int HUD_UpdateClientData( ref_params_t *parms, float flTime ) +{ + return gHUD.UpdateClientData( parms, flTime ); +} + +void HUD_Reset( void ) +{ + gHUD.VidInit(); +} + +void HUD_Frame( double time ) +{ + // place to call vgui_frame +} + +void HUD_Shutdown( void ) +{ + // no shutdown operations +} \ No newline at end of file diff --git a/client/global/enginecallback.h b/client/global/enginecallback.h new file mode 100644 index 00000000..460609f6 --- /dev/null +++ b/client/global/enginecallback.h @@ -0,0 +1,92 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// enginecallback.h - actual engine callbacks +//======================================================================= + +#ifndef ENGINECALLBACKS_H +#define ENGINECALLBACKS_H + +// built-in memory manager +#define MALLOC( x ) (*g_engfuncs.pfnMemAlloc)( x, __FILE__, __LINE__ ) +#define CALLOC( x, y ) (*g_engfuncs.pfnMemAlloc)((x) * (y), __FILE__, __LINE__ ) +#define FREE( x ) (*g_engfuncs.pfnMemFree)( x, __FILE__, __LINE__ ) + +// screen handlers +#define LOAD_SHADER (*g_engfuncs.pfnLoadShader) +#define DrawImage (*g_engfuncs.pfnDrawImage) +#define SetColor (*g_engfuncs.pfnSetColor) +#define CVAR_REGISTER (*g_engfuncs.pfnRegisterVariable) +#define CVAR_SET_FLOAT (*g_engfuncs.pfnCvarSetValue) +#define CVAR_GET_FLOAT (*g_engfuncs.pfnGetCvarFloat) +#define CVAR_GET_STRING (*g_engfuncs.pfnGetCvarString) + +#define SERVER_COMMAND (*g_engfuncs.pfnServerCmd) +#define CLIENT_COMMAND (*g_engfuncs.pfnClientCmd) +#define GET_PLAYER_INFO (*g_engfuncs.pfnGetPlayerInfo) +#define GET_GAME_MESSAGE (*g_engfuncs.pfnTextMessageGet) +#define CMD_ARGC (*g_engfuncs.pfnCmdArgc) +#define CMD_ARGV (*g_engfuncs.pfnCmdArgv) +#define ALERT (*g_engfuncs.pfnAlertMessage) + +inline void CL_PlaySound( const char *szSound, float flVolume ) +{ + g_engfuncs.pfnPlaySoundByName( szSound, flVolume, NULL ); +} + +inline void CL_PlaySound( int iSound, float flVolume ) +{ + g_engfuncs.pfnPlaySoundByIndex( iSound, flVolume, NULL ); +} + +inline void CL_PlaySound( const char *szSound, float flVolume, Vector &pos ) +{ + g_engfuncs.pfnPlaySoundByName( szSound, flVolume, pos ); +} + +inline void CL_PlaySound( int iSound, float flVolume, Vector &pos ) +{ + g_engfuncs.pfnPlaySoundByIndex( iSound, flVolume, pos ); +} + +#define AngleVectors (*g_engfuncs.pfnAngleVectors) +#define DrawCenterPrint (*g_engfuncs.pfnDrawCenterPrint) +#define CenterPrint (*g_engfuncs.pfnCenterPrint) +#define DrawChar (*g_engfuncs.pfnDrawCharacter) +#define DrawString (*g_engfuncs.pfnDrawString) +#define GetImageSize (*g_engfuncs.pfnGetImageSize) +#define GetViewAngles (*g_engfuncs.pfnGetViewAngles) +#define GetEntityByIndex (*g_engfuncs.pfnGetEntityByIndex) +#define GetLocalPlayer (*g_engfuncs.pfnGetLocalPlayer) +#define IsSpectateOnly (*g_engfuncs.pfnIsSpectateOnly) +#define GetClientTime (*g_engfuncs.pfnGetClientTime) +#define GetMaxClients (*g_engfuncs.pfnGetMaxClients) +#define GetViewModel (*g_engfuncs.pfnGetViewModel) +#define POINT_CONTENTS (*g_engfuncs.pfnPointContents) +#define TRACE_LINE (*g_engfuncs.pfnTraceLine) +#define RANDOM_LONG (*g_engfuncs.pfnRandomLong) +#define RANDOM_FLOAT (*g_engfuncs.pfnRandomFloat) +#define LOAD_FILE (*g_engfuncs.pfnLoadFile) +#define FREE_FILE (*g_engfuncs.pfnFreeFile) +#define GET_GAME_DIR (*g_engfuncs.pfnGetGameDir) +#define HOST_ERROR (*g_engfuncs.pfnHostError) + +// heavy legacy of Valve... +// tune char size by taste +inline void TextMessageDrawChar( int xpos, int ypos, int number, int r, int g, int b ) +{ + SetColor((r / 255.0f), (g / 255.0f), (b / 255.0f), 1.0f ); + DrawChar( xpos, ypos, SMALLCHAR_WIDTH, SMALLCHAR_HEIGHT, number ); +} + +inline void FillRGBA( int x, int y, int width, int height, int r, int g, int b, int a ) +{ + Vector RGB; + + RGB.x = (float)(r / 255.0f); + RGB.y = (float)(g / 255.0f); + RGB.z = (float)(b / 255.0f); + + g_engfuncs.pfnFillRGBA( x, y, width, height, RGB, (float)(a / 255.0f)); +} + +#endif//ENGINECALLBACKS_H \ No newline at end of file diff --git a/client/global/extdll.h b/client/global/extdll.h new file mode 100644 index 00000000..c57b55ae --- /dev/null +++ b/client/global/extdll.h @@ -0,0 +1,41 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// extdll.h - must be included into all client files +//======================================================================= + +#ifndef EXTDLL_H +#define EXTDLL_H + +// shut-up compiler warnings +#pragma warning(disable : 4305) // int or float data truncation +#pragma warning(disable : 4201) // nameless struct/union +#pragma warning(disable : 4514) // unreferenced inline function removed +#pragma warning(disable : 4100) // unreferenced formal parameter + +#include "windows.h" + +#ifndef BIT +#define BIT( n ) (1<<( n )) +#endif + +#define DLLEXPORT _declspec( dllexport ) + +// Misc C-runtime library headers +#include +#include +#include +#include +#include + +// shared engine/DLL constants +#include "const.h" + +// Vector class +#include "vector.h" + +// Shared header describing protocol between engine and DLLs +#include "entity_def.h" +#include "qfiles_ref.h" +#include "clgame_api.h" + +#endif//EXTDLL_H \ No newline at end of file diff --git a/client/global/tempents.cpp b/client/global/tempents.cpp new file mode 100644 index 00000000..381271b6 --- /dev/null +++ b/client/global/tempents.cpp @@ -0,0 +1,103 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// tempents.cpp - client side entity management functions +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" + +void HUD_CreateEntities( void ) +{ +} + +//====================== +// DRAW BEAM EVENT +//====================== +void EV_DrawBeam( void ) +{ + // special effect for displacer + cl_entity_t *view = gEngfuncs.GetViewModel(); + + if( view != NULL ) + { + float life = 1.05; + int m_iBeam = gEngfuncs.pEventAPI->EV_FindModelIndex( "sprites/plasma.spr" ); + + gEngfuncs.pEfxAPI->R_BeamEnts(view->index | 0x1000, view->index | 0x2000, m_iBeam, life, 0.8, 0.5, 0.5, 0.6, 0, 10, 2, 10, 0); + gEngfuncs.pEfxAPI->R_BeamEnts(view->index | 0x1000, view->index | 0x3000, m_iBeam, life, 0.8, 0.5, 0.5, 0.6, 0, 10, 2, 10, 0); + gEngfuncs.pEfxAPI->R_BeamEnts(view->index | 0x1000, view->index | 0x4000, m_iBeam, life, 0.8, 0.5, 0.5, 0.6, 0, 10, 2, 10, 0); + } +} + +//====================== +// Eject Shell +//====================== +void EV_EjectShell( const dstudioevent_t *event, edict_t *entity ) +{ + vec3_t view_ofs, ShellOrigin, ShellVelocity, forward, right, up; + vec3_t origin = entity->origin; + vec3_t angles = entity->angles; + + float fR, fU; + + int shell = g_engfuncs.pEventAPI->EV_FindModelIndex (event->options); + g_engfuncs.pEventAPI->EV_LocalPlayerViewheight( view_ofs ); + origin.z = origin.z - view_ofs[2]; + + for(int j = 0; j < 3; j++ ) + { + if(angles[j] < -180) angles[j] += 360; + else if(angles[j] > 180) angles[j] -= 360; + } + angles.x = -angles.x; + AngleVectors( angles, forward, right, up ); + + fR = gEngfuncs.pfnRandomFloat( 50, 70 ); + fU = gEngfuncs.pfnRandomFloat( 100, 150 ); + + for (int i = 0; i < 3; i++ ) + { + ShellVelocity[i] = p_velocity[i] + right[i] * fR + up[i] * fU + forward[i] * 25; + ShellOrigin[i] = origin[i] + view_ofs[i] + up[i] * -12 + forward[i] * 20 + right[i] * 4; + } + EV_EjectBrass ( ShellOrigin, ShellVelocity, angles[ YAW ], shell, TE_BOUNCE_SHELL ); +} + +void HUD_StudioEvent( const dstudioevent_t *event, edict_t *entity ) +{ + switch( event->event ) + { + case 5001: + gEngfuncs.pEfxAPI->R_MuzzleFlash( (float *)&entity->attachment[0], atoi( event->options) ); + break; + case 5011: + gEngfuncs.pEfxAPI->R_MuzzleFlash( (float *)&entity->attachment[1], atoi( event->options) ); + break; + case 5021: + gEngfuncs.pEfxAPI->R_MuzzleFlash( (float *)&entity->attachment[2], atoi( event->options) ); + break; + case 5031: + gEngfuncs.pEfxAPI->R_MuzzleFlash( (float *)&entity->attachment[3], atoi( event->options) ); + break; + case 5002: + gEngfuncs.pEfxAPI->R_SparkEffect( (float *)&entity->attachment[0], atoi( event->options), -100, 100 ); + break; + // Client side sound + case 5004: + gEngfuncs.pfnPlaySoundByNameAtLocation( (char *)event->options, 1.0, (float *)&entity->attachment[0] ); + break; + // Client side sound with random pitch + case 5005: + gEngfuncs.pEventAPI->EV_PlaySound( entity->index, (float *)&entity->attachment[0], CHAN_WEAPON, (char *)event->options, gEngfuncs.pfnRandomFloat(0.7, 0.9), ATTN_NORM, 0, 85 + gEngfuncs.pfnRandomLong(0,0x1f) ); + break; + // Special event for displacer + case 5050: + EV_DrawBeam(); + break; + case 5060: + EV_EjectShell( event, entity ); + break; + default: + break; + } +} \ No newline at end of file diff --git a/client/global/triapi.cpp b/client/global/triapi.cpp new file mode 100644 index 00000000..cfb80c3b --- /dev/null +++ b/client/global/triapi.cpp @@ -0,0 +1,15 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// triapi.cpp - triangle rendering, if any +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" + +void HUD_DrawNormalTriangles( void ) +{ +} + +void HUD_DrawTransparentTriangles( void ) +{ +} \ No newline at end of file diff --git a/client/global/vector.h b/client/global/vector.h new file mode 100644 index 00000000..9eac207b --- /dev/null +++ b/client/global/vector.h @@ -0,0 +1,243 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// vector.h - shared vector operations +//======================================================================= +#ifndef VECTOR_H +#define VECTOR_H + +#include +#include +#include +#include + +#pragma warning(disable : 4244) // int or float down-conversion + + // Header file containing definition of globalvars_t and entvars_t +typedef int func_t; // +typedef int string_t; // from engine's pr_comp.h; +typedef float vec_t; // needed before including progdefs.h + +//========================================================= +// 2DVector - used for many pathfinding and many other +// operations that are treated as planar rather than 3d. +//========================================================= +class Vector2D +{ +public: + inline Vector2D(void) { } + inline Vector2D(float X, float Y) { x = X; y = Y; } + inline Vector2D operator+(const Vector2D& v) const { return Vector2D(x+v.x, y+v.y); } + inline Vector2D operator-(const Vector2D& v) const { return Vector2D(x-v.x, y-v.y); } + inline Vector2D operator*(float fl) const { return Vector2D(x*fl, y*fl); } + inline Vector2D operator/(float fl) const { return Vector2D(x/fl, y/fl); } + + inline float Length(void) const { return (float)sqrt(x*x + y*y ); } + inline Vector2D Normalize ( void ) const + { + Vector2D vec2; + + float flLen = Length(); + if ( flLen == 0 ) + { + return Vector2D( (float)0, (float)0 ); + } + else + { + flLen = 1 / flLen; + return Vector2D( x * flLen, y * flLen ); + } + } + vec_t x, y; +}; + +#define nanmask (255<<23) +#define IS_NAN(x) (((*(int *)&x)&nanmask)==nanmask) + +inline float DotProduct(const Vector2D& a, const Vector2D& b) { return( a.x*b.x + a.y*b.y ); } +inline Vector2D operator*(float fl, const Vector2D& v) { return v * fl; } + +//========================================================= +// 3D Vector +//========================================================= +class Vector // same data-layout as engine's vec3_t, +{ // which is a vec_t[3] +public: + // Construction/destruction + inline Vector(void) { } + inline Vector(float X, float Y, float Z) { x = X; y = Y; z = Z; } + inline Vector(const Vector& v) { x = v.x; y = v.y; z = v.z; } + inline Vector(float rgfl[3]) { x = rgfl[0]; y = rgfl[1]; z = rgfl[2]; } + + // Initialization + void Init(vec_t ix=0.0f, vec_t iy=0.0f, vec_t iz=0.0f){ x = ix; y = iy; z = iz; } + + // Operators + inline Vector operator-(void) const { return Vector(-x,-y,-z); } + inline int operator==(const Vector& v) const { return x==v.x && y==v.y && z==v.z; } + inline int operator!=(const Vector& v) const { return !(*this==v); } + inline Vector operator+(const Vector& v) const { return Vector(x+v.x, y+v.y, z+v.z); } + inline Vector operator-(const Vector& v) const { return Vector(x-v.x, y-v.y, z-v.z); } + inline Vector operator*(float fl) const { return Vector(x*fl, y*fl, z*fl); } + inline Vector operator/(float fl) const { return Vector(x/fl, y/fl, z/fl); } + + _forceinline Vector& operator+=(const Vector &v) + { + x+=v.x; y+=v.y; z += v.z; + return *this; + } + _forceinline Vector& operator-=(const Vector &v) + { + x-=v.x; y-=v.y; z -= v.z; + return *this; + } + _forceinline Vector& operator*=(const Vector &v) + { + x *= v.x; y *= v.y; z *= v.z; + return *this; + } + _forceinline Vector& operator*=(float s) + { + x *= s; y *= s; z *= s; + return *this; + } + _forceinline Vector& operator/=(const Vector &v) + { + x /= v.x; y /= v.y; z /= v.z; + return *this; + } + _forceinline Vector& operator/=(float s) + { + float oofl = 1.0f / s; + x *= oofl; y *= oofl; z *= oofl; + return *this; + } + _forceinline Vector MA( const Vector &start, float scale, const Vector &direction ) const + { + return Vector(start.x + scale * direction.x, start.y + scale * direction.y, start.z + scale * direction.z) ; + } + + // Methods + inline void CopyToArray(float* rgfl) const { rgfl[0] = x, rgfl[1] = y, rgfl[2] = z; } + inline float Length(void) const { return (float)sqrt(x*x + y*y + z*z); } + operator float *() { return &x; } // Vectors will now automatically convert to float * when needed + operator const float *() const { return &x; } // Vectors will now automatically convert to float * when needed + + // array access... + vec_t operator[](int i) const { return ((vec_t*)this)[i];} + vec_t& operator[](int i) { return ((vec_t*)this)[i];} + + inline Vector Normalize(void) const + { + float flLen = Length(); + if (flLen == 0) return Vector(0,0,1); // ???? + flLen = 1 / flLen; + return Vector(x * flLen, y * flLen, z * flLen); + } + vec_t Dot(Vector const& vOther) const + { + return(x*vOther.x+y*vOther.y+z*vOther.z); + } + Vector Cross(const Vector &vOther) const + { + return Vector(y*vOther.z - z*vOther.y, z*vOther.x - x*vOther.z, x*vOther.y - y*vOther.x); + } + inline Vector2D Make2D ( void ) const + { + Vector2D Vec2; + Vec2.x = x; + Vec2.y = y; + return Vec2; + } + + inline float Length2D(void) const { return (float)sqrt(x*x + y*y); } + // Members + vec_t x, y, z; +}; +inline Vector operator*(float fl, const Vector& v) { return v * fl; } +inline float DotProduct(const Vector& a, const Vector& b) { return(a.x*b.x+a.y*b.y+a.z*b.z); } +inline Vector CrossProduct(const Vector& a, const Vector& b) { return Vector( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x ); } +#define vec3_t Vector + +//========================================================= +// 4D Vector - for matrix operations +//========================================================= +class Vector4D +{ +public: + // Members + vec_t x, y, z, w; + + // Construction/destruction + Vector4D(void){} + Vector4D(vec_t X, vec_t Y, vec_t Z, vec_t W) { x = X; y = Y; z = Z; w = W;} + Vector4D(double X, double Y, double Z, double W) { x = (double)X; y = (double)Y; z = (double)Z; w = (double)W;} + Vector4D(const float *pFloat) { x = pFloat[0]; y = pFloat[1]; z = pFloat[2]; w = pFloat[3];} + + // array access... + vec_t operator[](int i) const { return ((vec_t*)this)[i];} + vec_t& operator[](int i) { return ((vec_t*)this)[i];} + + // equality + bool operator==(const Vector4D& src) const{ return(src.x == x) && (src.y == y) && (src.z == z) && (src.w == w);} + bool operator!=(const Vector4D& src) const{ return(src.x != x) || (src.y != y) || (src.z != z) || (src.w != w);} + + // arithmetic operations + Vector4D& operator+=(const Vector4D &v){ x+=v.x; y+=v.y; z += v.z; return *this;} + Vector4D& operator-=(const Vector4D &v){ x-=v.x; y-=v.y; z -= v.z; return *this;} + Vector4D operator+ (const Vector4D &v)const {Vector4D res; res.x = x + v.x; res.y = y + v.y; res.z = z + v.z; res.w = w; return res;} + Vector4D operator- (const Vector4D &v)const {Vector4D res; res.x = x - v.x; res.y = y - v.y; res.z = z - v.z; res.w = w; return res;} + Vector4D operator* (const Vector4D &v)const {Vector4D res; res.x = y * v.z - z * v.y; res.y = z * v.x - x * v.z; res.z = x * v.y - y * v.x; res.w = w; return res;} + float operator% (const Vector4D &v)const { return (x * v.x + y * v.y + z * v.z); } + Vector4D Scale( float scale)const {Vector4D res; res.x = x * scale; res.y = y * scale; res.z = z * scale; res.w = w;return res; } + Vector4D CompProduct (const Vector4D &v)const {Vector4D res; res.x = x * v.x; res.y = y * v.y; res.z = z * v.z; res.w = w; return res;} +}; + +//========================================================= +// RandomRange - for random values +//========================================================= +class RandomRange +{ +public: + float m_flMax, m_flMin;//class members + + RandomRange() { m_flMin = m_flMax = 0; } + RandomRange(float fValue) { m_flMin = m_flMax = fValue; } + RandomRange(float fMin, float fMax) { m_flMin = fMin; m_flMax = fMax; } + RandomRange( char *szToken ) + { + char *cOneDot = NULL; + m_flMin = m_flMax = 0; + + for (char *c = szToken; *c; c++) + { + if (*c == '.') + { + if (cOneDot != NULL) + { + // found two dots in a row - it's a range + + *cOneDot = 0; // null terminate the first number + m_flMin = atof(szToken); // parse the first number + *cOneDot = '.'; // change it back, just in case + c++; + m_flMax = atof(c); // parse the second number + return; + } + else cOneDot = c; + } + else cOneDot = NULL; + } + + // no range, just record the number + m_flMax = m_flMin = atof(szToken); + } + + // FIXME: float Random() { return RANDOM_FLOAT(m_flMin, m_flMax); } + float Random() { return m_flMin - m_flMax; } + + // array access... + float operator[](int i) const { return ((float*)this)[i];} + float& operator[](int i) { return ((float*)this)[i];} +}; + +#endif \ No newline at end of file diff --git a/client/global/view.cpp b/client/global/view.cpp new file mode 100644 index 00000000..79ba24bf --- /dev/null +++ b/client/global/view.cpp @@ -0,0 +1,79 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// view.cpp - view/refresh setup functions +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +void V_CalcShake( void ) +{ + float frametime; + int i; + float fraction, freq; + + if( gHUD.m_Shake.time == 0 ) + return; + + if(( gHUD.m_flTime > gHUD.m_Shake.time ) || gHUD.m_Shake.duration <= 0 + || gHUD.m_Shake.amplitude <= 0 || gHUD.m_Shake.frequency <= 0 ) + { + memset( &gHUD.m_Shake, 0, sizeof( gHUD.m_Shake )); + return; + } + + frametime = gHUD.m_flTimeDelta; + + if( gHUD.m_flTime > gHUD.m_Shake.nextShake ) + { + // higher frequency means we recalc the extents more often and perturb the display again + gHUD.m_Shake.nextShake = gHUD.m_flTime + (1.0f / gHUD.m_Shake.frequency); + + // Compute random shake extents (the shake will settle down from this) + for( i = 0; i < 3; i++ ) + { + gHUD.m_Shake.offset[i] = RANDOM_FLOAT( -gHUD.m_Shake.amplitude, gHUD.m_Shake.amplitude ); + } + gHUD.m_Shake.angle = RANDOM_FLOAT( -gHUD.m_Shake.amplitude * 0.25, gHUD.m_Shake.amplitude * 0.25 ); + } + + // ramp down amplitude over duration (fraction goes from 1 to 0 linearly with slope 1/duration) + fraction = ( gHUD.m_Shake.time - gHUD.m_flTime ) / gHUD.m_Shake.duration; + + // ramp up frequency over duration + if( fraction ) + { + freq = (gHUD.m_Shake.frequency / fraction); + } + else + { + freq = 0; + } + + // square fraction to approach zero more quickly + fraction *= fraction; + + // Sine wave that slowly settles to zero + fraction = fraction * sin( gHUD.m_flTime * freq ); + + // add to view origin + gHUD.m_Shake.appliedOffset = gHUD.m_Shake.offset * fraction; + + // add to roll + gHUD.m_Shake.appliedAngle = gHUD.m_Shake.angle * fraction; + + // drop amplitude a bit, less for higher frequency shakes + float localAmp = gHUD.m_Shake.amplitude * ( frametime / ( gHUD.m_Shake.duration * gHUD.m_Shake.frequency )); + gHUD.m_Shake.amplitude -= localAmp; +} + +void V_ApplyShake( Vector& origin, Vector& angles, float factor ) +{ + origin.MA( origin, factor, gHUD.m_Shake.appliedOffset ); + angles.z += gHUD.m_Shake.appliedAngle * factor; +} + +void V_CalcRefdef( ref_params_t *parms ) +{ +} \ No newline at end of file diff --git a/client/hud/hud.cpp b/client/hud/hud.cpp new file mode 100644 index 00000000..ee02a0ac --- /dev/null +++ b/client/hud/hud.cpp @@ -0,0 +1,341 @@ +//======================================================================= +// Copyright (C) XashXT Group 2007 +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +extern float HUD_GetFOV( void ); +extern float v_idlescale; +float in_fov; + +void CHud :: Init( void ) +{ + InitMessages(); + m_Ammo.Init(); + m_Health.Init(); + m_SayText.Init(); + m_Geiger.Init(); + m_Train.Init(); + m_Battery.Init(); + m_Flash.Init(); + m_Redeemer.Init(); + m_Zoom.Init(); + m_Message.Init(); + m_Scoreboard.Init(); + m_StatusBar.Init(); + m_DeathNotice.Init(); + m_AmmoSecondary.Init(); + m_TextMessage.Init(); + m_StatusIcons.Init(); + m_Menu.Init(); + m_Sound.Init(); + m_MOTD.Init(); + + MsgFunc_ResetHUD(0, 0, NULL ); +} + +CHud :: ~CHud( void ) +{ + m_Sound.Close(); + + if( m_pHudList ) + { + HUDLIST *pList; + while( m_pHudList ) + { + pList = m_pHudList; + m_pHudList = m_pHudList->pNext; + FREE( pList ); + } + m_pHudList = NULL; + } +} + +int CHud :: GetSpriteIndex( const char *SpriteName ) +{ + // use built-in Shader Manager + return LOAD_SHADER( SpriteName ); +} + +void CHud :: VidInit( void ) +{ + // ---------- + // Load Sprites + // --------- + + m_hsprCursor = 0; + m_hHudError = 0; + + // TODO: build real table of fonts widthInChars + for( int i = 0; i < 256; i++ ) + charWidths[i] = SMALLCHAR_WIDTH; + iCharHeight = SMALLCHAR_HEIGHT; + + // assumption: number_1, number_2, etc, are all listed and loaded sequentially + m_HUD_number_0 = GetSpriteIndex( "number_0" ); + GetImageSize( NULL, &m_iFontHeight, m_HUD_number_0 ); + + // loading error sprite + m_HUD_error = GetSpriteIndex( "error" ); + m_hHudError = GetSprite( m_HUD_error ); + + m_Sound.VidInit(); + m_Ammo.VidInit(); + m_Health.VidInit(); + m_Geiger.VidInit(); + m_Train.VidInit(); + m_Battery.VidInit(); + m_Flash.VidInit(); + m_Redeemer.VidInit(); + m_Zoom.VidInit(); + m_MOTD.VidInit(); + m_Message.VidInit(); + m_Scoreboard.VidInit(); + m_StatusBar.VidInit(); + m_DeathNotice.VidInit(); + m_SayText.VidInit(); + m_Menu.VidInit(); + m_AmmoSecondary.VidInit(); + m_TextMessage.VidInit(); + m_StatusIcons.VidInit(); +} + +void CHud :: Think( void ) +{ + HUDLIST *pList = m_pHudList; + + while( pList ) + { + if (pList->p->m_iFlags & HUD_ACTIVE) + pList->p->Think(); + pList = pList->pNext; + } + + // think about default fov + if( m_iFOV == 0 ) + { + // only let players adjust up in fov, and only if they are not overriden by something else + m_iFOV = max( CVAR_GET_FLOAT( "default_fov" ), 90 ); + } +} + +int CHud :: UpdateClientData( ref_params_t *cdata, float time ) +{ + memcpy( m_vecOrigin, cdata->origin, sizeof( vec3_t )); + memcpy( m_vecAngles, cdata->angles, sizeof( vec3_t )); + + m_iKeyBits = cdata->iKeyBits; + m_iWeaponBits = cdata->iWeaponBits; + + Think(); + + cdata->fov = m_iFOV; + cdata->iKeyBits = m_iKeyBits; + cdata->fov = m_iFOV; + cdata->v_idlescale = m_iConcussionEffect; + + if( m_flMouseSensitivity ) + cdata->mouse_sensitivity = m_flMouseSensitivity; + + return 1; +} + +int CHud :: Redraw( float flTime, int intermission ) +{ + m_fOldTime = m_flTime; // save time of previous redraw + m_flTime = flTime; + m_flTimeDelta = (double)m_flTime - m_fOldTime; + + // clock was reset, reset delta + if( m_flTimeDelta < 0 ) m_flTimeDelta = 0; + + m_iIntermission = intermission; + + // draw screen fade before hud + DrawScreenFade(); + + // redeemer hud stuff + if( m_Redeemer.m_iHudMode > 0 ) + { + m_Redeemer.Draw( flTime ); + return 1; + } + + // zoom hud stuff + if( m_Zoom.m_iHudMode > 0 ) + { + m_Zoom.Draw( flTime ); + return 1; + } + + // custom view active, and flag "draw hud" isn't set + if(( viewFlags & 1 ) && !( viewFlags & 2 )) + return 1; + + if( CVAR_GET_FLOAT( "hud_draw" )) + { + HUDLIST *pList = m_pHudList; + + while( pList ) + { + if( !intermission ) + { + if(( pList->p->m_iFlags & HUD_ACTIVE ) && !(m_iHideHUDDisplay & HIDEHUD_ALL )) + pList->p->Draw(flTime); + } + else + { + // it's an intermission, so only draw hud elements + // that are set to draw during intermissions + if( pList->p->m_iFlags & HUD_INTERMISSION ) + pList->p->Draw( flTime ); + } + pList = pList->pNext; + } + } + return 1; +} + +int CHud :: DrawHudString( int xpos, int ypos, int iMaxX, char *szIt, int r, int g, int b ) +{ + // draw the string until we hit the null character or a newline character + for( ; *szIt != 0 && *szIt != '\n'; szIt++ ) + { + int next = xpos + gHUD.charWidths[*szIt]; // variable-width fonts look cool + if ( next > iMaxX ) + return xpos; + + TextMessageDrawChar( xpos, ypos, *szIt, r, g, b ); + xpos = next; + } + + return xpos; +} + +int CHud :: DrawHudNumberString( int xpos, int ypos, int iMinX, int iNumber, int r, int g, int b ) +{ + char szString[32]; + sprintf( szString, "%d", iNumber ); + return DrawHudStringReverse( xpos, ypos, iMinX, szString, r, g, b ); + +} + +int CHud :: DrawHudStringReverse( int xpos, int ypos, int iMinX, char *szString, int r, int g, int b ) +{ + // find the end of the string + for ( char *szIt = szString; *szIt != 0; szIt++ ) + { // we should count the length? + } + + // iterate throug the string in reverse + for( szIt--; szIt != (szString-1); szIt-- ) + { + int next = xpos - gHUD.charWidths[ *szIt ]; // variable-width fonts look cool + if( next < iMinX ) + return xpos; + xpos = next; + + TextMessageDrawChar( xpos, ypos, *szIt, r, g, b ); + } + + return xpos; +} + +int CHud :: DrawHudNumber( int x, int y, int iFlags, int iNumber, int r, int g, int b ) +{ + int iWidth = GetSpriteRect( m_HUD_number_0 ).right - GetSpriteRect( m_HUD_number_0 ).left; + int k; + + if( iNumber > 0 ) + { + // SPR_Draw 100's + if( iNumber >= 100 ) + { + k = iNumber / 100; + SPR_Set(GetSprite(m_HUD_number_0 + k), r, g, b ); + SPR_DrawAdditive( 0, x, y, &GetSpriteRect(m_HUD_number_0 + k)); + x += iWidth; + } + else if( iFlags & DHN_3DIGITS ) + { + x += iWidth; + } + + // SPR_Draw 10's + if( iNumber >= 10 ) + { + k = (iNumber % 100)/10; + SPR_Set(GetSprite(m_HUD_number_0 + k), r, g, b ); + SPR_DrawAdditive( 0, x, y, &GetSpriteRect(m_HUD_number_0 + k)); + x += iWidth; + } + else if( iFlags & (DHN_3DIGITS|DHN_2DIGITS)) + { + x += iWidth; + } + + // SPR_Draw ones + k = iNumber % 10; + SPR_Set(GetSprite(m_HUD_number_0 + k), r, g, b ); + SPR_DrawAdditive(0, x, y, &GetSpriteRect(m_HUD_number_0 + k)); + x += iWidth; + } + else if( iFlags & DHN_DRAWZERO ) + { + SPR_Set(GetSprite(m_HUD_number_0), r, g, b ); + + // SPR_Draw 100's + if( iFlags & DHN_3DIGITS ) + { + x += iWidth; + } + + if( iFlags & (DHN_3DIGITS|DHN_2DIGITS)) x += iWidth; + SPR_DrawAdditive( 0, x, y, &GetSpriteRect( m_HUD_number_0 )); + x += iWidth; + } + return x; +} + +int CHud::GetNumWidth( int iNumber, int iFlags ) +{ + if( iFlags & DHN_3DIGITS ) return 3; + if( iFlags & DHN_2DIGITS ) return 2; + + if( iNumber <= 0 ) + { + if( iFlags & DHN_DRAWZERO ) + return 1; + else return 0; + } + + if( iNumber < 10 ) return 1; + if( iNumber < 100 ) return 2; + + return 3; + +} + +void CHud::AddHudElem( CHudBase *phudelem ) +{ + HUDLIST *pdl, *ptemp; + + if( !phudelem ) return; + + pdl = (HUDLIST *)CALLOC( sizeof( HUDLIST ), 1 ); + pdl->p = phudelem; + + if( !m_pHudList ) + { + m_pHudList = pdl; + return; + } + + ptemp = m_pHudList; + + while( ptemp->pNext ) + ptemp = ptemp->pNext; + ptemp->pNext = pdl; +} \ No newline at end of file diff --git a/client/hud/hud.h b/client/hud/hud.h new file mode 100644 index 00000000..f0d53a1c --- /dev/null +++ b/client/hud/hud.h @@ -0,0 +1,680 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// hud.h - hud primary header +//======================================================================= + +#define RGB_YELLOWISH 0x00FFA000 // 255, 160, 0 +#define RGB_REDISH 0x00FF1010 // 255, 160, 0 +#define RGB_GREENISH 0x0000A000 // 0, 160, 0 + +#include "hud_ammo.h" + +#define DHN_DRAWZERO 1 +#define DHN_2DIGITS 2 +#define DHN_3DIGITS 4 +#define MIN_ALPHA 100 + +#define HUDELEM_ACTIVE 1 + +typedef struct +{ + int x, y; +} POSITION; + +// This structure is sent over the net to describe a screen shake event +typedef struct +{ + float time; + float duration; + float amplitude; + float frequency; + float nextShake; + Vector offset; + float angle; + Vector appliedOffset; + float appliedAngle; +} ScreenShake; + +typedef struct +{ + int dripsPerSecond; + float distFromPlayer; + float windX, windY; + float randX, randY; + int weatherMode; // 0 - snow, 1 - rain + float globalHeight; +} RainData; + +typedef struct +{ + byte r, g, b, a; +} RGBA; + +#define HUD_ACTIVE 1 +#define HUD_INTERMISSION 2 +#define MAX_SEC_AMMO_VALUES 4 +#define MAX_PLAYER_NAME_LENGTH 32 +#define MAX_MOTD_LENGTH 1536 +#define FADE_TIME 100 +#define maxHUDMessages 16 +#define MAX_SPRITE_NAME_LENGTH 24 + +// +//----------------------------------------------------- +// +class CHudBase +{ +public: + POSITION m_pos; + int m_type; + int m_iFlags; // active, moving, + + virtual ~CHudBase(){ } + virtual int Init( void ){ return 0; } + virtual int VidInit( void ){ return 0; } + virtual int Draw( float flTime ){ return 0; } + virtual void Think( void ){ return; } + virtual void Reset( void ){ return; } + virtual void InitHUDData( void ){ } // called every time a server is connected to +}; + +struct HUDLIST +{ + CHudBase *p; + HUDLIST *pNext; +}; + +// +//----------------------------------------------------- +// +class CHudAmmo: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw(float flTime); + void Think(void); + void Reset(void); + int DrawWList(float flTime); + int MsgFunc_CurWeapon( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_WeaponList( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_AmmoX(const char *pszName, int iSize, void *pbuf ); + int MsgFunc_AmmoPickup( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_WeapPickup( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_ItemPickup( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_HideWeapon( const char *pszName, int iSize, void *pbuf ); + + void SlotInput( int iSlot ); + void _cdecl UserCmd_Slot1( void ); + void _cdecl UserCmd_Slot2( void ); + void _cdecl UserCmd_Slot3( void ); + void _cdecl UserCmd_Slot4( void ); + void _cdecl UserCmd_Slot5( void ); + void _cdecl UserCmd_Slot6( void ); + void _cdecl UserCmd_Slot7( void ); + void _cdecl UserCmd_Slot8( void ); + void _cdecl UserCmd_Slot9( void ); + void _cdecl UserCmd_Slot10( void ); + void _cdecl UserCmd_Close( void ); + void _cdecl UserCmd_NextWeapon( void ); + void _cdecl UserCmd_PrevWeapon( void ); + +private: + float m_fFade; + RGBA m_rgba; + WEAPON *m_pWeapon; + int m_HUD_bucket0; + int m_HUD_selection; + +}; + +// +//----------------------------------------------------- +// +class CHudAmmoSecondary: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + void Reset( void ); + int Draw(float flTime); + + int MsgFunc_SecAmmoVal( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_SecAmmoIcon( const char *pszName, int iSize, void *pbuf ); + +private: + int m_HUD_ammoicon; // sprite indices + int m_iAmmoAmounts[MAX_SEC_AMMO_VALUES]; + float m_fFade; +}; + + +#include "hud_health.h" + +// +//----------------------------------------------------- +// +class CHudGeiger: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_Geiger( const char *pszName, int iSize, void *pbuf ); + +private: + int m_iGeigerRange; +}; + +// +//----------------------------------------------------- +// +class CHudTrain: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_Train( const char *pszName, int iSize, void *pbuf ); + +private: + HSPRITE m_hSprite; + int m_iPos; + +}; + +class CHudMOTD : public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + void Reset( void ); + + int MsgFunc_MOTD( const char *pszName, int iSize, void *pbuf ); + +protected: + static int MOTD_DISPLAY_TIME; + char m_szMOTD[MAX_MOTD_LENGTH]; + float m_flActiveTill; + int m_iLines; +}; + +// +//----------------------------------------------------- +// +class CHudSound: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int MsgFunc_Fsound( const char *pszName, int iSize, void *pbuf ); + int PlayStream( const char* name ); + int Draw( float flTime ); // used for get pause + int Close( void ); +private: + int m_iStatus; + int m_iTime; +}; + +// +//----------------------------------------------------- +// +class CHudScoreboard: public CHudBase +{ +public: + int Init( void ); + void InitHUDData( void ); + int VidInit( void ); + int Draw( float flTime ); + int DrawPlayers( int xoffset, float listslot, int nameoffset = 0, char *team = NULL ); // returns the ypos where it finishes drawing + void UserCmd_ShowScores( void ); + void UserCmd_HideScores( void ); + int MsgFunc_ScoreInfo( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_TeamInfo( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_TeamScore( const char *pszName, int iSize, void *pbuf ); + void DeathMsg( int killer, int victim ); + + enum + { + MAX_PLAYERS = 64, + MAX_TEAMS = 64, + MAX_TEAM_NAME = 16, + }; + + struct extra_player_info_t + { + short frags; + short deaths; + char teamname[MAX_TEAM_NAME]; + }; + + struct team_info_t + { + char name[MAX_TEAM_NAME]; + short frags; + short deaths; + short ping; + short packetloss; + short ownteam; + short players; + int already_drawn; + int scores_overriden; + }; + + hud_player_info_t m_PlayerInfoList[MAX_PLAYERS+1]; // player info from the engine + extra_player_info_t m_PlayerExtraInfo[MAX_PLAYERS+1]; // additional player info sent directly to the client.dll + team_info_t m_TeamInfo[MAX_TEAMS+1]; + + int m_iNumTeams; + int m_iLastKilledBy; + int m_fLastKillTime; + int m_iPlayerNum; + int m_iShowscoresHeld; + + void GetAllPlayersInfo( void ); +}; + +// +//----------------------------------------------------- +// +class CHudStatusBar : public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + void Reset( void ); + void ParseStatusString( int line_num ); + + int MsgFunc_StatusText( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_StatusValue( const char *pszName, int iSize, void *pbuf ); + +protected: + enum + { + MAX_STATUSTEXT_LENGTH = 128, + MAX_STATUSBAR_VALUES = 8, + MAX_STATUSBAR_LINES = 2, + }; + + char m_szStatusText[MAX_STATUSBAR_LINES][MAX_STATUSTEXT_LENGTH]; // a text string describing how the status bar is to be drawn + char m_szStatusBar[MAX_STATUSBAR_LINES][MAX_STATUSTEXT_LENGTH]; // the constructed bar that is drawn + int m_iStatusValues[MAX_STATUSBAR_VALUES]; // an array of values for use in the status bar + + int m_bReparseString; // set to TRUE whenever the m_szStatusBar needs to be recalculated + + // an array of colors...one color for each line + float *m_pflNameColors[MAX_STATUSBAR_LINES]; +}; + +// +//----------------------------------------------------- +// +class CHudDeathNotice : public CHudBase +{ +public: + int Init( void ); + void InitHUDData( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_DeathMsg( const char *pszName, int iSize, void *pbuf ); + +private: + int m_HUD_d_skull; // sprite index of skull icon +}; + +// +//----------------------------------------------------- +// +class CHudMenu : public CHudBase +{ +public: + int Init( void ); + void InitHUDData( void ); + int VidInit( void ); + void Reset( void ); + int Draw( float flTime ); + int MsgFunc_ShowMenu( const char *pszName, int iSize, void *pbuf ); + + void SelectMenuItem( int menu_item ); + + int m_fMenuDisplayed; + int m_bitsValidSlots; + float m_flShutoffTime; + int m_fWaitingForMore; +}; + +// +//----------------------------------------------------- +// +class CHudSayText : public CHudBase +{ +public: + int Init( void ); + void InitHUDData( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_SayText( const char *pszName, int iSize, void *pbuf ); + void SayTextPrint( const char *pszBuf, int iBufSize, int clientIndex = -1 ); + void EnsureTextFitsInOneLineAndWrapIfHaveTo( int line ); +private: + + float m_HUD_saytext; + float m_HUD_saytext_time; +}; + +// +//----------------------------------------------------- +// +class CHudBattery: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_Battery(const char *pszName, int iSize, void *pbuf ); + +private: + HSPRITE m_hSprite1; + HSPRITE m_hSprite2; + wrect_t *m_prc1; + wrect_t *m_prc2; + int m_iBat; + float m_fFade; + int m_iHeight; // width of the battery innards +}; + + +// +//----------------------------------------------------- +// +class CHudFlashlight: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw(float flTime); + void Reset( void ); + int MsgFunc_Flashlight( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_FlashBat( const char *pszName, int iSize, void *pbuf ); + +private: + HSPRITE m_hSprite1; + HSPRITE m_hSprite2; + HSPRITE m_hBeam; + wrect_t *m_prc1; + wrect_t *m_prc2; + wrect_t *m_prcBeam; + float m_flBat; + int m_iBat; + int m_fOn; + float m_fFade; + int m_iWidth; // width of the battery innards +}; + +class CHudRedeemer: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_WarHUD( const char *pszName, int iSize, void *pbuf ); + int m_iHudMode; +private: + HSPRITE m_hSprite; + HSPRITE m_hCrosshair; + HSPRITE m_hStatic; + HSPRITE m_hCamera; + HSPRITE m_hCamRec; +}; + +class CHudZoom: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_ZoomHUD( const char *pszName, int iSize, void *pbuf ); + int m_iHudMode; +private: + HSPRITE m_hCrosshair; + HSPRITE m_hLines; +}; +// +//----------------------------------------------------- +// +struct message_parms_t +{ + client_textmessage_t *pMessage; + float time; + int x, y; + int totalWidth, totalHeight; + int width; + int lines; + int lineLength; + int length; + int r, g, b; + int text; + int fadeBlend; + float charTime; + float fadeTime; +}; + +// +//----------------------------------------------------- +// +class CHudTextMessage: public CHudBase +{ +public: + int Init( void ); + static char *LocaliseTextString( const char *msg, char *dst_buffer, int buffer_size ); + static char *BufferedLocaliseTextString( const char *msg ); + char *LookupString( const char *msg_name, int *msg_dest = NULL ); + int MsgFunc_TextMsg( const char *pszName, int iSize, void *pbuf ); +}; + +// +//----------------------------------------------------- +// + +class CHudMessage: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + int Draw( float flTime ); + int MsgFunc_HudText( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_GameTitle( const char *pszName, int iSize, void *pbuf ); + + float FadeBlend( float fadein, float fadeout, float hold, float localTime ); + int XPosition( float x, int width, int lineWidth ); + int YPosition( float y, int height ); + + void MessageAdd( const char *pName, float time ); + void MessageAdd( client_textmessage_t * newMessage ); + void MessageDrawScan( client_textmessage_t *pMessage, float time ); + void MessageScanStart( void ); + void MessageScanNextChar( void ); + void Reset( void ); + +private: + client_textmessage_t *m_pMessages[maxHUDMessages]; + float m_startTime[maxHUDMessages]; + message_parms_t m_parms; + float m_gameTitleTime; + client_textmessage_t *m_pGameTitle; + int HUD_Logo; // display logo +}; + +// +//----------------------------------------------------- +// +class CHudStatusIcons: public CHudBase +{ +public: + int Init( void ); + int VidInit( void ); + void Reset( void ); + int Draw( float flTime ); + int MsgFunc_StatusIcon( const char *pszName, int iSize, void *pbuf ); + + enum + { + MAX_ICONSPRITENAME_LENGTH = MAX_SPRITE_NAME_LENGTH, + MAX_ICONSPRITES = 4, + }; + + // had to make these public so CHud could access them (to enable concussion icon) + // could use a friend declaration instead... + void EnableIcon( char *pszIconName, byte red, byte green, byte blue ); + void DisableIcon( char *pszIconName ); + +private: + + typedef struct + { + char szSpriteName[MAX_ICONSPRITENAME_LENGTH]; + HSPRITE spr; + wrect_t rc; + byte r, g, b; + } icon_sprite_t; + + icon_sprite_t m_IconList[MAX_ICONSPRITES]; +}; + +// +//----------------------------------------------------- +// + +#define SKY_OFF 0 +#define SKY_ON 1 + +class CHud +{ +private: + HUDLIST *m_pHudList; + float m_flMouseSensitivity; + int m_iConcussionEffect; + +public: + HSPRITE m_hsprCursor; + float m_flTime; // the current client time + float m_fOldTime; // the time at which the HUD was last redrawn + double m_flTimeDelta;// the difference between flTime and fOldTime + float m_vecOrigin[3]; + float m_vecAngles[3]; + int m_iKeyBits; + int m_iHideHUDDisplay; + int m_iFOV; + int m_Teamplay; + int m_iRes; + Vector m_vecSkyPos; + int m_iSkyMode; + int m_iCameraMode; + int m_iLastCameraMode; + int m_iFontHeight; + int DrawHudNumber( int x, int y, int iFlags, int iNumber, int r, int g, int b ); + int DrawHudString( int x, int y, int iMaxX, char *szString, int r, int g, int b ); + int DrawHudStringReverse( int xpos, int ypos, int iMinX, char *szString, int r, int g, int b ); + int DrawHudNumberString( int xpos, int ypos, int iMinX, int iNumber, int r, int g, int b ); + int GetNumWidth( int iNumber, int iFlags ); + int viewEntityIndex; + int m_iHUDColor; + int viewFlags; +private: + wrect_t m_Rect; +public: + HSPRITE GetSprite( int index ) { return (HSPRITE)index; } + wrect_t& GetSpriteRect( HSPRITE index ) + { + m_Rect.left = m_Rect.top = 0; + GetImageSize( &m_Rect.right, &m_Rect.bottom, index ); + return m_Rect; + } + int InitMessages( void ); // init hud messages + int GetSpriteIndex( const char *SpriteName ); + + CHudAmmo m_Ammo; + CHudHealth m_Health; + CHudGeiger m_Geiger; + CHudBattery m_Battery; + CHudTrain m_Train; + CHudFlashlight m_Flash; + CHudRedeemer m_Redeemer; + CHudZoom m_Zoom; + CHudMessage m_Message; + CHudScoreboard m_Scoreboard; + CHudStatusBar m_StatusBar; + CHudDeathNotice m_DeathNotice; + CHudSayText m_SayText; + CHudMenu m_Menu; + CHudAmmoSecondary m_AmmoSecondary; + CHudTextMessage m_TextMessage; + CHudStatusIcons m_StatusIcons; + CHudSound m_Sound; + CHudMOTD m_MOTD; + + void Init( void ); + void VidInit( void ); + void Think( void ); + int Redraw( float flTime, int intermission ); + int UpdateClientData( ref_params_t *pparams, float time ); + + CHud() : m_pHudList(NULL) { } + ~CHud(); // destructor, frees allocated memory + + // user messages + int _cdecl MsgFunc_Damage( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_GameMode( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_ServerName( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_ResetHUD( const char *pszName, int iSize, void *pbuf); + int _cdecl MsgFunc_InitHUD( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_ViewMode( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_SetFOV( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_Concuss( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_ScreenShake( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_RainData( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_HUDColor( const char *pszName, int iSize, void *pbuf); + int _cdecl MsgFunc_SetFog( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_SetSky( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_CamData( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_SetBody( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_SetSkin( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_AddScreen( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_AddMirror( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_AddPortal( const char *pszName, int iSize, void *pbuf ); + int _cdecl MsgFunc_Particle( const char *pszName, int iSize, void *pbuf ); + + // from m_screenInfo + byte charWidths[256]; + int iCharHeight; + + qword m_iWeaponBits; + int m_fPlayerDead; + int m_iIntermission; + + RainData Rain; // buz rain + + // fog stuff + Vector m_FogColor; + float m_fStartDist; + float m_fEndDist; + int m_iFinalEndDist; + float m_fFadeDuration; + + // sprite indexes + int m_HUD_number_0; + + // screen shake handler + ScreenShake m_Shake; + + // error sprite + int m_HUD_error; + HSPRITE m_hHudError; + + void AddHudElem( CHudBase *p ); + float GetSensitivity() { return m_flMouseSensitivity; } +}; + +extern CHud gHUD; \ No newline at end of file diff --git a/client/hud/hud_ammo.cpp b/client/hud/hud_ammo.cpp new file mode 100644 index 00000000..e7fa41f7 --- /dev/null +++ b/client/hud/hud_ammo.cpp @@ -0,0 +1,1189 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// Ammo.cpp +// +// implementation of CHudAmmo class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" +#include "hud_ammohistory.h" + +WEAPON *gpActiveSel; // NULL means off, 1 means just the menu bar, otherwise +WEAPON *gpLastSel; // Last weapon menu selection +WeaponsResource gWR; +int g_weaponselect = 0; + +void WeaponsResource :: LoadWeaponSprite( WEAPON *pWeapon, const char *type ) +{ + if( !pWeapon ) return; + + // fmt + HSPRITE hSprite = LOAD_SHADER( va( "%s::%s\n", pWeapon->szName, type )); + + if( !strcmp( type, "crosshair" )) + { + pWeapon->hCrosshair = hSprite; + pWeapon->rcCrosshair = gHUD.GetSpriteRect( hSprite ); + } + else if( !strcmp( type, "autoaim" )) + { + pWeapon->hAutoaim = hSprite; + pWeapon->rcAutoaim = gHUD.GetSpriteRect( hSprite ); + } + else if( !strcmp( type, "zoom" )) + { + pWeapon->hZoomedCrosshair = hSprite; + pWeapon->rcZoomedCrosshair = gHUD.GetSpriteRect( hSprite ); + } + else if( !strcmp( type, "zoom_autoaim" )) + { + pWeapon->hZoomedAutoaim = hSprite; + pWeapon->rcZoomedAutoaim = gHUD.GetSpriteRect( hSprite ); + } + else if( !strcmp( type, "weapon" )) + { + pWeapon->hInactive = hSprite; + pWeapon->rcInactive = gHUD.GetSpriteRect( hSprite ); + gHR.iHistoryGap = max( gHR.iHistoryGap, pWeapon->rcActive.bottom - pWeapon->rcActive.top ); + } + else if( !strcmp( type, "weapon_s" )) + { + pWeapon->hActive = hSprite; + pWeapon->rcActive = gHUD.GetSpriteRect( hSprite ); + } + else if( !strcmp( type, "ammo" )) + { + pWeapon->hAmmo = hSprite; + pWeapon->rcAmmo = gHUD.GetSpriteRect( hSprite ); + } + else if( !strcmp( type, "ammo2" )) + { + pWeapon->hAmmo2 = hSprite; + pWeapon->rcAmmo2 = gHUD.GetSpriteRect( hSprite ); + } +} + +void WeaponsResource :: LoadAllWeaponSprites( void ) +{ + for (int i = 0; i < MAX_WEAPONS; i++) + { + if ( rgWeapons[i].iId ) LoadWeaponSprites( &rgWeapons[i] ); + } +} + +int WeaponsResource :: CountAmmo( int iId ) +{ + if ( iId < 0 ) + return 0; + + return riAmmo[iId]; +} + +int WeaponsResource :: HasAmmo( WEAPON *p ) +{ + if( !p ) + return FALSE; + + // weapons with no max ammo can always be selected + if( p->iMax1 == -1 ) + return TRUE; + + return (p->iAmmoType == -1) || p->iClip > 0 || CountAmmo(p->iAmmoType) + || CountAmmo(p->iAmmo2Type) || ( p->iFlags & WEAPON_FLAGS_SELECTONEMPTY ); +} + +void WeaponsResource :: LoadWeaponSprites( WEAPON *pWeapon ) +{ + if( !pWeapon ) return; + + memset( &pWeapon->rcActive, 0, sizeof( wrect_t )); + memset( &pWeapon->rcInactive, 0, sizeof( wrect_t )); + memset( &pWeapon->rcAmmo, 0, sizeof( wrect_t )); + memset( &pWeapon->rcAmmo2, 0, sizeof( wrect_t )); + pWeapon->hInactive = 0; + pWeapon->hActive = 0; + pWeapon->hAmmo = 0; + pWeapon->hAmmo2 = 0; + + // find specified shaders + LoadWeaponSprite( pWeapon, "crosshair" ); + LoadWeaponSprite( pWeapon, "autoaim" ); + LoadWeaponSprite( pWeapon, "zoom" ); + LoadWeaponSprite( pWeapon, "zoom_autoaim" ); + LoadWeaponSprite( pWeapon, "weapon" ); + LoadWeaponSprite( pWeapon, "weapon_s" ); + LoadWeaponSprite( pWeapon, "ammo" ); + LoadWeaponSprite( pWeapon, "ammo2" ); +} + +// Returns the first weapon for a given slot. +WEAPON *WeaponsResource :: GetFirstPos( int iSlot ) +{ + WEAPON *pret = NULL; + + for (int i = 0; i < MAX_WEAPON_POSITIONS; i++) + { + if( rgSlots[iSlot][i] && HasAmmo( rgSlots[iSlot][i] )) + { + pret = rgSlots[iSlot][i]; + break; + } + } + + return pret; +} + +WEAPON* WeaponsResource :: GetNextActivePos( int iSlot, int iSlotPos ) +{ + if ( iSlotPos >= MAX_WEAPON_POSITIONS || iSlot >= MAX_WEAPON_SLOTS ) + return NULL; + + WEAPON *p = gWR.rgSlots[ iSlot ][ iSlotPos+1 ]; + + if ( !p || !gWR.HasAmmo(p) ) + return GetNextActivePos( iSlot, iSlotPos + 1 ); + + return p; +} + +int giBucketHeight, giBucketWidth, giABHeight, giABWidth; // Ammo Bar width and height + +HSPRITE ghsprBuckets; // Sprite for top row of weapons menu + +DECLARE_MESSAGE( m_Ammo, CurWeapon ); // Current weapon and clip +DECLARE_MESSAGE( m_Ammo, WeaponList ); // new weapon type +DECLARE_MESSAGE( m_Ammo, AmmoX ); // update known ammo type's count +DECLARE_MESSAGE( m_Ammo, AmmoPickup ); // flashes an ammo pickup record +DECLARE_MESSAGE( m_Ammo, WeapPickup ); // flashes a weapon pickup record +DECLARE_MESSAGE( m_Ammo, HideWeapon ); // hides the weapon, ammo, and crosshair displays temporarily +DECLARE_MESSAGE( m_Ammo, ItemPickup ); +DECLARE_MESSAGE( m_AmmoSecondary, SecAmmoVal ); +DECLARE_MESSAGE( m_AmmoSecondary, SecAmmoIcon ); + +DECLARE_COMMAND( m_Ammo, Slot1 ); +DECLARE_COMMAND( m_Ammo, Slot2 ); +DECLARE_COMMAND( m_Ammo, Slot3 ); +DECLARE_COMMAND( m_Ammo, Slot4 ); +DECLARE_COMMAND( m_Ammo, Slot5 ); +DECLARE_COMMAND( m_Ammo, Slot6 ); +DECLARE_COMMAND( m_Ammo, Slot7 ); +DECLARE_COMMAND( m_Ammo, Slot8 ); +DECLARE_COMMAND( m_Ammo, Slot9 ); +DECLARE_COMMAND( m_Ammo, Slot10 ); +DECLARE_COMMAND( m_Ammo, Close ); +DECLARE_COMMAND( m_Ammo, NextWeapon ); +DECLARE_COMMAND( m_Ammo, PrevWeapon ); + +// width of ammo fonts +#define AMMO_SMALL_WIDTH 10 +#define AMMO_LARGE_WIDTH 20 + +#define HISTORY_DRAW_TIME "5" + +int CHudAmmo::Init( void ) +{ + gHUD.AddHudElem(this); + + HOOK_MESSAGE( CurWeapon ); + HOOK_MESSAGE( WeaponList ); + HOOK_MESSAGE( AmmoPickup ); + HOOK_MESSAGE( WeapPickup ); + HOOK_MESSAGE( ItemPickup ); + HOOK_MESSAGE( HideWeapon ); + HOOK_MESSAGE( AmmoX ); + + HOOK_COMMAND( "slot1", Slot1 ); + HOOK_COMMAND( "slot2", Slot2 ); + HOOK_COMMAND( "slot3", Slot3 ); + HOOK_COMMAND( "slot4", Slot4 ); + HOOK_COMMAND( "slot5", Slot5 ); + HOOK_COMMAND( "slot6", Slot6 ); + HOOK_COMMAND( "slot7", Slot7 ); + HOOK_COMMAND( "slot8", Slot8 ); + HOOK_COMMAND( "slot9", Slot9 ); + HOOK_COMMAND( "slot10", Slot10 ); + HOOK_COMMAND( "cancelselect", Close ); + HOOK_COMMAND( "invnext", NextWeapon ); + HOOK_COMMAND( "invprev", PrevWeapon ); + + Reset(); + + CVAR_REGISTER( "hud_drawhistory_time", HISTORY_DRAW_TIME, 0, "weapons pickup history show time" ); + CVAR_REGISTER( "hud_fastswitch", "0", CVAR_ARCHIVE, "controls whether or not weapons can be selected in one keypress" ); + + m_iFlags |= HUD_ACTIVE; //!!! + + gWR.Init(); + gHR.Init(); + + return 1; +} + +void CHudAmmo :: Reset( void ) +{ + m_fFade = 0; + m_iFlags |= HUD_ACTIVE; //!!! + + gpActiveSel = NULL; + gHUD.m_iHideHUDDisplay = 0; + + gWR.Reset(); + gHR.Reset(); + + static wrect_t nullrc; + SetCrosshair( 0, nullrc, 0, 0, 0 ); // reset crosshair + m_pWeapon = NULL;// reset last weapon +} + +int CHudAmmo :: VidInit( void ) +{ + // Load sprites for buckets (top row of weapon menu) + m_HUD_bucket0 = gHUD.GetSpriteIndex( "bucket1" ); + m_HUD_selection = gHUD.GetSpriteIndex( "selection" ); + + ghsprBuckets = gHUD.GetSprite(m_HUD_bucket0); + giBucketWidth = gHUD.GetSpriteRect(m_HUD_bucket0).right - gHUD.GetSpriteRect(m_HUD_bucket0).left; + giBucketHeight = gHUD.GetSpriteRect(m_HUD_bucket0).bottom - gHUD.GetSpriteRect(m_HUD_bucket0).top; + + gHR.iHistoryGap = max( gHR.iHistoryGap, gHUD.GetSpriteRect(m_HUD_bucket0).bottom - gHUD.GetSpriteRect(m_HUD_bucket0).top); + + // If we've already loaded weapons, let's get new sprites + gWR.LoadAllWeaponSprites(); + + giABWidth = 20; + giABHeight = 4; + + return 1; +} + +// +// Think: +// Used for selection of weapon menu item. +// +void CHudAmmo :: Think( void ) +{ + if( gHUD.m_fPlayerDead ) + return; + + if( gHUD.m_iWeaponBits != gWR.iOldWeaponBits ) + { + gWR.iOldWeaponBits = gHUD.m_iWeaponBits; + + for (int i = MAX_WEAPONS-1; i > 0; i-- ) + { + WEAPON *p = gWR.GetWeapon(i); + + if( p ) + { + if( gHUD.m_iWeaponBits & ( 1 << p->iId )) + gWR.PickupWeapon( p ); + else gWR.DropWeapon( p ); + } + } + } + + if( !gpActiveSel ) return; + + // has the player selected one? + if( gHUD.m_iKeyBits & IN_ATTACK ) + { + if( gpActiveSel != (WEAPON *)1 ) + { + SERVER_COMMAND( gpActiveSel->szName ); + g_weaponselect = gpActiveSel->iId; + } + + gpLastSel = gpActiveSel; + gpActiveSel = NULL; + gHUD.m_iKeyBits &= ~IN_ATTACK; + + CL_PlaySound( "common/wpn_select.wav", 1.0f ); + } + +} + +// +// Helper function to return a Ammo pointer from id +// +HSPRITE* WeaponsResource :: GetAmmoPicFromWeapon( int iAmmoId, wrect_t& rect ) +{ + for ( int i = 0; i < MAX_WEAPONS; i++ ) + { + if ( rgWeapons[i].iAmmoType == iAmmoId ) + { + rect = rgWeapons[i].rcAmmo; + return &rgWeapons[i].hAmmo; + } + else if ( rgWeapons[i].iAmmo2Type == iAmmoId ) + { + rect = rgWeapons[i].rcAmmo2; + return &rgWeapons[i].hAmmo2; + } + } + + return NULL; +} + +// Menu Selection Code +void WeaponsResource :: SelectSlot( int iSlot, int fAdvance, int iDirection ) +{ + if( gHUD.m_Menu.m_fMenuDisplayed && (fAdvance == FALSE) && (iDirection == 1) ) + { + // menu is overriding slot use commands + gHUD.m_Menu.SelectMenuItem( iSlot + 1 ); // slots are one off the key numbers + return; + } + + if( iSlot > MAX_WEAPON_SLOTS ) + return; + + if( gHUD.m_fPlayerDead || gHUD.m_iHideHUDDisplay & ( HIDEHUD_WEAPONS|HIDEHUD_ALL )) + return; + + if( !(gHUD.m_iHideHUDDisplay & ITEM_SUIT )) + return; + + WEAPON *p = NULL; + bool fastSwitch = CVAR_GET_FLOAT( "hud_fastswitch" ) != 0; + + if((gpActiveSel == NULL) || (gpActiveSel == (WEAPON *)1) || (iSlot != gpActiveSel->iSlot)) + { + CL_PlaySound( "common/wpn_hudon.wav", 1.0f ); + p = GetFirstPos( iSlot ); + + if( p && fastSwitch ) // check for fast weapon switch mode + { + // if fast weapon switch is on, then weapons can be selected in a single keypress + // but only if there is only one item in the bucket + WEAPON *p2 = GetNextActivePos( p->iSlot, p->iSlotPos ); + if( !p2 ) + { // only one active item in bucket, so change directly to weapon + SERVER_COMMAND( p->szName ); + g_weaponselect = p->iId; + return; + } + } + } + else + { + CL_PlaySound( "common/wpn_moveselect.wav", 1.0f ); + if( gpActiveSel ) + p = GetNextActivePos( gpActiveSel->iSlot, gpActiveSel->iSlotPos ); + if( !p ) p = GetFirstPos( iSlot ); + } + + + if( !p ) // no selection found + { + // just display the weapon list, unless fastswitch is on just ignore it + if( !fastSwitch ) + gpActiveSel = (WEAPON *)1; + else gpActiveSel = NULL; + } + else gpActiveSel = p; +} + +//------------------------------------------------------------------------ +// Message Handlers +//------------------------------------------------------------------------ + +// +// AmmoX -- Update the count of a known type of ammo +// +int CHudAmmo::MsgFunc_AmmoX(const char *pszName, int iSize, void *pbuf) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int iIndex = READ_BYTE(); + int iCount = READ_BYTE(); + + gWR.SetAmmo( iIndex, abs(iCount) ); + + return 1; +} + +int CHudAmmo::MsgFunc_AmmoPickup( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + int iIndex = READ_BYTE(); + int iCount = READ_BYTE(); + + // Add ammo to the history + gHR.AddToHistory( HISTSLOT_AMMO, iIndex, abs(iCount) ); + + END_READ(); + return 1; +} + +int CHudAmmo::MsgFunc_WeapPickup( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + int iIndex = READ_BYTE(); + + // Add the weapon to the history + gHR.AddToHistory( HISTSLOT_WEAP, iIndex ); + + END_READ(); + return 1; +} + +int CHudAmmo::MsgFunc_ItemPickup( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + const char *szName = READ_STRING(); + + // Add the weapon to the history + gHR.AddToHistory( HISTSLOT_ITEM, szName ); + + END_READ(); + return 1; +} + + +int CHudAmmo::MsgFunc_HideWeapon( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + gHUD.m_iHideHUDDisplay = READ_BYTE(); + + if((m_pWeapon == NULL) || (gHUD.m_iHideHUDDisplay & ( HIDEHUD_WEAPONS | HIDEHUD_ALL ))) + { + static wrect_t nullrc; + gpActiveSel = NULL; + SetCrosshair( 0, nullrc, 0, 0, 0 ); + } + else + { + SetCrosshair( m_pWeapon->hCrosshair, m_pWeapon->rcCrosshair, 255, 255, 255 ); + } + + END_READ(); + return 1; +} + +// +// CurWeapon: Update hud state with the current weapon and clip count. Ammo +// counts are updated with AmmoX. Server assures that the Weapon ammo type +// numbers match a real ammo type. +// +int CHudAmmo::MsgFunc_CurWeapon( const char *pszName, int iSize, void *pbuf ) +{ + static wrect_t nullrc; + int fOnTarget = FALSE; + + BEGIN_READ( pszName, iSize, pbuf ); + + int iState = READ_BYTE(); + int iId = READ_CHAR(); + int iClip = READ_CHAR(); + + // detect if we're also on target + if( iState > 1 ) + { + fOnTarget = TRUE; + } + + if( iId < 1 ) + { + SetCrosshair(0, nullrc, 0, 0, 0); + m_pWeapon = NULL; + return 0; + } + + // Is player dead??? + if((iId == -1) && (iClip == -1)) + { + gHUD.m_fPlayerDead = TRUE; + gpActiveSel = NULL; + return 1; + } + + gHUD.m_fPlayerDead = FALSE; + WEAPON *pWeapon = gWR.GetWeapon( iId ); + + if( !pWeapon ) + return 0; + + if( iClip < -1 ) + pWeapon->iClip = abs( iClip ); + else pWeapon->iClip = iClip; + + + if( iState == 0 ) // we're not the current weapon, so update no more + return 1; + + m_pWeapon = pWeapon; + + if( gHUD.m_iFOV >= 90 ) + { + // normal crosshairs + if( fOnTarget && m_pWeapon->hAutoaim ) + SetCrosshair(m_pWeapon->hAutoaim, m_pWeapon->rcAutoaim, 255, 255, 255 ); + else SetCrosshair( m_pWeapon->hCrosshair, m_pWeapon->rcCrosshair, 255, 255, 255 ); + } + else + { // zoomed crosshairs + if( fOnTarget && m_pWeapon->hZoomedAutoaim ) + SetCrosshair(m_pWeapon->hZoomedAutoaim, m_pWeapon->rcZoomedAutoaim, 255, 255, 255 ); + else SetCrosshair( m_pWeapon->hZoomedCrosshair, m_pWeapon->rcZoomedCrosshair, 255, 255, 255 ); + + } + + m_fFade = 200.0f; //!!! + m_iFlags |= HUD_ACTIVE; + + END_READ(); + return 1; +} + +// +// WeaponList -- Tells the hud about a new weapon type. +// +int CHudAmmo::MsgFunc_WeaponList( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + WEAPON Weapon; + + strcpy( Weapon.szName, READ_STRING() ); + Weapon.iAmmoType = (int)READ_CHAR(); + + Weapon.iMax1 = READ_BYTE(); + if( Weapon.iMax1 == 255 ) + Weapon.iMax1 = -1; + + Weapon.iAmmo2Type = READ_CHAR(); + Weapon.iMax2 = READ_BYTE(); + if( Weapon.iMax2 == 255 ) + Weapon.iMax2 = -1; + + Weapon.iSlot = READ_CHAR(); + Weapon.iSlotPos = READ_CHAR(); + Weapon.iId = READ_CHAR(); + Weapon.iFlags = READ_BYTE(); + Weapon.iClip = 0; + + gWR.AddWeapon( &Weapon ); + + END_READ(); + return 1; + +} + +//------------------------------------------------------------------------ +// Command Handlers +//------------------------------------------------------------------------ +// Slot button pressed +void CHudAmmo::SlotInput( int iSlot ) +{ + gWR.SelectSlot( iSlot, FALSE, 1 ); +} + +void CHudAmmo::UserCmd_Slot1( void ) +{ + SlotInput( 0 ); +} + +void CHudAmmo::UserCmd_Slot2( void ) +{ + SlotInput( 1 ); +} + +void CHudAmmo::UserCmd_Slot3( void ) +{ + SlotInput( 2 ); +} + +void CHudAmmo::UserCmd_Slot4( void ) +{ + SlotInput( 3 ); +} + +void CHudAmmo::UserCmd_Slot5( void ) +{ + SlotInput( 4 ); +} + +void CHudAmmo::UserCmd_Slot6( void ) +{ + SlotInput( 5 ); +} + +void CHudAmmo::UserCmd_Slot7( void ) +{ + SlotInput( 6 ); +} + +void CHudAmmo::UserCmd_Slot8( void ) +{ + SlotInput( 7 ); +} + +void CHudAmmo::UserCmd_Slot9( void ) +{ + SlotInput( 8 ); +} + +void CHudAmmo::UserCmd_Slot10( void ) +{ + SlotInput( 9 ); +} + +void CHudAmmo::UserCmd_Close( void ) +{ + if( gpActiveSel ) + { + gpLastSel = gpActiveSel; + gpActiveSel = NULL; + CL_PlaySound( "common/wpn_hudoff.wav", 1.0f ); + } + else CLIENT_COMMAND( "escape" ); +} + + +// Selects the next item in the weapon menu +void CHudAmmo::UserCmd_NextWeapon( void ) +{ + if( gHUD.m_fPlayerDead || (gHUD.m_iHideHUDDisplay & (HIDEHUD_WEAPONS|HIDEHUD_ALL))) + return; + + if( !gpActiveSel || gpActiveSel == (WEAPON*)1 ) + gpActiveSel = m_pWeapon; + + int pos = 0; + int slot = 0; + if( gpActiveSel ) + { + pos = gpActiveSel->iSlotPos + 1; + slot = gpActiveSel->iSlot; + } + + for( int loop = 0; loop <= 1; loop++ ) + { + for( ; slot < MAX_WEAPON_SLOTS; slot++ ) + { + for( ; pos < MAX_WEAPON_POSITIONS; pos++ ) + { + WEAPON *wsp = gWR.GetWeaponSlot( slot, pos ); + + if( wsp && gWR.HasAmmo( wsp )) + { + gpActiveSel = wsp; + return; + } + } + pos = 0; + } + slot = 0; // start looking from the first slot again + } + gpActiveSel = NULL; +} + +// Selects the previous item in the menu +void CHudAmmo :: UserCmd_PrevWeapon( void ) +{ + if( gHUD.m_fPlayerDead || (gHUD.m_iHideHUDDisplay & (HIDEHUD_WEAPONS | HIDEHUD_ALL))) + return; + + if( !gpActiveSel || gpActiveSel == (WEAPON*)1 ) + gpActiveSel = m_pWeapon; + + int pos = MAX_WEAPON_POSITIONS-1; + int slot = MAX_WEAPON_SLOTS-1; + if( gpActiveSel ) + { + pos = gpActiveSel->iSlotPos - 1; + slot = gpActiveSel->iSlot; + } + + for( int loop = 0; loop <= 1; loop++ ) + { + for( ; slot >= 0; slot-- ) + { + for( ; pos >= 0; pos-- ) + { + WEAPON *wsp = gWR.GetWeaponSlot( slot, pos ); + + if( wsp && gWR.HasAmmo( wsp )) + { + gpActiveSel = wsp; + return; + } + } + pos = MAX_WEAPON_POSITIONS-1; + } + slot = MAX_WEAPON_SLOTS-1; + } + gpActiveSel = NULL; +} + + + +//------------------------------------------------------------------------- +// Drawing code +//------------------------------------------------------------------------- + +int CHudAmmo::Draw( float flTime ) +{ + int a, x, y, r, g, b; + int AmmoWidth; + + if(!(gHUD.m_iHideHUDDisplay & ITEM_SUIT )) + return 1; + + if( (gHUD.m_iHideHUDDisplay & ( HIDEHUD_WEAPONS | HIDEHUD_ALL ))) + return 1; + + // Draw Weapon Menu + DrawWList( flTime ); + + // Draw ammo pickup history + gHR.DrawAmmoHistory( flTime ); + + if(!( m_iFlags & HUD_ACTIVE )) + return 0; + + if( !m_pWeapon ) + { + return 0; + } + + WEAPON *pw = m_pWeapon; // shorthand + + // SPR_Draw Ammo + if((pw->iAmmoType < 0) && (pw->iAmmo2Type < 0)) + return 0; + + + int iFlags = DHN_DRAWZERO; // draw 0 values + + AmmoWidth = gHUD.GetSpriteRect(gHUD.m_HUD_number_0).right - gHUD.GetSpriteRect(gHUD.m_HUD_number_0).left; + + a = (int) max( MIN_ALPHA, m_fFade ); + + if( m_fFade > 0 ) + m_fFade -= (gHUD.m_flTimeDelta * 20); + + UnpackRGB( r,g,b, gHUD.m_iHUDColor ); + + ScaleColors(r, g, b, a ); + + // Does this weapon have a clip? + y = SCREEN_HEIGHT - gHUD.m_iFontHeight - gHUD.m_iFontHeight / 2; + + // Does weapon have any ammo at all? + if( m_pWeapon->iAmmoType > 0 ) + { + int iIconWidth = m_pWeapon->rcAmmo.right - m_pWeapon->rcAmmo.left; + + if( pw->iClip >= 0 ) + { + // room for the number and the '|' and the current ammo + + x = SCREEN_WIDTH - (8 * AmmoWidth) - iIconWidth; + x = gHUD.DrawHudNumber(x, y, iFlags|DHN_3DIGITS, pw->iClip, r, g, b ); + + wrect_t rc; + rc.top = 0; + rc.left = 0; + rc.right = AmmoWidth; + rc.bottom = 100; + + int iBarWidth = AmmoWidth/10; + + x += AmmoWidth/2; + + UnpackRGB(r,g,b, gHUD.m_iHUDColor); + + // draw the | bar + FillRGBA( x, y, iBarWidth, gHUD.m_iFontHeight, r, g, b, a ); + + x += iBarWidth + AmmoWidth / 2; + + // GL Seems to need this + ScaleColors( r, g, b, a ); + x = gHUD.DrawHudNumber(x, y, iFlags | DHN_3DIGITS, gWR.CountAmmo(pw->iAmmoType), r, g, b); + } + else + { + // SPR_Draw a bullets only line + x = SCREEN_WIDTH - 4 * AmmoWidth - iIconWidth; + x = gHUD.DrawHudNumber(x, y, iFlags | DHN_3DIGITS, gWR.CountAmmo(pw->iAmmoType), r, g, b); + } + + // Draw the ammo Icon + int iOffset = (m_pWeapon->rcAmmo.bottom - m_pWeapon->rcAmmo.top) / 8; + SPR_Set( m_pWeapon->hAmmo, r, g, b ); + SPR_DrawAdditive( 0, x, y - iOffset, &m_pWeapon->rcAmmo ); + } + + // Does weapon have secondary ammo? + if( pw->iAmmo2Type > 0 ) + { + int iIconWidth = m_pWeapon->rcAmmo2.right - m_pWeapon->rcAmmo2.left; + + // Do we have secondary ammo? + if((pw->iAmmo2Type != 0) && (gWR.CountAmmo(pw->iAmmo2Type) > 0)) + { + y -= gHUD.m_iFontHeight + gHUD.m_iFontHeight/4; + x = SCREEN_WIDTH - 4 * AmmoWidth - iIconWidth; + x = gHUD.DrawHudNumber(x, y, iFlags|DHN_3DIGITS, gWR.CountAmmo(pw->iAmmo2Type), r, g, b); + + // Draw the ammo Icon + SPR_Set(m_pWeapon->hAmmo2, r, g, b); + int iOffset = (m_pWeapon->rcAmmo2.bottom - m_pWeapon->rcAmmo2.top)/8; + SPR_DrawAdditive(0, x, y - iOffset, &m_pWeapon->rcAmmo2); + } + } + return 1; +} + + +// +// Draws the ammo bar on the hud +// +int DrawBar( int x, int y, int width, int height, float f ) +{ + int r, g, b; + + if( f < 0 ) f = 0; + if( f > 1 ) f = 1; + + if( f ) + { + int w = f * width; + + // Always show at least one pixel if we have ammo. + if( w <= 0 ) w = 1; + UnpackRGB( r, g, b, RGB_GREENISH ); + FillRGBA( x, y, w, height, r, g, b, 255 ); + x += w; + width -= w; + } + + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + + FillRGBA( x, y, width, height, r, g, b, 128 ); + return (x + width); +} + +void DrawAmmoBar( WEAPON *p, int x, int y, int width, int height ) +{ + if( !p ) return; + + if( p->iAmmoType != -1 ) + { + if( !gWR.CountAmmo( p->iAmmoType )) + return; + + float f = (float)gWR.CountAmmo(p->iAmmoType)/(float)p->iMax1; + + x = DrawBar(x, y, width, height, f); + + // Do we have secondary ammo too? + if( p->iAmmo2Type != -1 ) + { + f = (float)gWR.CountAmmo(p->iAmmo2Type) / (float)p->iMax2; + + x += 5; //!!! + DrawBar( x, y, width, height, f ); + } + } +} + +// +// Draw Weapon Menu +// +int CHudAmmo::DrawWList( float flTime ) +{ + int r,g,b,x,y,a,i; + + if( !gpActiveSel ) + return 0; + + int iActiveSlot; + + if( gpActiveSel == (WEAPON *)1 ) + iActiveSlot = -1; // current slot has no weapons + else iActiveSlot = gpActiveSel->iSlot; + + x = 10; //!!! + y = 10; //!!! + + + // Ensure that there are available choices in the active slot + if( iActiveSlot > 0 ) + { + if( !gWR.GetFirstPos( iActiveSlot ) ) + { + gpActiveSel = (WEAPON *)1; + iActiveSlot = -1; + } + } + + // Draw top line + for( i = 0; i < MAX_WEAPON_SLOTS; i++ ) + { + int iWidth; + + UnpackRGB( r,g,b, gHUD.m_iHUDColor ); + + if( iActiveSlot == i ) + a = 255; + else a = 192; // not used ??? + + ScaleColors( r, g, b, 255 ); + SPR_Set( gHUD.GetSprite( m_HUD_bucket0 + i ), r, g, b ); + + // make active slot wide enough to accomodate gun pictures + if( i == iActiveSlot ) + { + WEAPON *p = gWR.GetFirstPos(iActiveSlot); + if ( p ) + iWidth = p->rcActive.right - p->rcActive.left; + else iWidth = giBucketWidth; + } + else iWidth = giBucketWidth; + + SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect( m_HUD_bucket0 + i )); + + x += iWidth + 5; + } + + + a = 128; //!!! + x = 10; + + // Draw all of the buckets + for( i = 0; i < MAX_WEAPON_SLOTS; i++ ) + { + y = giBucketHeight + 10; + + // If this is the active slot, draw the bigger pictures, + // otherwise just draw boxes + if( i == iActiveSlot ) + { + WEAPON *p = gWR.GetFirstPos( i ); + int iWidth = giBucketWidth; + if( p ) iWidth = p->rcActive.right - p->rcActive.left; + + for( int iPos = 0; iPos < MAX_WEAPON_POSITIONS; iPos++ ) + { + p = gWR.GetWeaponSlot( i, iPos ); + + if( !p || !p->iId ) + continue; + + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + + // if active, then we must have ammo. + if( gpActiveSel == p ) + { + SPR_Set( p->hActive, r, g, b ); + SPR_DrawAdditive( 0, x, y, &p->rcActive ); + + SPR_Set( gHUD.GetSprite(m_HUD_selection), r, g, b ); + SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect( m_HUD_selection )); + } + else + { + // Draw Weapon if Red if no ammo + + if( gWR.HasAmmo( p )) + ScaleColors( r, g, b, 192 ); + else + { + UnpackRGB( r, g, b, RGB_REDISH ); + ScaleColors( r, g, b, 128 ); + } + + SPR_Set( p->hInactive, r, g, b ); + SPR_DrawAdditive( 0, x, y, &p->rcInactive ); + } + + // Draw Ammo Bar + DrawAmmoBar( p, x + giABWidth/2, y, giABWidth, giABHeight ); + y += p->rcActive.bottom - p->rcActive.top + 5; + } + + x += iWidth + 5; + } + else + { + // Draw Row of weapons. + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + + for( int iPos = 0; iPos < MAX_WEAPON_POSITIONS; iPos++ ) + { + WEAPON *p = gWR.GetWeaponSlot( i, iPos ); + + if( !p || !p->iId ) + continue; + + if( gWR.HasAmmo(p) ) + { + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + a = 128; + } + else + { + UnpackRGB( r, g, b, RGB_REDISH ); + a = 96; + } + + FillRGBA( x, y, giBucketWidth, giBucketHeight, r, g, b, a ); + y += giBucketHeight + 5; + } + x += giBucketWidth + 5; + } + } + + return 1; +} + +int CHudAmmoSecondary :: Init( void ) +{ + HOOK_MESSAGE( SecAmmoVal ); + HOOK_MESSAGE( SecAmmoIcon ); + + gHUD.AddHudElem( this ); + m_HUD_ammoicon = 0; + + for( int i = 0; i < MAX_SEC_AMMO_VALUES; i++ ) + m_iAmmoAmounts[i] = -1; // -1 means don't draw this value + Reset(); + + return 1; +} + +void CHudAmmoSecondary :: Reset( void ) +{ + m_fFade = 0; +} + +int CHudAmmoSecondary :: VidInit( void ) +{ + return 1; +} + +int CHudAmmoSecondary :: Draw( float flTime ) +{ + if(( gHUD.m_iHideHUDDisplay & ( HIDEHUD_WEAPONS | HIDEHUD_ALL ))) + return 1; + + // draw secondary ammo icons above normal ammo readout + int a, x, y, r, g, b, AmmoWidth; + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + a = (int) max( MIN_ALPHA, m_fFade ); + if( m_fFade > 0 ) + m_fFade -= (gHUD.m_flTimeDelta * 20); // slowly lower alpha to fade out icons + ScaleColors( r, g, b, a ); + + AmmoWidth = gHUD.GetSpriteRect(gHUD.m_HUD_number_0).right - gHUD.GetSpriteRect(gHUD.m_HUD_number_0).left; + + y = SCREEN_HEIGHT - (gHUD.m_iFontHeight*4); // this is one font height higher than the weapon ammo values + x = SCREEN_WIDTH - AmmoWidth; + + if( m_HUD_ammoicon ) + { + // Draw the ammo icon + x -= (gHUD.GetSpriteRect(m_HUD_ammoicon).right - gHUD.GetSpriteRect(m_HUD_ammoicon).left); + y -= (gHUD.GetSpriteRect(m_HUD_ammoicon).top - gHUD.GetSpriteRect(m_HUD_ammoicon).bottom); + + SPR_Set( gHUD.GetSprite(m_HUD_ammoicon), r, g, b ); + SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect(m_HUD_ammoicon) ); + } + else + { + // move the cursor by the '0' char instead, since we don't have an icon to work with + x -= AmmoWidth; + y -= (gHUD.GetSpriteRect(gHUD.m_HUD_number_0).top - gHUD.GetSpriteRect(gHUD.m_HUD_number_0).bottom); + } + + // draw the ammo counts, in reverse order, from right to left + for( int i = MAX_SEC_AMMO_VALUES-1; i >= 0; i-- ) + { + if( m_iAmmoAmounts[i] < 0 ) + continue; // negative ammo amounts imply that they shouldn't be drawn + + // half a char gap between the ammo number and the previous pic + x -= (AmmoWidth / 2); + + // draw the number, right-aligned + x -= (gHUD.GetNumWidth( m_iAmmoAmounts[i], DHN_DRAWZERO ) * AmmoWidth); + gHUD.DrawHudNumber( x, y, DHN_DRAWZERO, m_iAmmoAmounts[i], r, g, b ); + + if ( i != 0 ) + { + // draw the divider bar + x -= (AmmoWidth / 2); + FillRGBA(x, y, (AmmoWidth/10), gHUD.m_iFontHeight, r, g, b, a); + } + } + return 1; +} + +// Message handler for Secondary Ammo Value +// accepts one value: +// string: sprite name +int CHudAmmoSecondary :: MsgFunc_SecAmmoIcon( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_HUD_ammoicon = gHUD.GetSpriteIndex( READ_STRING() ); + + END_READ(); + return 1; +} + +// Message handler for Secondary Ammo Icon +// Sets an ammo value +// takes two values: +// byte: ammo index +// byte: ammo value +int CHudAmmoSecondary :: MsgFunc_SecAmmoVal( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int index = READ_BYTE(); + if( index < 0 || index >= MAX_SEC_AMMO_VALUES ) + return 1; + + m_iAmmoAmounts[index] = READ_BYTE(); + m_iFlags |= HUD_ACTIVE; + + // check to see if there is anything left to draw + int count = 0; + for( int i = 0; i < MAX_SEC_AMMO_VALUES; i++ ) + { + count += max( 0, m_iAmmoAmounts[i] ); + } + + if( count == 0 ) + { + // the ammo fields are all empty, so turn off this hud area + m_iFlags &= ~HUD_ACTIVE; + return 1; + } + + // make the icons light up + m_fFade = 200.0f; + END_READ(); + + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_ammo.h b/client/hud/hud_ammo.h new file mode 100644 index 00000000..b064ddb9 --- /dev/null +++ b/client/hud/hud_ammo.h @@ -0,0 +1,59 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ + +#ifndef __AMMO_H__ +#define __AMMO_H__ + +#define MAX_WEAPON_NAME 128 +#define MAX_WEAPON_SLOTS 5 // hud item selection slots +#define WEAPON_FLAGS_SELECTONEMPTY 1 +#define WEAPON_IS_ONTARGET 0x40 + +struct WEAPON +{ + char szName[MAX_WEAPON_NAME]; + int iAmmoType; + int iAmmo2Type; + int iMax1; + int iMax2; + int iSlot; + int iSlotPos; + int iFlags; + int iId; + int iClip; + int iCount; // # of itesm in plist + + HSPRITE hActive; + wrect_t rcActive; + HSPRITE hInactive; + wrect_t rcInactive; + HSPRITE hAmmo; + wrect_t rcAmmo; + HSPRITE hAmmo2; + wrect_t rcAmmo2; + HSPRITE hCrosshair; + wrect_t rcCrosshair; + HSPRITE hAutoaim; + wrect_t rcAutoaim; + HSPRITE hZoomedCrosshair; + wrect_t rcZoomedCrosshair; + HSPRITE hZoomedAutoaim; + wrect_t rcZoomedAutoaim; +}; + +typedef int AMMO; + + +#endif \ No newline at end of file diff --git a/client/hud/hud_ammohistory.cpp b/client/hud/hud_ammohistory.cpp new file mode 100644 index 00000000..46d3db87 --- /dev/null +++ b/client/hud/hud_ammohistory.cpp @@ -0,0 +1,186 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// ammohistory.cpp +// + + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" +#include "hud_ammohistory.h" + +HistoryResource gHR; + +#define AMMO_PICKUP_GAP (gHR.iHistoryGap+5) +#define AMMO_PICKUP_PICK_HEIGHT (32 + (gHR.iHistoryGap * 2)) +#define AMMO_PICKUP_HEIGHT_MAX (SCREEN_HEIGHT - 100) + +#define MAX_ITEM_NAME 32 +int HISTORY_DRAW_TIME = 5; + +// keep a list of items +struct ITEM_INFO +{ + char szName[MAX_ITEM_NAME]; + HSPRITE spr; + wrect_t rect; +}; + +void HistoryResource :: AddToHistory( int iType, int iId, int iCount ) +{ + if ( iType == HISTSLOT_AMMO && !iCount ) + return; // no amount, so don't add + + if ( (((AMMO_PICKUP_GAP * iCurrentHistorySlot) + AMMO_PICKUP_PICK_HEIGHT) > AMMO_PICKUP_HEIGHT_MAX) || (iCurrentHistorySlot >= MAX_HISTORY) ) + { // the pic would have to be drawn too high + // so start from the bottom + iCurrentHistorySlot = 0; + } + + HIST_ITEM *freeslot = &rgAmmoHistory[iCurrentHistorySlot++]; // default to just writing to the first slot + HISTORY_DRAW_TIME = CVAR_GET_FLOAT( "hud_drawhistory_time" ); + + freeslot->type = iType; + freeslot->iId = iId; + freeslot->iCount = iCount; + freeslot->DisplayTime = gHUD.m_flTime + HISTORY_DRAW_TIME; +} + +void HistoryResource :: AddToHistory( int iType, const char *szName, int iCount ) +{ + if ( iType != HISTSLOT_ITEM ) + return; + + if ( (((AMMO_PICKUP_GAP * iCurrentHistorySlot) + AMMO_PICKUP_PICK_HEIGHT) > AMMO_PICKUP_HEIGHT_MAX) || (iCurrentHistorySlot >= MAX_HISTORY) ) + { // the pic would have to be drawn too high + // so start from the bottom + iCurrentHistorySlot = 0; + } + + HIST_ITEM *freeslot = &rgAmmoHistory[iCurrentHistorySlot++]; // default to just writing to the first slot + + // I am really unhappy with all the code in this file + + int i = gHUD.GetSpriteIndex( szName ); + if ( i == -1 ) + return; // unknown sprite name, don't add it to history + + freeslot->iId = i; + freeslot->type = iType; + freeslot->iCount = iCount; + + HISTORY_DRAW_TIME = CVAR_GET_FLOAT( "hud_drawhistory_time" ); + freeslot->DisplayTime = gHUD.m_flTime + HISTORY_DRAW_TIME; +} + + +void HistoryResource :: CheckClearHistory( void ) +{ + for ( int i = 0; i < MAX_HISTORY; i++ ) + { + if ( rgAmmoHistory[i].type ) + return; + } + + iCurrentHistorySlot = 0; +} + +// +// Draw Ammo pickup history +// +int HistoryResource :: DrawAmmoHistory( float flTime ) +{ + for ( int i = 0; i < MAX_HISTORY; i++ ) + { + if ( rgAmmoHistory[i].type ) + { + rgAmmoHistory[i].DisplayTime = min( rgAmmoHistory[i].DisplayTime, gHUD.m_flTime + HISTORY_DRAW_TIME ); + + if ( rgAmmoHistory[i].DisplayTime <= flTime ) + { // pic drawing time has expired + memset( &rgAmmoHistory[i], 0, sizeof(HIST_ITEM) ); + CheckClearHistory(); + } + else if ( rgAmmoHistory[i].type == HISTSLOT_AMMO ) + { + wrect_t rcPic; + HSPRITE *spr = gWR.GetAmmoPicFromWeapon( rgAmmoHistory[i].iId, rcPic ); + + int r, g, b; + UnpackRGB(r,g,b, gHUD.m_iHUDColor); + float scale = (rgAmmoHistory[i].DisplayTime - flTime) * 80; + ScaleColors(r, g, b, min(scale, 255) ); + + // Draw the pic + int ypos = SCREEN_HEIGHT - (AMMO_PICKUP_PICK_HEIGHT + (AMMO_PICKUP_GAP * i)); + int xpos = SCREEN_WIDTH - 24; + if ( spr && *spr ) // weapon isn't loaded yet so just don't draw the pic + { // the dll has to make sure it has sent info the weapons you need + SPR_Set( *spr, r, g, b ); + SPR_DrawAdditive( 0, xpos, ypos, &rcPic ); + } + + // Draw the number + gHUD.DrawHudNumberString( xpos - 10, ypos, xpos - 100, rgAmmoHistory[i].iCount, r, g, b ); + } + else if ( rgAmmoHistory[i].type == HISTSLOT_WEAP ) + { + WEAPON *weap = gWR.GetWeapon( rgAmmoHistory[i].iId ); + + if ( !weap ) + return 1; // we don't know about the weapon yet, so don't draw anything + + int r, g, b; + UnpackRGB(r,g,b, gHUD.m_iHUDColor); + + if ( !gWR.HasAmmo( weap ) ) + UnpackRGB(r,g,b, RGB_REDISH); // if the weapon doesn't have ammo, display it as red + + float scale = (rgAmmoHistory[i].DisplayTime - flTime) * 80; + ScaleColors(r, g, b, min(scale, 255) ); + + int ypos = SCREEN_HEIGHT - (AMMO_PICKUP_PICK_HEIGHT + (AMMO_PICKUP_GAP * i)); + int xpos = SCREEN_WIDTH - (weap->rcInactive.right - weap->rcInactive.left); + SPR_Set( weap->hInactive, r, g, b ); + SPR_DrawAdditive( 0, xpos, ypos, &weap->rcInactive ); + } + else if ( rgAmmoHistory[i].type == HISTSLOT_ITEM ) + { + int r, g, b; + + if ( !rgAmmoHistory[i].iId ) + continue; // sprite not loaded + + wrect_t rect = gHUD.GetSpriteRect( rgAmmoHistory[i].iId ); + + UnpackRGB(r,g,b, gHUD.m_iHUDColor); + float scale = (rgAmmoHistory[i].DisplayTime - flTime) * 80; + ScaleColors(r, g, b, min(scale, 255) ); + + int ypos = SCREEN_HEIGHT - (AMMO_PICKUP_PICK_HEIGHT + (AMMO_PICKUP_GAP * i)); + int xpos = SCREEN_WIDTH - (rect.right - rect.left) - 10; + + SPR_Set( gHUD.GetSprite( rgAmmoHistory[i].iId ), r, g, b ); + SPR_DrawAdditive( 0, xpos, ypos, &rect ); + } + } + } + + + return 1; +} + + diff --git a/client/hud/hud_ammohistory.h b/client/hud/hud_ammohistory.h new file mode 100644 index 00000000..b74103c1 --- /dev/null +++ b/client/hud/hud_ammohistory.h @@ -0,0 +1,134 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// ammohistory.h +// + +// this is the max number of items in each bucket +#define MAX_WEAPON_POSITIONS MAX_WEAPON_SLOTS + +class WeaponsResource +{ +private: + // Information about weapons & ammo + WEAPON rgWeapons[MAX_WEAPONS]; // Weapons Array + + // counts of weapons * ammo + // The slots currently in use by weapons. The value is a pointer to the weapon; + // if it's NULL, no weapon is there + WEAPON* rgSlots[MAX_WEAPON_SLOTS+1][MAX_WEAPON_POSITIONS+1]; + int riAmmo[MAX_AMMO_SLOTS]; // count of each ammo type + +public: + void Init( void ) + { + memset( rgWeapons, 0, sizeof rgWeapons ); + Reset(); + } + + void Reset( void ) + { + iOldWeaponBits = 0; + memset( rgSlots, 0, sizeof rgSlots ); + memset( riAmmo, 0, sizeof riAmmo ); + } + +///// WEAPON ///// + int iOldWeaponBits; + + WEAPON *GetWeapon( int iId ) { return &rgWeapons[iId]; } + void AddWeapon( WEAPON *wp ) + { + rgWeapons[ wp->iId ] = *wp; + LoadWeaponSprites( &rgWeapons[ wp->iId ] ); + } + + void PickupWeapon( WEAPON *wp ) + { + rgSlots[ wp->iSlot ][ wp->iSlotPos ] = wp; + } + + void DropWeapon( WEAPON *wp ) + { + rgSlots[ wp->iSlot ][ wp->iSlotPos ] = NULL; + } + + void DropAllWeapons( void ) + { + for ( int i = 0; i < MAX_WEAPONS; i++ ) + { + if ( rgWeapons[i].iId ) + DropWeapon( &rgWeapons[i] ); + } + } + + WEAPON* GetWeaponSlot( int slot, int pos ) { return rgSlots[slot][pos]; } + + void LoadWeaponSprite( WEAPON *p, const char *type ); + void LoadWeaponSprites( WEAPON* wp ); + void LoadAllWeaponSprites( void ); + WEAPON* GetFirstPos( int iSlot ); + void SelectSlot( int iSlot, int fAdvance, int iDirection ); + WEAPON* GetNextActivePos( int iSlot, int iSlotPos ); + + int HasAmmo( WEAPON *p ); + + ///// AMMO ///// + AMMO GetAmmo( int iId ) { return iId; } + + void SetAmmo( int iId, int iCount ) { riAmmo[ iId ] = iCount; } + + int CountAmmo( int iId ); + + HSPRITE* GetAmmoPicFromWeapon( int iAmmoId, wrect_t& rect ); +}; + +#define MAX_HISTORY 12 +enum +{ + HISTSLOT_EMPTY, + HISTSLOT_AMMO, + HISTSLOT_WEAP, + HISTSLOT_ITEM, +}; + +class HistoryResource +{ +private: + struct HIST_ITEM + { + int type; + float DisplayTime; // the time at which this item should be removed from the history + int iCount; + int iId; + }; + + HIST_ITEM rgAmmoHistory[MAX_HISTORY]; +public: + void Init( void ) { Reset(); } + void Reset( void ) { memset( rgAmmoHistory, 0, sizeof rgAmmoHistory ); } + + int iHistoryGap; + int iCurrentHistorySlot; + + void AddToHistory( int iType, int iId, int iCount = 0 ); + void AddToHistory( int iType, const char *szName, int iCount = 0 ); + + void CheckClearHistory( void ); + int DrawAmmoHistory( float flTime ); +}; + +extern WeaponsResource gWR; +extern HistoryResource gHR; \ No newline at end of file diff --git a/client/hud/hud_battery.cpp b/client/hud/hud_battery.cpp new file mode 100644 index 00000000..b8f7fd54 --- /dev/null +++ b/client/hud/hud_battery.cpp @@ -0,0 +1,135 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// battery.cpp +// +// implementation of CHudBattery class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_Battery, Battery ) + +int CHudBattery :: Init( void ) +{ + m_iBat = 0; + m_fFade = 0; + m_iFlags = 0; + + HOOK_MESSAGE( Battery ); + + gHUD.AddHudElem( this ); + + return 1; +} + +int CHudBattery :: VidInit( void ) +{ + int HUD_suit_empty = gHUD.GetSpriteIndex( "suit_empty" ); + int HUD_suit_full = gHUD.GetSpriteIndex( "suit_full" ); + + m_hSprite1 = m_hSprite2 = 0; // delaying get sprite handles until we know the sprites are loaded + m_prc1 = &gHUD.GetSpriteRect( HUD_suit_empty ); + m_prc2 = &gHUD.GetSpriteRect( HUD_suit_full ); + m_iHeight = m_prc2->bottom - m_prc1->top; + m_fFade = 0; + return 1; +}; + +int CHudBattery:: MsgFunc_Battery( const char *pszName, int iSize, void *pbuf ) +{ + m_iFlags |= HUD_ACTIVE; + + + BEGIN_READ( pszName, iSize, pbuf ); + int x = READ_SHORT(); + + if( x != m_iBat ) + { + m_fFade = FADE_TIME; + m_iBat = x; + } + + END_READ(); + + return 1; +} + +int CHudBattery :: Draw( float flTime ) +{ + if( gHUD.m_iHideHUDDisplay & HIDEHUD_HEALTH ) + return 1; + + int r, g, b, x, y, a; + wrect_t rc; + + rc = *m_prc2; + + // battery can go from 0 to 100 so * 0.01 goes from 0 to 1 + rc.top += m_iHeight * ((float)(100-(min(100,m_iBat))) * 0.01); + + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + + if(!( gHUD.m_iHideHUDDisplay & ITEM_SUIT )) + return 1; + + // Has health changed? Flash the health # + if( m_fFade ) + { + if( m_fFade > FADE_TIME ) + m_fFade = FADE_TIME; + + m_fFade -= (gHUD.m_flTimeDelta * 20); + if( m_fFade <= 0 ) + { + a = 128; + m_fFade = 0; + } + + // Fade the health number back to dim + a = MIN_ALPHA + (m_fFade/FADE_TIME) * 128; + + } + else a = MIN_ALPHA; + + ScaleColors( r, g, b, a ); + + int iOffset = (m_prc1->bottom - m_prc1->top) / 6; + + y = SCREEN_HEIGHT - gHUD.m_iFontHeight - gHUD.m_iFontHeight / 2; + x = SCREEN_WIDTH / 5; + + // make sure we have the right sprite handles + if( !m_hSprite1 ) + m_hSprite1 = gHUD.GetSprite( gHUD.GetSpriteIndex( "suit_empty" )); + if( !m_hSprite2 ) + m_hSprite2 = gHUD.GetSprite( gHUD.GetSpriteIndex( "suit_full" )); + + SPR_Set( m_hSprite1, r, g, b ); + SPR_DrawAdditive( 0, x, y - iOffset, m_prc1 ); + + if( rc.bottom > rc.top ) + { + SPR_Set( m_hSprite2, r, g, b ); + SPR_DrawAdditive( 0, x, y - iOffset + (rc.top - m_prc2->top), &rc ); + } + + x += (m_prc1->right - m_prc1->left); + x = gHUD.DrawHudNumber( x, y, DHN_3DIGITS|DHN_DRAWZERO, m_iBat, r, g, b ); + + return 1; +} diff --git a/client/hud/hud_death.cpp b/client/hud/hud_death.cpp new file mode 100644 index 00000000..86d1e1e3 --- /dev/null +++ b/client/hud/hud_death.cpp @@ -0,0 +1,232 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// death notice +// +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_DeathNotice, DeathMsg ); + +struct DeathNoticeItem +{ + char szKiller[MAX_PLAYER_NAME_LENGTH*2]; + char szVictim[MAX_PLAYER_NAME_LENGTH*2]; + int iId; // the index number of the associated sprite + int iSuicide; + int iTeamKill; + int iNonPlayerKill; + float flDisplayTime; +}; + +#define MAX_DEATHNOTICES 4 +static int DEATHNOTICE_DISPLAY_TIME = 6; + +#define DEATHNOTICE_TOP 32 + +DeathNoticeItem rgDeathNoticeList[ MAX_DEATHNOTICES + 1 ]; + + +int CHudDeathNotice :: Init( void ) +{ + gHUD.AddHudElem( this ); + + HOOK_MESSAGE( DeathMsg ); + + CVAR_REGISTER( "hud_deathnotice_time", "6", 0, "time to show deathnotice" ); + + return 1; +} + + +void CHudDeathNotice :: InitHUDData( void ) +{ + memset( rgDeathNoticeList, 0, sizeof( rgDeathNoticeList )); +} + + +int CHudDeathNotice :: VidInit( void ) +{ + m_HUD_d_skull = gHUD.GetSpriteIndex( "d_skull" ); + + return 1; +} + +int CHudDeathNotice :: Draw( float flTime ) +{ + int x, y, r, g, b; + + for( int i = 0; i < MAX_DEATHNOTICES; i++ ) + { + if( rgDeathNoticeList[i].iId == 0 ) + break; // we've gone through them all + + if( rgDeathNoticeList[i].flDisplayTime < flTime ) + { + // display time has expired + // remove the current item from the list + memmove( &rgDeathNoticeList[i], &rgDeathNoticeList[i+1], sizeof(DeathNoticeItem) * (MAX_DEATHNOTICES - i)); + i--; // continue on the next item; stop the counter getting incremented + continue; + } + + rgDeathNoticeList[i].flDisplayTime = min( rgDeathNoticeList[i].flDisplayTime, gHUD.m_flTime + DEATHNOTICE_DISPLAY_TIME ); + + // Draw the death notice + + y = DEATHNOTICE_TOP + (20 * i); //!!! + + int id = (rgDeathNoticeList[i].iId == -1) ? m_HUD_d_skull : rgDeathNoticeList[i].iId; + x = SCREEN_WIDTH - ConsoleStringLen(rgDeathNoticeList[i].szVictim) - (gHUD.GetSpriteRect(id).right - gHUD.GetSpriteRect(id).left); + + if ( !rgDeathNoticeList[i].iSuicide ) + { + x -= (5 + ConsoleStringLen( rgDeathNoticeList[i].szKiller ) ); + + // Draw killers name + x = 5 + DrawConsoleString( x, y, rgDeathNoticeList[i].szKiller ); + } + + r = 255; g = 80; b = 0; + if ( rgDeathNoticeList[i].iTeamKill ) + { + r = 10; g = 240; b = 10; // display it in sickly green + } + + // Draw death weapon + SPR_Set( gHUD.GetSprite(id), r, g, b ); + SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect( id )); + + x += (gHUD.GetSpriteRect( id ).right - gHUD.GetSpriteRect( id ).left ); + + // Draw victims name + x = DrawConsoleString( x, y, rgDeathNoticeList[i].szVictim ); + } + return 1; +} + +// This message handler may be better off elsewhere +int CHudDeathNotice :: MsgFunc_DeathMsg( const char *pszName, int iSize, void *pbuf ) +{ + m_iFlags |= HUD_ACTIVE; + + BEGIN_READ( pszName, iSize, pbuf ); + + int killer = READ_BYTE(); + int victim = READ_BYTE(); + + char killedwith[32]; + strcpy( killedwith, "d_" ); + strncat( killedwith, READ_STRING(), 32 ); + + gHUD.m_Scoreboard.DeathMsg( killer, victim ); + + for ( int i = 0; i < MAX_DEATHNOTICES; i++ ) + { + if ( rgDeathNoticeList[i].iId == 0 ) + break; + } + if ( i == MAX_DEATHNOTICES ) + { + // move the rest of the list forward to make room for this item + memmove( rgDeathNoticeList, rgDeathNoticeList+1, sizeof( DeathNoticeItem ) * MAX_DEATHNOTICES ); + i = MAX_DEATHNOTICES - 1; + } + + gHUD.m_Scoreboard.GetAllPlayersInfo(); + + char *killer_name = gHUD.m_Scoreboard.m_PlayerInfoList[ killer ].name; + char *victim_name = gHUD.m_Scoreboard.m_PlayerInfoList[ victim ].name; + if( !killer_name ) killer_name = ""; + if( !victim_name ) victim_name = ""; + + // Is it a non-player object kill? + if ( ((char)victim) == -1 ) + { + rgDeathNoticeList[i].iNonPlayerKill = TRUE; + + // Store the object's name in the Victim slot (skip the d_ bit) + strcpy( rgDeathNoticeList[i].szVictim, killedwith+2 ); + } + else + { + if( killer == victim || killer == 0 ) + rgDeathNoticeList[i].iSuicide = TRUE; + + if( !strcmp( killedwith, "d_teammate" ) ) + rgDeathNoticeList[i].iTeamKill = TRUE; + } + + // Find the sprite in the list + int spr = gHUD.GetSpriteIndex( killedwith ); + + rgDeathNoticeList[i].iId = spr; + + DEATHNOTICE_DISPLAY_TIME = CVAR_GET_FLOAT( "hud_deathnotice_time" ); + rgDeathNoticeList[i].flDisplayTime = gHUD.m_flTime + DEATHNOTICE_DISPLAY_TIME; + + if( rgDeathNoticeList[i].iNonPlayerKill ) + { + ConsolePrint( rgDeathNoticeList[i].szKiller ); + ConsolePrint( " killed a " ); + ConsolePrint( rgDeathNoticeList[i].szVictim ); + ConsolePrint( "\n" ); + } + else + { + // record the death notice in the console + if ( rgDeathNoticeList[i].iSuicide ) + { + ConsolePrint( rgDeathNoticeList[i].szVictim ); + + if ( !strcmp( killedwith, "d_world" ) ) + { + ConsolePrint( " died" ); + } + else + { + ConsolePrint( " killed self" ); + } + } + else if ( rgDeathNoticeList[i].iTeamKill ) + { + ConsolePrint( rgDeathNoticeList[i].szKiller ); + ConsolePrint( " killed his teammate " ); + ConsolePrint( rgDeathNoticeList[i].szVictim ); + } + else + { + ConsolePrint( rgDeathNoticeList[i].szKiller ); + ConsolePrint( " killed " ); + ConsolePrint( rgDeathNoticeList[i].szVictim ); + } + + if ( killedwith && *killedwith && (*killedwith > 13 ) && strcmp( killedwith, "d_world" ) && !rgDeathNoticeList[i].iTeamKill ) + { + ConsolePrint( " with " ); + + // replace the code names with the 'real' names + if( !strcmp( killedwith+2, "egon" ) ) + strcpy( killedwith, "d_gluon gun" ); + if( !strcmp( killedwith+2, "gauss" ) ) + strcpy( killedwith, "d_tau cannon" ); + + ConsolePrint( killedwith+2 ); // skip over the "d_" part + } + ConsolePrint( "\n" ); + } + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_flashlight.cpp b/client/hud/hud_flashlight.cpp new file mode 100644 index 00000000..7aa945c2 --- /dev/null +++ b/client/hud/hud_flashlight.cpp @@ -0,0 +1,140 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// flashlight.cpp +// +// implementation of CHudFlashlight class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_Flash, FlashBat ) +DECLARE_MESSAGE( m_Flash, Flashlight ) + +int CHudFlashlight :: Init( void ) +{ + m_fFade = 0; + m_fOn = 0; + + HOOK_MESSAGE( Flashlight ); + HOOK_MESSAGE( FlashBat ); + + m_iFlags |= HUD_ACTIVE; + + gHUD.AddHudElem( this ); + + return 1; +} + +void CHudFlashlight :: Reset( void ) +{ + m_fFade = 0; + m_fOn = 0; +} + +int CHudFlashlight :: VidInit( void ) +{ + int HUD_flash_empty = gHUD.GetSpriteIndex( "flash_empty" ); + int HUD_flash_full = gHUD.GetSpriteIndex( "flash_full" ); + int HUD_flash_beam = gHUD.GetSpriteIndex( "flash_beam" ); + + m_hSprite1 = gHUD.GetSprite( HUD_flash_empty ); + m_hSprite2 = gHUD.GetSprite( HUD_flash_full ); + m_hBeam = gHUD.GetSprite( HUD_flash_beam ); + m_prc1 = &gHUD.GetSpriteRect( HUD_flash_empty ); + m_prc2 = &gHUD.GetSpriteRect( HUD_flash_full ); + m_prcBeam = &gHUD.GetSpriteRect( HUD_flash_beam ); + m_iWidth = m_prc2->right - m_prc2->left; + return 1; +} + +int CHudFlashlight:: MsgFunc_FlashBat(const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int x = READ_BYTE(); + m_iBat = x; + m_flBat = ((float)x) / 100.0; + + END_READ(); + return 1; +} + +int CHudFlashlight :: MsgFunc_Flashlight( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_fOn = READ_BYTE(); + int x = READ_BYTE(); + m_iBat = x; + m_flBat = ((float)x) / 100.0; + + END_READ(); + return 1; +} + +int CHudFlashlight :: Draw( float flTime ) +{ + if( gHUD.m_iHideHUDDisplay & (HIDEHUD_FLASHLIGHT|HIDEHUD_ALL) ) + return 1; + + int r, g, b, x, y, a; + wrect_t rc; + + if(!( gHUD.m_iHideHUDDisplay & ITEM_SUIT )) + return 1; + + if( m_fOn ) + a = 225; + else a = MIN_ALPHA; + + if( m_flBat < 0.20 ) + UnpackRGB( r, g, b, RGB_REDISH ); + else UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + + ScaleColors( r, g, b, a ); + + y = (m_prc1->bottom - m_prc2->top) / 2; + x = SCREEN_WIDTH - m_iWidth - m_iWidth / 2; + + // draw the flashlight casing + SPR_Set( m_hSprite1, r, g, b ); + SPR_DrawAdditive( 0, x, y, m_prc1 ); + + if( m_fOn ) + { + // draw the flashlight beam + x = SCREEN_WIDTH - m_iWidth / 2; + + SPR_Set( m_hBeam, r, g, b ); + SPR_DrawAdditive( 0, x, y, m_prcBeam ); + } + + // draw the flashlight energy level + x = SCREEN_WIDTH - m_iWidth - m_iWidth / 2; + int iOffset = m_iWidth * (1.0 - m_flBat); + + if( iOffset < m_iWidth ) + { + rc = *m_prc2; + rc.left += iOffset; + + SPR_Set( m_hSprite2, r, g, b ); + SPR_DrawAdditive( 0, x + iOffset, y, &rc ); + } + return 1; +} diff --git a/client/hud/hud_geiger.cpp b/client/hud/hud_geiger.cpp new file mode 100644 index 00000000..4bcd4b40 --- /dev/null +++ b/client/hud/hud_geiger.cpp @@ -0,0 +1,177 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// Geiger.cpp +// +// implementation of CHudAmmo class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_Geiger, Geiger ) + +int CHudGeiger::Init( void ) +{ + HOOK_MESSAGE( Geiger ); + + m_iGeigerRange = 0; + m_iFlags = 0; + + gHUD.AddHudElem( this ); + + return 1; +} + +int CHudGeiger::VidInit( void ) +{ + return 1; +} + +int CHudGeiger::MsgFunc_Geiger( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + // update geiger data + m_iGeigerRange = READ_BYTE(); + m_iGeigerRange = m_iGeigerRange << 2; + + m_iFlags |= HUD_ACTIVE; + + END_READ(); + + return 1; +} + +int CHudGeiger::Draw( float flTime ) +{ + int pct; + float flvol; + int rg[3]; + int i; + + if( m_iGeigerRange < 1000 && m_iGeigerRange > 0 ) + { + // peicewise linear is better than continuous formula for this + if( m_iGeigerRange > 800 ) + { + pct = 0; //ALERT( at_console, "range > 800\n" ); + } + else if( m_iGeigerRange > 600 ) + { + pct = 2; + flvol = 0.4; //ALERT( at_console, "range > 600\n" ); + rg[0] = 1; + rg[1] = 1; + i = 2; + } + else if( m_iGeigerRange > 500 ) + { + pct = 4; + flvol = 0.5; //ALERT( at_console, "range > 500\n" ); + rg[0] = 1; + rg[1] = 2; + i = 2; + } + else if( m_iGeigerRange > 400 ) + { + pct = 8; + flvol = 0.6; //ALERT( at_console, "range > 400\n" ); + rg[0] = 1; + rg[1] = 2; + rg[2] = 3; + i = 3; + } + else if( m_iGeigerRange > 300 ) + { + pct = 8; + flvol = 0.7; //ALERT( at_console, "range > 300\n" ); + rg[0] = 2; + rg[1] = 3; + rg[2] = 4; + i = 3; + } + else if( m_iGeigerRange > 200 ) + { + pct = 28; + flvol = 0.78; //ALERT( at_console, "range > 200\n" ); + rg[0] = 2; + rg[1] = 3; + rg[2] = 4; + i = 3; + } + else if( m_iGeigerRange > 150 ) + { + pct = 40; + flvol = 0.80; //ALERT( at_console, "range > 150\n" ); + rg[0] = 3; + rg[1] = 4; + rg[2] = 5; + i = 3; + } + else if( m_iGeigerRange > 100 ) + { + pct = 60; + flvol = 0.85; //ALERT( at_console, "range > 100\n" ); + rg[0] = 3; + rg[1] = 4; + rg[2] = 5; + i = 3; + } + else if( m_iGeigerRange > 75 ) + { + pct = 80; + flvol = 0.9; + rg[0] = 4; + rg[1] = 5; + rg[2] = 6; + i = 3; + } + else if( m_iGeigerRange > 50 ) + { + pct = 90; + flvol = 0.95; + rg[0] = 5; + rg[1] = 6; + i = 2; + } + else + { + pct = 95; + flvol = 1.0; + rg[0] = 5; + rg[1] = 6; + i = 2; + } + + flvol = flvol * RANDOM_FLOAT( 0.25, 0.5 ); + + if( RANDOM_LONG( 0, 128 ) < pct ) + { + char sz[256]; + + int j = RANDOM_LONG( 0, 2 ); + if( i > 2 ) + j += RANDOM_LONG( 0, 2 ); + + // player/geiger6.wav isn't used ? + sprintf( sz, "player/geiger%d.wav", j + 1 ); + CL_PlaySound( sz, flvol ); + + } + } + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_health.cpp b/client/hud/hud_health.cpp new file mode 100644 index 00000000..a5bde00c --- /dev/null +++ b/client/hud/hud_health.cpp @@ -0,0 +1,448 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// Health.cpp +// +// implementation of CHudHealth class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_Health, Health ) +DECLARE_MESSAGE( m_Health, Damage ) + +#define PAIN_NAME "hud_pain" +#define DAMAGE_NAME "sprites/%d_dmg.spr" + +int giDmgHeight, giDmgWidth; + +int giDmgFlags[NUM_DMG_TYPES] = +{ + DMG_POISON, + DMG_ACID, + DMG_FREEZE|DMG_SLOWFREEZE, + DMG_DROWN, + DMG_BURN|DMG_SLOWBURN, + DMG_NERVEGAS, + DMG_RADIATION, + DMG_SHOCK, + DMG_NUCLEAR +}; + +int CHudHealth :: Init( void ) +{ + HOOK_MESSAGE( Health ); + HOOK_MESSAGE( Damage ); + m_iHealth = 100; + m_fFade = 0; + m_iFlags = 0; + m_bitsDamage = 0; + m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 0; + giDmgHeight = 0; + giDmgWidth = 0; + + memset( m_dmg, 0, sizeof(DAMAGE_IMAGE) * NUM_DMG_TYPES ); + gHUD.AddHudElem( this ); + + return 1; +} + +void CHudHealth :: Reset( void ) +{ + // make sure the pain compass is cleared when the player respawns + m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 0; + + // force all the flashing damage icons to expire + m_bitsDamage = 0; + for( int i = 0; i < NUM_DMG_TYPES; i++ ) + { + m_dmg[i].fExpire = 0; + } +} + +int CHudHealth :: VidInit( void ) +{ + m_hSprite = 0; + + m_HUD_dmg_bio = gHUD.GetSpriteIndex( "dmg_bio" ) + 1; + m_HUD_cross = gHUD.GetSpriteIndex( "cross" ); + + giDmgHeight = gHUD.GetSpriteRect(m_HUD_dmg_bio).right - gHUD.GetSpriteRect(m_HUD_dmg_bio).left; + giDmgWidth = gHUD.GetSpriteRect(m_HUD_dmg_bio).bottom - gHUD.GetSpriteRect(m_HUD_dmg_bio).top; + return 1; +} + +int CHudHealth :: MsgFunc_Health( const char *pszName, int iSize, void *pbuf ) +{ + // TODO: update local health data + BEGIN_READ( pszName, iSize, pbuf ); + int x = READ_BYTE(); + + m_iFlags |= HUD_ACTIVE; + + // only update the fade if we've changed health + if( x != m_iHealth ) + { + m_fFade = FADE_TIME; + m_iHealth = x; + } + END_READ(); + + return 1; +} + +int CHudHealth :: MsgFunc_Damage( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int armor = READ_BYTE(); // armor + int damageTaken = READ_BYTE(); // health + long bitsDamage = READ_LONG(); // damage bits + + vec3_t vecFrom; + + for( int i = 0 ; i < 3 ; i++) + vecFrom[i] = READ_COORD(); + + UpdateTiles( gHUD.m_flTime, bitsDamage ); + + // actually took damage? + if( damageTaken > 0 || armor > 0 ) + CalcDamageDirection(vecFrom); + END_READ(); + + return 1; +} + +// Returns back a color from the +// Green <-> Yellow <-> Red ramp +void CHudHealth :: GetPainColor( int &r, int &g, int &b ) +{ + int iHealth = m_iHealth; + + if( iHealth > 25 ) + iHealth -= 25; + else if( iHealth < 0 ) + iHealth = 0; + + if( m_iHealth > 25 ) + { + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + } + else + { + r = 250; + g = 0; + b = 0; + } +} + +int CHudHealth :: Draw( float flTime ) +{ + int r, g, b; + int a = 0, x, y; + int HealthWidth; + + if((gHUD.m_iHideHUDDisplay & HIDEHUD_HEALTH) || IsSpectateOnly()) + return 1; + + if( !m_hSprite ) + m_hSprite = LOAD_SHADER( PAIN_NAME ); + + // has health changed? Flash the health # + if( m_fFade ) + { + m_fFade -= (gHUD.m_flTimeDelta * 20); + if( m_fFade <= 0 ) + { + a = MIN_ALPHA; + m_fFade = 0; + } + + // fade the health number back to dim + a = MIN_ALPHA + (m_fFade/FADE_TIME) * 128; + + } + else a = MIN_ALPHA; + + // if health is getting low, make it bright red + if( m_iHealth <= 15 ) a = 255; + + GetPainColor( r, g, b ); + ScaleColors( r, g, b, a ); + + // only draw health if we have the suit. + if( gHUD.m_iHideHUDDisplay & ITEM_SUIT ) + { + HealthWidth = gHUD.GetSpriteRect(gHUD.m_HUD_number_0).right - gHUD.GetSpriteRect(gHUD.m_HUD_number_0).left; + int CrossWidth = gHUD.GetSpriteRect(m_HUD_cross).right - gHUD.GetSpriteRect(m_HUD_cross).left; + + y = SCREEN_HEIGHT - gHUD.m_iFontHeight - gHUD.m_iFontHeight / 2; + x = CrossWidth / 2; + + SPR_Set( gHUD.GetSprite( m_HUD_cross ), r, g, b ); + SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect( m_HUD_cross )); + + x = CrossWidth + HealthWidth / 2; + + x = gHUD.DrawHudNumber( x, y, DHN_3DIGITS | DHN_DRAWZERO, m_iHealth, r, g, b ); + + x += HealthWidth / 2; + + int iHeight = gHUD.m_iFontHeight; + int iWidth = HealthWidth / 10; + + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + FillRGBA( x, y, iWidth, iHeight, r, g, b, a ); + } + + DrawDamage( flTime ); + return DrawPain( flTime ); +} + +void CHudHealth :: CalcDamageDirection( Vector vecFrom ) +{ + Vector forward, right, up; + float side, front; + Vector vecOrigin, vecAngles; + + if( vecFrom == Vector( 0, 0, 0 )) + { + m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 0; + return; + } + + memcpy( vecOrigin, gHUD.m_vecOrigin, sizeof( vec3_t )); + memcpy( vecAngles, gHUD.m_vecAngles, sizeof( vec3_t )); + + vecFrom -= vecOrigin; + + float flDistToTarget = vecFrom.Length(); + + vecFrom = vecFrom.Normalize(); + AngleVectors( vecAngles, forward, right, up ); + + front = vecFrom.Dot( right ); + side = vecFrom.Dot( forward ); + + if( flDistToTarget <= 50 ) + { + m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 1; + } + else + { + if( side > 0 ) + { + if( side > 0.3 ) + m_fAttackFront = max( m_fAttackFront, side ); + } + else + { + float f = fabs( side ); + if( f > 0.3 ) + m_fAttackRear = max( m_fAttackRear, f ); + } + + if( front > 0 ) + { + if( front > 0.3 ) + m_fAttackRight = max( m_fAttackRight, front ); + } + else + { + float f = fabs( front ); + if( f > 0.3 ) + m_fAttackLeft = max( m_fAttackLeft, f ); + } + } +} + +int CHudHealth :: DrawPain( float flTime ) +{ + if(!( m_fAttackFront || m_fAttackRear || m_fAttackLeft || m_fAttackRight )) + return 1; + + int r, g, b; + int x, y, a, shade; + + // TODO: get the shift value of the health + a = 255; // max brightness until then + + float fFade = gHUD.m_flTimeDelta * 2; + + // SPR_Draw top + if( m_fAttackFront > 0.4 ) + { + GetPainColor( r, g, b ); + shade = a * max( m_fAttackFront, 0.5 ); + ScaleColors( r, g, b, shade ); + SPR_Set( m_hSprite, r, g, b ); + + x = SCREEN_WIDTH / 2 - SPR_Width( m_hSprite, 0 ) / 2; + y = SCREEN_HEIGHT / 2 - SPR_Height( m_hSprite, 0 ) * 3; + SPR_DrawAdditive( 0, x, y, NULL ); + m_fAttackFront = max( 0, m_fAttackFront - fFade ); + } + else m_fAttackFront = 0; + + if( m_fAttackRight > 0.4 ) + { + GetPainColor( r, g, b ); + shade = a * max( m_fAttackRight, 0.5 ); + ScaleColors( r, g, b, shade ); + SPR_Set( m_hSprite, r, g, b ); + + x = SCREEN_WIDTH / 2 + SPR_Width( m_hSprite, 1 ) * 2; + y = SCREEN_HEIGHT / 2 - SPR_Height( m_hSprite,1 ) / 2; + SPR_DrawAdditive( 1, x, y, NULL ); + m_fAttackRight = max( 0, m_fAttackRight - fFade ); + } + else m_fAttackRight = 0; + + if( m_fAttackRear > 0.4 ) + { + GetPainColor( r, g, b ); + shade = a * max( m_fAttackRear, 0.5 ); + ScaleColors(r, g, b, shade); + SPR_Set(m_hSprite, r, g, b ); + + x = SCREEN_WIDTH / 2 - SPR_Width( m_hSprite, 2 ) / 2; + y = SCREEN_HEIGHT / 2 + SPR_Height( m_hSprite, 2 ) * 2; + SPR_DrawAdditive( 2, x, y, NULL); + m_fAttackRear = max( 0, m_fAttackRear - fFade ); + } + else m_fAttackRear = 0; + + if( m_fAttackLeft > 0.4 ) + { + GetPainColor( r, g, b ); + shade = a * max( m_fAttackLeft, 0.5 ); + ScaleColors( r, g, b, shade ); + SPR_Set( m_hSprite, r, g, b ); + + x = SCREEN_WIDTH / 2 - SPR_Width( m_hSprite, 3 ) * 3; + y = SCREEN_HEIGHT / 2 - SPR_Height( m_hSprite,3 ) / 2; + SPR_DrawAdditive( 3, x, y, NULL ); + + m_fAttackLeft = max( 0, m_fAttackLeft - fFade ); + } + else m_fAttackLeft = 0; + + return 1; +} + +int CHudHealth :: DrawDamage( float flTime ) +{ + int r, g, b, a; + DAMAGE_IMAGE *pdmg; + + if( !m_bitsDamage ) + return 1; + + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + + a = (int)(fabs(sin( flTime * 2)) * 256.0 ); + + ScaleColors( r, g, b, a ); + + // draw all the items + for( int i = 0; i < NUM_DMG_TYPES; i++ ) + { + if( m_bitsDamage & giDmgFlags[i] ) + { + pdmg = &m_dmg[i]; + SPR_Set( gHUD.GetSprite( m_HUD_dmg_bio + i ), r, g, b ); + SPR_DrawAdditive( 0, pdmg->x, pdmg->y, &gHUD.GetSpriteRect( m_HUD_dmg_bio + i )); + } + } + + // check for bits that should be expired + for( i = 0; i < NUM_DMG_TYPES; i++ ) + { + DAMAGE_IMAGE *pdmg = &m_dmg[i]; + + if( m_bitsDamage & giDmgFlags[i] ) + { + pdmg->fExpire = min( flTime + DMG_IMAGE_LIFE, pdmg->fExpire ); + + // when the time has expired and the flash is at the low point of the cycle + if( pdmg->fExpire <= flTime && a < 40 ) + { + pdmg->fExpire = 0; + + int y = pdmg->y; + pdmg->x = pdmg->y = 0; + + // move everyone above down + for( int j = 0; j < NUM_DMG_TYPES; j++ ) + { + pdmg = &m_dmg[j]; + if(( pdmg->y ) && ( pdmg->y < y )) + pdmg->y += giDmgHeight; + + } + m_bitsDamage &= ~giDmgFlags[i]; // clear the bits + } + } + } + return 1; +} + +void CHudHealth :: UpdateTiles( float flTime, long bitsDamage ) +{ + DAMAGE_IMAGE *pdmg; + + // Which types are new? + long bitsOn = ~m_bitsDamage & bitsDamage; + + for( int i = 0; i < NUM_DMG_TYPES; i++ ) + { + pdmg = &m_dmg[i]; + + // is this one already on? + if( m_bitsDamage & giDmgFlags[i] ) + { + pdmg->fExpire = flTime + DMG_IMAGE_LIFE; // extend the duration + if( !pdmg->fBaseline ) + pdmg->fBaseline = flTime; + } + + // are we just turning it on? + if( bitsOn & giDmgFlags[i] ) + { + // put this one at the bottom + pdmg->x = giDmgWidth / 8; + pdmg->y = SCREEN_HEIGHT - giDmgHeight * 2; + pdmg->fExpire=flTime + DMG_IMAGE_LIFE; + + // move everyone else up + for( int j = 0; j < NUM_DMG_TYPES; j++ ) + { + if( j == i ) continue; + + pdmg = &m_dmg[j]; + if( pdmg->y ) + pdmg->y -= giDmgHeight; + + } + pdmg = &m_dmg[i]; + } + } + + // damage bits are only turned on here; they are turned off when the draw time has expired (in DrawDamage()) + m_bitsDamage |= bitsDamage; +} diff --git a/client/hud/hud_health.h b/client/hud/hud_health.h new file mode 100644 index 00000000..cd90829a --- /dev/null +++ b/client/hud/hud_health.h @@ -0,0 +1,106 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ + +#define DMG_IMAGE_LIFE 2 // seconds that image is up + +#define DMG_IMAGE_POISON 0 +#define DMG_IMAGE_ACID 1 +#define DMG_IMAGE_COLD 2 +#define DMG_IMAGE_DROWN 3 +#define DMG_IMAGE_BURN 4 +#define DMG_IMAGE_NERVE 5 +#define DMG_IMAGE_RAD 6 +#define DMG_IMAGE_SHOCK 7 +//tf defines +#define DMG_IMAGE_CALTROP 8 +#define DMG_IMAGE_TRANQ 9 +#define DMG_IMAGE_CONCUSS 10 +#define DMG_IMAGE_HALLUC 11 +#define NUM_DMG_TYPES 12 +// instant damage + +#define DMG_GENERIC 0 // generic damage was done +#define DMG_CRUSH (1 << 0) // crushed by falling or moving object +#define DMG_BULLET (1 << 1) // shot +#define DMG_SLASH (1 << 2) // cut, clawed, stabbed +#define DMG_BURN (1 << 3) // heat burned +#define DMG_FREEZE (1 << 4) // frozen +#define DMG_FALL (1 << 5) // fell too far +#define DMG_BLAST (1 << 6) // explosive blast damage +#define DMG_CLUB (1 << 7) // crowbar, punch, headbutt +#define DMG_SHOCK (1 << 8) // electric shock +#define DMG_SONIC (1 << 9) // sound pulse shockwave +#define DMG_ENERGYBEAM (1 << 10) // laser or other high energy beam +#define DMG_NEVERGIB (1 << 12) // with this bit OR'd in, no damage type will be able to gib victims upon death +#define DMG_ALWAYSGIB (1 << 13) // with this bit OR'd in, any damage type can be made to gib victims upon death. + + +// time-based damage +//mask off TF-specific stuff too +#define DMG_TIMEBASED (~(0xff003fff)) // mask for time-based damage + + +#define DMG_DROWN (1 << 14) // Drowning +#define DMG_FIRSTTIMEBASED DMG_DROWN + +#define DMG_PARALYZE (1 << 15) // slows affected creature down +#define DMG_NERVEGAS (1 << 16) // nerve toxins, very bad +#define DMG_POISON (1 << 17) // blood poisioning +#define DMG_RADIATION (1 << 18) // radiation exposure +#define DMG_DROWNRECOVER (1 << 19) // drowning recovery +#define DMG_ACID (1 << 20) // toxic chemicals or acid burns +#define DMG_SLOWBURN (1 << 21) // in an oven +#define DMG_SLOWFREEZE (1 << 22) // in a subzero freezer +#define DMG_MORTAR (1 << 23) // Hit by air raid (done to distinguish grenade from mortar) +#define DMG_NUCLEAR (1 << 24) // Players hit by this begin to burn + + +typedef struct +{ + float fExpire; + float fBaseline; + int x, y; +} DAMAGE_IMAGE; + +// +//----------------------------------------------------- +// +class CHudHealth: public CHudBase +{ +public: + virtual int Init( void ); + virtual int VidInit( void ); + virtual int Draw( float fTime ); + virtual void Reset( void ); + int MsgFunc_Health( const char *pszName, int iSize, void *pbuf ); + int MsgFunc_Damage( const char *pszName, int iSize, void *pbuf ); + int m_iHealth; + int m_HUD_dmg_bio; + int m_HUD_cross; + float m_fAttackFront, m_fAttackRear, m_fAttackLeft, m_fAttackRight; + void GetPainColor( int &r, int &g, int &b ); + float m_fFade; + +private: + HSPRITE m_hSprite; + HSPRITE m_hDamage; + + DAMAGE_IMAGE m_dmg[NUM_DMG_TYPES]; + int m_bitsDamage; + int DrawPain( float fTime ); + int DrawDamage( float fTime ); + void CalcDamageDirection( vec3_t vecFrom ); + void UpdateTiles( float fTime, long bits ); +}; diff --git a/client/hud/hud_icons.cpp b/client/hud/hud_icons.cpp new file mode 100644 index 00000000..ebe74c6f --- /dev/null +++ b/client/hud/hud_icons.cpp @@ -0,0 +1,150 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// status_icons.cpp +// +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_StatusIcons, StatusIcon ); + +int CHudStatusIcons::Init( void ) +{ + HOOK_MESSAGE( StatusIcon ); + gHUD.AddHudElem( this ); + Reset(); + + return 1; +} + +int CHudStatusIcons::VidInit( void ) +{ + return 1; +} + +void CHudStatusIcons::Reset( void ) +{ + memset( m_IconList, 0, sizeof m_IconList ); + m_iFlags &= ~HUD_ACTIVE; +} + +// Draw status icons along the left-hand side of the screen +int CHudStatusIcons::Draw( float flTime ) +{ + // find starting position to draw from, along right-hand side of screen + int x = 5; + int y = SCREEN_HEIGHT / 2; + + // loop through icon list, and draw any valid icons drawing up from the middle of screen + for( int i = 0; i < MAX_ICONSPRITES; i++ ) + { + if ( m_IconList[i].spr ) + { + y -= ( m_IconList[i].rc.bottom - m_IconList[i].rc.top ) + 5; + + SPR_Set( m_IconList[i].spr, m_IconList[i].r, m_IconList[i].g, m_IconList[i].b ); + SPR_DrawAdditive( 0, x, y, &m_IconList[i].rc ); + } + } + return 1; +} + +// Message handler for StatusIcon message +// accepts five values: +// byte : TRUE = ENABLE icon, FALSE = DISABLE icon +// string : the sprite name to display +// byte : red +// byte : green +// byte : blue +int CHudStatusIcons::MsgFunc_StatusIcon( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int ShouldEnable = READ_BYTE(); + char *pszIconName = READ_STRING(); + if ( ShouldEnable ) + { + int r = READ_BYTE(); + int g = READ_BYTE(); + int b = READ_BYTE(); + EnableIcon( pszIconName, r, g, b ); + m_iFlags |= HUD_ACTIVE; + } + else + { + DisableIcon( pszIconName ); + } + END_READ(); + + return 1; +} + +// add the icon to the icon list, and set it's drawing color +void CHudStatusIcons::EnableIcon( char *pszIconName, byte red, byte green, byte blue ) +{ + // check to see if the sprite is in the current list + for( int i = 0; i < MAX_ICONSPRITES; i++ ) + { + if ( !stricmp( m_IconList[i].szSpriteName, pszIconName ) ) + break; + } + + if( i == MAX_ICONSPRITES ) + { + // icon not in list, so find an empty slot to add to + for( i = 0; i < MAX_ICONSPRITES; i++ ) + { + if( !m_IconList[i].spr ) + break; + } + } + + // if we've run out of space in the list, overwrite the first icon + if( i == MAX_ICONSPRITES ) + { + i = 0; + } + + // Load the sprite and add it to the list + // the sprite must be listed in hud.txt + int spr_index = gHUD.GetSpriteIndex( pszIconName ); + m_IconList[i].spr = gHUD.GetSprite( spr_index ); + m_IconList[i].rc = gHUD.GetSpriteRect( spr_index ); + m_IconList[i].r = red; + m_IconList[i].g = green; + m_IconList[i].b = blue; + strcpy( m_IconList[i].szSpriteName, pszIconName ); + + // HACKHACK: Play Timer sound when a grenade icon is played (in 0.8 seconds) + if( strstr( m_IconList[i].szSpriteName, "grenade" )) + { + CL_PlaySound( "weapons/timer.wav", 1.0 ); + } +} + +void CHudStatusIcons::DisableIcon( char *pszIconName ) +{ + // find the sprite is in the current list + for( int i = 0; i < MAX_ICONSPRITES; i++ ) + { + if( !stricmp( m_IconList[i].szSpriteName, pszIconName ) ) + { + // clear the item from the list + memset( &m_IconList[i], 0, sizeof icon_sprite_t ); + return; + } + } +} \ No newline at end of file diff --git a/client/hud/hud_iface.h b/client/hud/hud_iface.h new file mode 100644 index 00000000..ad0e26e9 --- /dev/null +++ b/client/hud/hud_iface.h @@ -0,0 +1,154 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// hud_iface.h - client exported funcs +//======================================================================= + +#ifndef HUD_IFACE_H +#define HUD_IFACE_H + +extern cl_enginefuncs_t g_engfuncs; + +#include "enginecallback.h" + +extern int HUD_VidInit( void ); +extern void HUD_Init( void ); +extern int HUD_Redraw( float flTime, int intermission ); +extern int HUD_UpdateClientData( ref_params_t *parms, float flTime ); +extern void HUD_Reset( void ); +extern void HUD_Frame( double time ); +extern void HUD_Shutdown( void ); +extern void HUD_DrawNormalTriangles( void ); +extern void HUD_DrawTransparentTriangles( void ); +extern void HUD_CreateEntities( void ); +extern void HUD_StudioEvent( const dstudioevent_t *event, edict_t *entity ); +extern void V_CalcRefdef( ref_params_t *parms ); + +typedef struct rect_s +{ + int left; + int right; + int top; + int bottom; +} wrect_t; + +typedef HMODULE dllhandle_t; +typedef struct dllfunction_s +{ + const char *name; + void **funcvariable; +} dllfunction_t; + +// cvar flags +#define CVAR_ARCHIVE BIT(0) // set to cause it to be saved to vars.rc +#define CVAR_USERINFO BIT(1) // added to userinfo when changed +#define CVAR_SERVERINFO BIT(2) // added to serverinfo when changed + +// macros to hook function calls into the HUD object +#define HOOK_MESSAGE( x ) (*g_engfuncs.pfnHookUserMsg)( #x, __MsgFunc_##x ); + +#define DECLARE_MESSAGE( y, x ) int __MsgFunc_##x(const char *pszName, int iSize, void *pbuf) \ +{ \ + return gHUD.##y.MsgFunc_##x(pszName, iSize, pbuf ); \ +} + +#define DECLARE_HUDMESSAGE( x ) int __MsgFunc_##x( const char *pszName, int iSize, void *pbuf ) \ +{ \ + return gHUD.MsgFunc_##x(pszName, iSize, pbuf ); \ +} + +#define HOOK_COMMAND( x, y ) (*g_engfuncs.pfnAddCommand)( x, __CmdFunc_##y, "user-defined command" ); +#define DECLARE_COMMAND( y, x ) void __CmdFunc_##x( void ) \ +{ \ + gHUD.##y.UserCmd_##x( ); \ +} + +inline void UnpackRGB( int &r, int &g, int &b, dword ulRGB ) +{ + r = (ulRGB & 0xFF0000) >>16;\ + g = (ulRGB & 0xFF00) >> 8;\ + b = ulRGB & 0xFF;\ +} + +inline void ScaleColors( int &r, int &g, int &b, int a ) +{ + float x = (float)a / 255; + r = (int)(r * x); + g = (int)(g * x); + b = (int)(b * x); +} + +inline int ConsoleStringLen( const char *string ) +{ + // console using fixed font size + return strlen( string ) * SMALLCHAR_WIDTH; +} + +inline void GetConsoleStringSize( const char *string, int *width, int *height ) +{ + // console using fixed font size + if( width ) *width = ConsoleStringLen( string ); + if( height ) *height = SMALLCHAR_HEIGHT; +} + +// simple huh ? +inline int DrawConsoleString( int x, int y, const char *string ) +{ + DrawString( x, y, SMALLCHAR_WIDTH, SMALLCHAR_HEIGHT, string ); + + return ConsoleStringLen( string ); +} + +inline void ConsolePrint( const char *string ) +{ + ALERT( at_console, "%s", string ); +} + +// returns the players name of entity no. +inline void GetPlayerInfo( int ent_num, hud_player_info_t *pinfo ) +{ + GET_PLAYER_INFO( ent_num, pinfo ); +} + +inline client_textmessage_t *TextMessageGet( const char *pName ) +{ + return GET_GAME_MESSAGE( pName ); +} + +// message reading +extern void BEGIN_READ( const char *pszName, int iSize, void *pBuf ); +extern int READ_CHAR( void ); +extern int READ_BYTE( void ); +extern int READ_SHORT( void ); +extern int READ_WORD( void ); +extern int READ_LONG( void ); +extern float READ_FLOAT( void ); +extern char* READ_STRING( void ); +extern float READ_COORD( void ); +extern float READ_ANGLE( void ); +extern float READ_ANGLE16( void ); +extern void END_READ( void ); + +// drawing stuff +extern int SPR_Frames( HSPRITE hPic ); +extern int SPR_Height( HSPRITE hPic, int frame ); +extern int SPR_Width( HSPRITE hPic, int frame ); +extern void SPR_Set( HSPRITE hPic, int r, int g, int b ); +extern void SPR_Draw( int frame, int x, int y, const wrect_t *prc ); +extern void SPR_Draw( int frame, int x, int y, int width, int height ); +extern void SPR_DrawHoles( int frame, int x, int y, const wrect_t *prc ); +extern void SPR_DrawHoles( int frame, int x, int y, int width, int height ); +extern void SPR_DrawAdditive( int frame, int x, int y, const wrect_t *prc ); +extern void SetCrosshair( HSPRITE hspr, wrect_t rc, int r, int g, int b ); +extern void DrawCrosshair( void ); +extern void SetScreenFade( Vector fadeColor, float alpha, float duration, float holdTime, int fadeFlags ); +extern void DrawScreenFade( void ); + +// stdio stuff +extern char *va( const char *format, ... ); + +// dlls stuff +BOOL Sys_LoadLibrary( const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts ); +void* Sys_GetProcAddress( dllhandle_t handle, const char* name ); +void Sys_UnloadLibrary( dllhandle_t* handle ); + +#endif//HUD_IFACE_H \ No newline at end of file diff --git a/client/hud/hud_menu.cpp b/client/hud/hud_menu.cpp new file mode 100644 index 00000000..552bc551 --- /dev/null +++ b/client/hud/hud_menu.cpp @@ -0,0 +1,172 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// menu.cpp +// +// generic menu handler +// +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +#define MAX_MENU_STRING 512 +char g_szMenuString[MAX_MENU_STRING]; +char g_szPrelocalisedMenuString[MAX_MENU_STRING]; + +DECLARE_MESSAGE( m_Menu, ShowMenu ); + +int CHudMenu :: Init( void ) +{ + gHUD.AddHudElem( this ); + + HOOK_MESSAGE( ShowMenu ); + + InitHUDData(); + + return 1; +} + +void CHudMenu :: InitHUDData( void ) +{ + m_fMenuDisplayed = 0; + m_bitsValidSlots = 0; + Reset(); +} + +void CHudMenu :: Reset( void ) +{ + g_szPrelocalisedMenuString[0] = 0; + m_fWaitingForMore = FALSE; +} + +int CHudMenu :: VidInit( void ) +{ + return 1; +} + +int CHudMenu :: Draw( float flTime ) +{ + // check for if menu is set to disappear + if( m_flShutoffTime > 0 ) + { + if( m_flShutoffTime <= gHUD.m_flTime ) + { + // times up, shutoff + m_fMenuDisplayed = 0; + m_iFlags &= ~HUD_ACTIVE; + return 1; + } + } + + // don't draw the menu if the scoreboard is being shown + if( gHUD.m_Scoreboard.m_iShowscoresHeld ) + return 1; + + // draw the menu, along the left-hand side of the screen + + // count the number of newlines + int nlc = 0; + for( int i = 0; i < MAX_MENU_STRING && g_szMenuString[i] != '\0'; i++ ) + { + if( g_szMenuString[i] == '\n' ) + nlc++; + } + + // center it + int y = (SCREEN_HEIGHT/2) - ((nlc/2)*12) - 40; // make sure it is above the say text + int x = 20; + + i = 0; + while( i < MAX_MENU_STRING && g_szMenuString[i] != '\0' ) + { + gHUD.DrawHudString( x, y, 320, g_szMenuString + i, 255, 255, 255 ); + y += 12; + + while( i < MAX_MENU_STRING && g_szMenuString[i] != '\0' && g_szMenuString[i] != '\n' ) + i++; + if( g_szMenuString[i] == '\n' ) + i++; + } + return 1; +} + +// selects an item from the menu +void CHudMenu :: SelectMenuItem( int menu_item ) +{ + // if menu_item is in a valid slot, send a menuselect command to the server + if( (menu_item > 0) && (m_bitsValidSlots & (1<<(menu_item - 1))) ) + { + char szbuf[32]; + sprintf( szbuf, "menuselect %d\n", menu_item ); + CLIENT_COMMAND( szbuf ); + + // remove the menu + m_fMenuDisplayed = 0; + m_iFlags &= ~HUD_ACTIVE; + } +} + +// Message handler for ShowMenu message +// takes four values: +// short: a bitfield of keys that are valid input +// char : the duration, in seconds, the menu should stay up. -1 means is stays until something is chosen. +// byte : a boolean, TRUE if there is more string yet to be received before displaying the menu, FALSE if it's the last string +// string: menu string to display +// if this message is never received, then scores will simply be the combined totals of the players. + +int CHudMenu :: MsgFunc_ShowMenu( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_bitsValidSlots = READ_SHORT(); + int DisplayTime = READ_CHAR(); + int NeedMore = READ_BYTE(); + + if( DisplayTime > 0 ) + m_flShutoffTime = DisplayTime + gHUD.m_flTime; + else m_flShutoffTime = -1; + + if( m_bitsValidSlots ) + { + if( !m_fWaitingForMore ) // this is the start of a new menu + { + strncpy( g_szPrelocalisedMenuString, READ_STRING(), MAX_MENU_STRING ); + } + else + { + // append to the current menu string + strncat( g_szPrelocalisedMenuString, READ_STRING(), MAX_MENU_STRING - strlen(g_szPrelocalisedMenuString) ); + } + g_szPrelocalisedMenuString[MAX_MENU_STRING-1] = 0; // ensure null termination (strncat/strncpy does not) + + if( !NeedMore ) + { + // we have the whole string, so we can localise it now + strcpy( g_szMenuString, gHUD.m_TextMessage.BufferedLocaliseTextString( g_szPrelocalisedMenuString ) ); + } + + m_fMenuDisplayed = 1; + m_iFlags |= HUD_ACTIVE; + } + else + { + m_fMenuDisplayed = 0; // no valid slots means that the menu should be turned off + m_iFlags &= ~HUD_ACTIVE; + } + + m_fWaitingForMore = NeedMore; + + return 1; +} diff --git a/client/hud/hud_message.cpp b/client/hud/hud_message.cpp new file mode 100644 index 00000000..9a399045 --- /dev/null +++ b/client/hud/hud_message.cpp @@ -0,0 +1,514 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// Message.cpp +// +// implementation of CHudMessage class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_Message, HudText ) +DECLARE_MESSAGE( m_Message, GameTitle ) + +// 1 Global client_textmessage_t for custom messages that aren't in the titles.txt +client_textmessage_t g_pCustomMessage; +char *g_pCustomName = "Custom"; +char g_pCustomText[1024]; + +int CHudMessage :: Init( void ) +{ + HOOK_MESSAGE( HudText ); + HOOK_MESSAGE( GameTitle ); + + gHUD.AddHudElem( this ); + Reset(); + + return 1; +} + +int CHudMessage :: VidInit( void ) +{ + HUD_Logo = gHUD.GetSpriteIndex( "logo" ); + return 1; +} + +void CHudMessage :: Reset( void ) +{ + memset( m_pMessages, 0, sizeof( m_pMessages[0] ) * maxHUDMessages ); + memset( m_startTime, 0, sizeof( m_startTime[0] ) * maxHUDMessages ); + + m_gameTitleTime = 0; + m_pGameTitle = NULL; +} + +float CHudMessage :: FadeBlend( float fadein, float fadeout, float hold, float localTime ) +{ + float fadeTime = fadein + hold; + float fadeBlend; + + if( localTime < 0 ) + return 0; + + if( localTime < fadein ) + { + fadeBlend = 1 - ((fadein - localTime) / fadein); + } + else if( localTime > fadeTime ) + { + if( fadeout > 0 ) + fadeBlend = 1 - ((localTime - fadeTime) / fadeout); + else fadeBlend = 0; + } + else fadeBlend = 1; + + return fadeBlend; +} + +int CHudMessage::XPosition( float x, int width, int totalWidth ) +{ + int xPos; + + if( x == -1 ) + { + xPos = (SCREEN_WIDTH - width) / 2; + } + else + { + if ( x < 0 ) + xPos = (1.0 + x) * SCREEN_WIDTH - totalWidth; // Alight right + else + xPos = x * SCREEN_WIDTH; + } + + if( xPos + width > SCREEN_WIDTH ) + xPos = SCREEN_WIDTH - width; + else if( xPos < 0 ) + xPos = 0; + + return xPos; +} + +int CHudMessage::YPosition( float y, int height ) +{ + int yPos; + + if( y == -1 ) // Centered? + yPos = (SCREEN_HEIGHT - height) * 0.5; + else + { + // Alight bottom? + if( y < 0 ) + yPos = (1.0 + y) * SCREEN_HEIGHT - height; // Alight bottom + else // align top + yPos = y * SCREEN_HEIGHT; + } + + if( yPos + height > SCREEN_HEIGHT ) + yPos = SCREEN_HEIGHT - height; + else if( yPos < 0 ) + yPos = 0; + + return yPos; +} + +void CHudMessage :: MessageScanNextChar( void ) +{ + int srcRed, srcGreen, srcBlue, destRed, destGreen, destBlue; + int blend; + + srcRed = m_parms.pMessage->r1; + srcGreen = m_parms.pMessage->g1; + srcBlue = m_parms.pMessage->b1; + blend = 0; // Pure source + + switch( m_parms.pMessage->effect ) + { + case 0: // Fade-in / Fade-out + case 1: + destRed = destGreen = destBlue = 0; + blend = m_parms.fadeBlend; + break; + + case 2: + m_parms.charTime += m_parms.pMessage->fadein; + if( m_parms.charTime > m_parms.time ) + { + srcRed = srcGreen = srcBlue = 0; + blend = 0; // pure source + } + else + { + float deltaTime = m_parms.time - m_parms.charTime; + + destRed = destGreen = destBlue = 0; + if( m_parms.time > m_parms.fadeTime ) + { + blend = m_parms.fadeBlend; + } + else if( deltaTime > m_parms.pMessage->fxtime ) + blend = 0; // pure dest + else + { + destRed = m_parms.pMessage->r2; + destGreen = m_parms.pMessage->g2; + destBlue = m_parms.pMessage->b2; + blend = 255 - (deltaTime * (1.0 / m_parms.pMessage->fxtime) * 255.0 + 0.5); + } + } + break; + } + + if( blend > 255 ) + blend = 255; + else if( blend < 0 ) + blend = 0; + + m_parms.r = ((srcRed * (255-blend)) + (destRed * blend)) >> 8; + m_parms.g = ((srcGreen * (255-blend)) + (destGreen * blend)) >> 8; + m_parms.b = ((srcBlue * (255-blend)) + (destBlue * blend)) >> 8; + + if( m_parms.pMessage->effect == 1 && m_parms.charTime != 0 ) + { + if( m_parms.x >= 0 && m_parms.y >= 0 && (m_parms.x + gHUD.charWidths[ m_parms.text ]) <= SCREEN_WIDTH ) + TextMessageDrawChar( m_parms.x, m_parms.y, m_parms.text, m_parms.pMessage->r2, m_parms.pMessage->g2, m_parms.pMessage->b2 ); + } +} + + +void CHudMessage :: MessageScanStart( void ) +{ + switch( m_parms.pMessage->effect ) + { + case 1: // Fade-in / out with flicker + case 0: + m_parms.fadeTime = m_parms.pMessage->fadein + m_parms.pMessage->holdtime; + + if( m_parms.time < m_parms.pMessage->fadein ) + { + m_parms.fadeBlend = ((m_parms.pMessage->fadein - m_parms.time) * (1.0/m_parms.pMessage->fadein) * 255); + } + else if( m_parms.time > m_parms.fadeTime ) + { + if ( m_parms.pMessage->fadeout > 0 ) + m_parms.fadeBlend = (((m_parms.time - m_parms.fadeTime) / m_parms.pMessage->fadeout) * 255); + else + m_parms.fadeBlend = 255; // Pure dest (off) + } + else m_parms.fadeBlend = 0; // Pure source (on) + m_parms.charTime = 0; + + if( m_parms.pMessage->effect == 1 && (rand() % 100 ) < 10 ) + m_parms.charTime = 1; + break; + case 2: + m_parms.fadeTime = (m_parms.pMessage->fadein * m_parms.length) + m_parms.pMessage->holdtime; + + if( m_parms.time > m_parms.fadeTime && m_parms.pMessage->fadeout > 0 ) + m_parms.fadeBlend = (((m_parms.time - m_parms.fadeTime) / m_parms.pMessage->fadeout) * 255); + else m_parms.fadeBlend = 0; + break; + } +} + + +void CHudMessage :: MessageDrawScan( client_textmessage_t *pMessage, float time ) +{ + int i, j, length, width; + const char *pText; + unsigned char line[80]; + + pText = pMessage->pMessage; + // Count lines + m_parms.lines = 1; + m_parms.time = time; + m_parms.pMessage = pMessage; + length = 0; + width = 0; + m_parms.totalWidth = 0; + + while( *pText ) + { + if ( *pText == '\n' ) + { + m_parms.lines++; + if ( width > m_parms.totalWidth ) + m_parms.totalWidth = width; + width = 0; + } + else width += gHUD.charWidths[*pText]; + pText++; + length++; + } + m_parms.length = length; + m_parms.totalHeight = (m_parms.lines * gHUD.iCharHeight); + + + m_parms.y = YPosition( pMessage->y, m_parms.totalHeight ); + pText = pMessage->pMessage; + + m_parms.charTime = 0; + + MessageScanStart(); + + for( i = 0; i < m_parms.lines; i++ ) + { + m_parms.lineLength = 0; + m_parms.width = 0; + while( *pText && *pText != '\n' ) + { + unsigned char c = *pText; + line[m_parms.lineLength] = c; + m_parms.width += gHUD.charWidths[c]; + m_parms.lineLength++; + pText++; + } + pText++; // Skip LineFeed + line[m_parms.lineLength] = 0; + + m_parms.x = XPosition( pMessage->x, m_parms.width, m_parms.totalWidth ); + + for ( j = 0; j < m_parms.lineLength; j++ ) + { + m_parms.text = line[j]; + int next = m_parms.x + gHUD.charWidths[ m_parms.text ]; + MessageScanNextChar(); + + if ( m_parms.x >= 0 && m_parms.y >= 0 && next <= SCREEN_WIDTH ) + TextMessageDrawChar( m_parms.x, m_parms.y, m_parms.text, m_parms.r, m_parms.g, m_parms.b ); + m_parms.x = next; + } + m_parms.y += gHUD.iCharHeight; + } +} + + +int CHudMessage :: Draw( float fTime ) +{ + int i, drawn; + client_textmessage_t *pMessage; + float endTime; + + drawn = 0; + + if( m_gameTitleTime > 0 ) + { + float localTime = gHUD.m_flTime - m_gameTitleTime; + float brightness; + + // Maybe timer isn't set yet + if( m_gameTitleTime > gHUD.m_flTime ) + m_gameTitleTime = gHUD.m_flTime; + + if( localTime > (m_pGameTitle->fadein + m_pGameTitle->holdtime + m_pGameTitle->fadeout) ) + m_gameTitleTime = 0; + else + { + brightness = FadeBlend( m_pGameTitle->fadein, m_pGameTitle->fadeout, m_pGameTitle->holdtime, localTime ); + + int fullWidth = gHUD.GetSpriteRect(HUD_Logo).right - gHUD.GetSpriteRect(HUD_Logo).left; + int fullHeight = gHUD.GetSpriteRect(HUD_Logo).bottom - gHUD.GetSpriteRect(HUD_Logo).top; + + int x = XPosition( m_pGameTitle->x, fullWidth, fullWidth ); + int y = YPosition( m_pGameTitle->y, fullHeight); + + SPR_Set( gHUD.GetSprite(HUD_Logo), brightness * m_pGameTitle->r1, brightness * m_pGameTitle->g1, brightness * m_pGameTitle->b1 ); + SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect( HUD_Logo )); + + drawn = 1; + } + } + + // fixup level transitions + for( i = 0; i < maxHUDMessages; i++ ) + { + // assume m_parms.time contains last time + if( m_pMessages[i] ) + { + pMessage = m_pMessages[i]; + if( m_startTime[i] > gHUD.m_flTime ) + { + // Server takes 0.2 seconds to spawn, adjust for this + m_startTime[i] = gHUD.m_flTime + m_parms.time - m_startTime[i] + 0.2; + } + } + } + + for( i = 0; i < maxHUDMessages; i++ ) + { + if ( m_pMessages[i] ) + { + pMessage = m_pMessages[i]; + + // This is when the message is over + switch( pMessage->effect ) + { + case 0: + case 1: + endTime = m_startTime[i] + pMessage->fadein + pMessage->fadeout + pMessage->holdtime; + break; + + // Fade in is per character in scanning messages + case 2: + endTime = m_startTime[i] + (pMessage->fadein * strlen( pMessage->pMessage )) + pMessage->fadeout + pMessage->holdtime; + break; + } + + if( fTime <= endTime ) + { + float messageTime = fTime - m_startTime[i]; + + // Draw the message + // effect 0 is fade in/fade out + // effect 1 is flickery credits + // effect 2 is write out (training room) + MessageDrawScan( pMessage, messageTime ); + + drawn++; + } + else + { + // The message is over + m_pMessages[i] = NULL; + } + } + } + + // Remember the time -- to fix up level transitions + m_parms.time = gHUD.m_flTime; + + // Don't call until we get another message + if( !drawn ) m_iFlags &= ~HUD_ACTIVE; + + return 1; +} + +void CHudMessage :: MessageAdd( const char *pName, float time ) +{ + int i, j; + client_textmessage_t *tempMessage; + + for( i = 0; i < maxHUDMessages; i++ ) + { + if( !m_pMessages[i] ) + { + // Trim off a leading # if it's there + if( pName[0] == '#' ) + tempMessage = TextMessageGet( pName+1 ); + else tempMessage = TextMessageGet( pName ); + + // If we couldnt find it in the titles.txt, just create it + if( !tempMessage ) + { + g_pCustomMessage.effect = 2; + g_pCustomMessage.r1 = g_pCustomMessage.g1 = g_pCustomMessage.b1 = g_pCustomMessage.a1 = 100; + g_pCustomMessage.r2 = 240; + g_pCustomMessage.g2 = 110; + g_pCustomMessage.b2 = 0; + g_pCustomMessage.a2 = 0; + g_pCustomMessage.x = -1; // Centered + g_pCustomMessage.y = 0.7; + g_pCustomMessage.fadein = 0.01; + g_pCustomMessage.fadeout = 1.5; + g_pCustomMessage.fxtime = 0.25; + g_pCustomMessage.holdtime = 5; + g_pCustomMessage.pName = g_pCustomName; + strcpy( g_pCustomText, pName ); + g_pCustomMessage.pMessage = g_pCustomText; + + tempMessage = &g_pCustomMessage; + } + + for( j = 0; j < maxHUDMessages; j++ ) + { + if( m_pMessages[j] ) + { + // is this message already in the list + if( !strcmp( tempMessage->pMessage, m_pMessages[j]->pMessage ) ) + { + return; + } + + // get rid of any other messages in same location (only one displays at a time) + if( fabs( tempMessage->y - m_pMessages[j]->y ) < 0.0001 ) + { + if ( fabs( tempMessage->x - m_pMessages[j]->x ) < 0.0001 ) + { + m_pMessages[j] = NULL; + } + } + } + } + m_pMessages[i] = tempMessage; + m_startTime[i] = time; + return; + } + } +} + +int CHudMessage::MsgFunc_HudText( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + char *pString = READ_STRING(); + + MessageAdd( pString, gHUD.m_flTime ); + // Remember the time -- to fix up level transitions + m_parms.time = gHUD.m_flTime; + + // Turn on drawing + if(!( m_iFlags & HUD_ACTIVE )) + m_iFlags |= HUD_ACTIVE; + END_READ(); + + return 1; +} + +int CHudMessage::MsgFunc_GameTitle( const char *pszName, int iSize, void *pbuf ) +{ + m_pGameTitle = TextMessageGet( "GAMETITLE" ); + + if( m_pGameTitle != NULL ) + { + m_gameTitleTime = gHUD.m_flTime; + + // Turn on drawing + if(!( m_iFlags & HUD_ACTIVE )) + m_iFlags |= HUD_ACTIVE; + } + return 1; +} + +void CHudMessage :: MessageAdd( client_textmessage_t *newMessage ) +{ + m_parms.time = gHUD.m_flTime; + + // Turn on drawing + if(!( m_iFlags & HUD_ACTIVE )) + m_iFlags |= HUD_ACTIVE; + + for( int i = 0; i < maxHUDMessages; i++ ) + { + if( !m_pMessages[i] ) + { + m_pMessages[i] = newMessage; + m_startTime[i] = gHUD.m_flTime; + return; + } + } + +} \ No newline at end of file diff --git a/client/hud/hud_motd.cpp b/client/hud/hud_motd.cpp new file mode 100644 index 00000000..0bf9e317 --- /dev/null +++ b/client/hud/hud_motd.cpp @@ -0,0 +1,132 @@ +/*** +* +* Copyright (c) 1999, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// +// for displaying a server-sent message of the day +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_MOTD, MOTD ); + +int CHudMOTD :: MOTD_DISPLAY_TIME; + +int CHudMOTD :: Init( void ) +{ + gHUD.AddHudElem( this ); + + HOOK_MESSAGE( MOTD ); + CVAR_REGISTER( "motd_display_time", "6", 0, "time to show message of the day" ); + m_iFlags &= ~HUD_ACTIVE; // start out inactive + m_szMOTD[0] = 0; + + return 1; +} + +int CHudMOTD :: VidInit( void ) +{ + // Load sprites here + + return 1; +} + +void CHudMOTD :: Reset( void ) +{ + m_iFlags &= ~HUD_ACTIVE; // start out inactive + m_szMOTD[0] = 0; + m_iLines = 0; + m_flActiveTill = 0; +} + +#define LINE_HEIGHT 13 + +int CHudMOTD :: Draw( float fTime ) +{ + // Draw MOTD line-by-line + + if( m_flActiveTill < gHUD.m_flTime ) + { + // finished with MOTD, disable it + m_szMOTD[0] = 0; + m_iLines = 0; + m_iFlags &= ~HUD_ACTIVE; + return 1; + } + + // cap activetill time to the display time + m_flActiveTill = min( gHUD.m_flTime + MOTD_DISPLAY_TIME, m_flActiveTill ); + + // find the top of where the MOTD should be drawn, so the whole thing is centered in the screen + int ypos = max(((SCREEN_HEIGHT - (m_iLines * LINE_HEIGHT)) / 2) - 40, 30 ); // shift it up slightly + char *ch = m_szMOTD; + while( *ch ) + { + int line_length = 0; // count the length of the current line + for( char *next_line = ch; *next_line != '\n' && *next_line != 0; next_line++ ) + line_length += gHUD.charWidths[ *next_line ]; + char *top = next_line; + if( *top == '\n' ) + *top = 0; + else + top = NULL; + + // find where to start drawing the line + int xpos = (SCREEN_WIDTH - line_length) / 2; + + gHUD.DrawHudString( xpos, ypos, SCREEN_WIDTH, ch, 255, 180, 0 ); + + ypos += LINE_HEIGHT; + + if( top ) *top = '\n'; // restore + + ch = next_line; + if( *ch == '\n' ) + ch++; + + if ( ypos > ( SCREEN_HEIGHT - 20 )) + break; // don't let it draw too low + } + return 1; +} + +int CHudMOTD :: MsgFunc_MOTD( const char *pszName, int iSize, void *pbuf ) +{ + if( m_iFlags & HUD_ACTIVE ) + { + Reset(); // clear the current MOTD in prep for this one + } + + BEGIN_READ( pszName, iSize, pbuf ); + + int is_finished = READ_BYTE(); + strcat( m_szMOTD, READ_STRING() ); + + if( is_finished ) + { + m_iFlags |= HUD_ACTIVE; + + MOTD_DISPLAY_TIME = CVAR_GET_FLOAT( "motd_display_time" ); + m_flActiveTill = gHUD.m_flTime + MOTD_DISPLAY_TIME; + + for( char *sz = m_szMOTD; *sz != 0; sz++ ) // count the number of lines in the MOTD + { + if( *sz == '\n' ) + m_iLines++; + } + } + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_msg.cpp b/client/hud/hud_msg.cpp new file mode 100644 index 00000000..bad1e7c6 --- /dev/null +++ b/client/hud/hud_msg.cpp @@ -0,0 +1,426 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// hud_msg.cpp +// +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +// CHud message handlers +DECLARE_HUDMESSAGE( HUDColor ); +DECLARE_HUDMESSAGE( SetFog ); +DECLARE_HUDMESSAGE( SetSky ); +DECLARE_HUDMESSAGE( RainData ); +DECLARE_HUDMESSAGE( SetBody ); +DECLARE_HUDMESSAGE( SetSkin ); +DECLARE_HUDMESSAGE( ResetHUD ); +DECLARE_HUDMESSAGE( InitHUD ); +DECLARE_HUDMESSAGE( ViewMode ); +DECLARE_HUDMESSAGE( Particle ); +DECLARE_HUDMESSAGE( SetFOV ); +DECLARE_HUDMESSAGE( Concuss ); +DECLARE_HUDMESSAGE( GameMode ); +DECLARE_HUDMESSAGE( CamData ); +DECLARE_HUDMESSAGE( AddMirror ); +DECLARE_HUDMESSAGE( AddScreen ); +DECLARE_HUDMESSAGE( AddPortal ); +DECLARE_HUDMESSAGE( ServerName ); +DECLARE_HUDMESSAGE( ScreenShake ); + +int CHud :: InitMessages( void ) +{ + HOOK_MESSAGE( ResetHUD ); + HOOK_MESSAGE( GameMode ); + HOOK_MESSAGE( ServerName ); + HOOK_MESSAGE( InitHUD ); + HOOK_MESSAGE( ViewMode ); + HOOK_MESSAGE( SetFOV ); + HOOK_MESSAGE( Concuss ); + HOOK_MESSAGE( HUDColor ); + HOOK_MESSAGE( Particle ); + HOOK_MESSAGE( SetFog ); + HOOK_MESSAGE( SetSky ); + HOOK_MESSAGE( CamData ); + HOOK_MESSAGE( RainData ); + HOOK_MESSAGE( SetBody ); + HOOK_MESSAGE( SetSkin ); + HOOK_MESSAGE( AddMirror); + HOOK_MESSAGE( AddScreen ); + HOOK_MESSAGE( AddPortal ); + + viewEntityIndex = 0; // trigger_viewset stuff + viewFlags = 0; + m_iFOV = 0; + m_iHUDColor = RGB_YELLOWISH; // 255,160,0 + + CVAR_REGISTER( "zoom_sensitivity_ratio", "1.2", 0, "mouse sensitivity when zooming" ); + CVAR_REGISTER( "default_fov", "90", 0, "default client fov" ); + CVAR_REGISTER( "hud_draw", "1", CVAR_ARCHIVE, "hud drawing modes" ); + + // clear any old HUD list + if( m_pHudList ) + { + HUDLIST *pList; + while( m_pHudList ) + { + pList = m_pHudList; + m_pHudList = m_pHudList->pNext; + FREE( pList ); + } + m_pHudList = NULL; + } + + m_flTime = 1.0; + + return 1; +} + +int CHud :: MsgFunc_ResetHUD(const char *pszName, int iSize, void *pbuf ) +{ + // clear all hud data + HUDLIST *pList = m_pHudList; + + while( pList ) + { + if( pList->p ) pList->p->Reset(); + pList = pList->pNext; + } + + // reset sensitivity + m_flMouseSensitivity = 0; + + // reset concussion effect + m_iConcussionEffect = 0; + + // reset fog + m_fStartDist = 0; + m_fEndDist = 0; + + return 1; +} + +int CHud :: MsgFunc_ViewMode( const char *pszName, int iSize, void *pbuf ) +{ + // CAM_ToFirstPerson(); + return 1; +} + +int CHud :: MsgFunc_InitHUD( const char *pszName, int iSize, void *pbuf ) +{ + m_fStartDist = 0; + m_fEndDist = 0; + m_iSkyMode = SKY_OFF; + + // prepare all hud data + HUDLIST *pList = m_pHudList; + + while (pList) + { + if ( pList->p ) pList->p->InitHUDData(); + pList = pList->pNext; + } + + return 1; +} + +int CHud::MsgFunc_SetFOV( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int newfov = READ_BYTE(); + int def_fov = CVAR_GET_FLOAT( "default_fov" ); + + if( newfov == 0 ) + { + m_iFOV = def_fov; + } + else + { + m_iFOV = newfov; + } + + if( m_iFOV == def_fov ) + { + m_flMouseSensitivity = 0; + } + else + { + // set a new sensitivity that is proportional to the change from the FOV default + m_flMouseSensitivity = CVAR_GET_FLOAT( "sensitivity" ) * ( (float)newfov / (float)def_fov ); + m_flMouseSensitivity *= CVAR_GET_FLOAT( "zoom_sensitivity_ratio" ); // apply zoom factor + } + END_READ(); + + return 1; +} + + +int CHud::MsgFunc_HUDColor(const char *pszName, int iSize, void *pbuf) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_iHUDColor = READ_LONG(); + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_SetFog( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_FogColor.x = (float)(READ_BYTE() / 255.0f); + m_FogColor.y = (float)(READ_BYTE() / 255.0f); + m_FogColor.z = (float)(READ_BYTE() / 255.0f); + + m_fStartDist = READ_SHORT(); + m_fEndDist = READ_SHORT(); + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_SetSky( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_iSkyMode = READ_BYTE(); + m_vecSkyPos[0] = READ_COORD(); + m_vecSkyPos[1] = READ_COORD(); + m_vecSkyPos[2] = READ_COORD(); + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_GameMode( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_Teamplay = READ_BYTE(); + + END_READ(); + + return 1; +} + + +int CHud :: MsgFunc_Damage( const char *pszName, int iSize, void *pbuf ) +{ + int armor, blood; + int i; + float count; + + BEGIN_READ( pszName, iSize, pbuf ); + armor = READ_BYTE(); + blood = READ_BYTE(); + + float from[3]; + for( i = 0; i < 3; i++ ) + from[i] = READ_COORD(); + + count = (blood * 0.5) + (armor * 0.5); + if( count < 10 ) count = 10; + + END_READ(); + + // TODO: kick viewangles, show damage visually + + return 1; +} + +int CHud :: MsgFunc_Concuss( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + m_iConcussionEffect = READ_BYTE(); + if( m_iConcussionEffect ) + m_StatusIcons.EnableIcon( "dmg_concuss", 255, 160, 0 ); + else m_StatusIcons.DisableIcon( "dmg_concuss" ); + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_CamData( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + gHUD.viewEntityIndex = READ_SHORT(); + gHUD.viewFlags = READ_SHORT(); + + if( gHUD.viewFlags ) + m_iCameraMode = 1; + else m_iCameraMode = m_iLastCameraMode; + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_RainData( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + Rain.dripsPerSecond = READ_SHORT(); + Rain.distFromPlayer = READ_COORD(); + Rain.windX = READ_COORD(); + Rain.windY = READ_COORD(); + Rain.randX = READ_COORD(); + Rain.randY = READ_COORD(); + Rain.weatherMode = READ_SHORT(); + Rain.globalHeight= READ_COORD(); // FIXME: calc on client side ? + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_SetBody( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + edict_t *viewmodel = GetViewModel(); + int body = READ_BYTE(); + + if( viewmodel ) + viewmodel->v.body = body; + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_SetSkin( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + edict_t *viewmodel = GetViewModel(); + int skin = READ_BYTE(); + + if( viewmodel ) + viewmodel->v.skin = skin; + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_AddScreen( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + // AddScreenBrushEntity( READ_BYTE() ); + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_AddMirror( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + // AddMirrorBrushEntity( READ_BYTE() ); + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_AddPortal( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + // AddPortalBrushEntity( READ_BYTE() ); + + END_READ(); + + return 1; +} + +int CHud :: MsgFunc_Particle( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int idx = READ_BYTE(); + char *sz = READ_STRING(); + // CreateAurora( idx, sz ); + + END_READ(); + + return 1; +} + +int CHud::MsgFunc_ServerName( const char *pszName, int iSize, void *pbuf ) +{ + char m_szServerName[32]; + + BEGIN_READ( pszName, iSize, pbuf ); + strncpy( m_szServerName, READ_STRING(), 32 ); + END_READ(); + + return 1; +} + +int CHud::MsgFunc_ScreenShake( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + ShakeCommand_t eCommand = (ShakeCommand_t)READ_BYTE(); + float amplitude = READ_FLOAT(); + float frequency = READ_FLOAT(); + float duration = READ_FLOAT(); + + if( eCommand == SHAKE_STOP ) + { + m_Shake.amplitude = 0; + m_Shake.frequency = 0; + m_Shake.duration = 0; + } + else + { + if(( eCommand == SHAKE_START) || ( eCommand == SHAKE_FREQUENCY )) + { + m_Shake.frequency = frequency; + } + + if(( eCommand == SHAKE_START) || ( eCommand == SHAKE_AMPLITUDE )) + { + // don't overwrite larger existing shake unless we are told to. + if(( amplitude > m_Shake.amplitude ) || ( eCommand == SHAKE_AMPLITUDE )) + { + m_Shake.amplitude = amplitude; + } + } + + // only reset the timer for a new shake. + if( eCommand == SHAKE_START ) + { + m_Shake.duration = duration; + m_Shake.nextShake = 0; + m_Shake.time = m_flTime + duration; + } + } + + END_READ(); + + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_saytext.cpp b/client/hud/hud_saytext.cpp new file mode 100644 index 00000000..c3ebc3e9 --- /dev/null +++ b/client/hud/hud_saytext.cpp @@ -0,0 +1,226 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// saytext.cpp +// +// implementation of CHudSayText class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +#define MAX_LINES 5 +#define MAX_CHARS_PER_LINE 256 /* it can be less than this, depending on char size */ + +// allow 20 pixels on either side of the text +#define MAX_LINE_WIDTH ( SCREEN_WIDTH - 40 ) +#define LINE_START 10 +static float SCROLL_SPEED = 5; + +static char g_szLineBuffer[ MAX_LINES + 1 ][ MAX_CHARS_PER_LINE ]; +static float flScrollTime = 0; // the time at which the lines next scroll up + +static int Y_START = 0; +static int line_height = 0; + +DECLARE_MESSAGE( m_SayText, SayText ); + +int CHudSayText :: Init( void ) +{ + gHUD.AddHudElem( this ); + + HOOK_MESSAGE( SayText ); + + InitHUDData(); + + CVAR_REGISTER( "hud_saytext_time", "5", 0, "time to show message text" ); + m_iFlags |= HUD_INTERMISSION; // is always drawn during an intermission + + return 1; +} + +void CHudSayText :: InitHUDData( void ) +{ + memset( g_szLineBuffer, 0, sizeof g_szLineBuffer ); +} + +int CHudSayText :: VidInit( void ) +{ + return 1; +} + + +void ScrollTextUp( void ) +{ + ConsolePrint( g_szLineBuffer[0] ); // move the first line into the console buffer + memmove( g_szLineBuffer[0], g_szLineBuffer[1], sizeof(g_szLineBuffer) - sizeof(g_szLineBuffer[0]) ); // overwrite the first line + + if( g_szLineBuffer[0][0] == ' ' ) // also scroll up following lines + { + g_szLineBuffer[0][0] = 2; + ScrollTextUp(); + } +} + +int CHudSayText :: Draw( float flTime ) +{ + int y = Y_START; + + // make sure the scrolltime is within reasonable bounds, to guard against the clock being reset + flScrollTime = min( flScrollTime, flTime + CVAR_GET_FLOAT( "hud_saytext_time" )); + + // make sure the scrolltime is within reasonable bounds, to guard against the clock being reset + flScrollTime = min( flScrollTime, flTime + CVAR_GET_FLOAT( "hud_saytext_time" )); + + if( flScrollTime <= flTime ) + { + if( *g_szLineBuffer[0] ) + { + flScrollTime = flTime + CVAR_GET_FLOAT( "hud_saytext_time" ); + // push the console up + ScrollTextUp(); + } + else + { + // buffer is empty, just disable drawing of this section + m_iFlags &= ~HUD_ACTIVE; + } + } + + for ( int i = 0; i < MAX_LINES; i++ ) + { + if ( *g_szLineBuffer[i] ) + DrawConsoleString( LINE_START, y, g_szLineBuffer[i] ); + + y += line_height; + } + return 1; +} + +int CHudSayText :: MsgFunc_SayText( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int client_index = READ_BYTE(); // the client who spoke the message + SayTextPrint( READ_STRING(), iSize - 1, client_index ); + END_READ(); + + return 1; +} + +void CHudSayText :: SayTextPrint( const char *pszBuf, int iBufSize, int clientIndex ) +{ + // find an empty string slot + for( int i = 0; i < MAX_LINES; i++ ) + { + if( !*g_szLineBuffer[i] ) + break; + } + + if( i == MAX_LINES ) + { + // force scroll buffer up + ScrollTextUp(); + i = MAX_LINES - 1; + } + + strncpy( g_szLineBuffer[i], pszBuf, max(iBufSize -1, MAX_CHARS_PER_LINE-1) ); + + // make sure the text fits in one line + EnsureTextFitsInOneLineAndWrapIfHaveTo( i ); + + // set scroll time + if( i == 0 ) + { + flScrollTime = gHUD.m_flTime + CVAR_GET_FLOAT( "hud_saytext_time" ); + } + + m_iFlags |= HUD_ACTIVE; + CL_PlaySound( "misc/talk.wav", 1.0f ); + + Y_START = SCREEN_HEIGHT - 60; + Y_START -= (line_height * (MAX_LINES+1)); + +} + +void CHudSayText :: EnsureTextFitsInOneLineAndWrapIfHaveTo( int line ) +{ + int line_width = 0; + GetConsoleStringSize( g_szLineBuffer[line], &line_width, &line_height ); + + if((line_width + LINE_START) > MAX_LINE_WIDTH ) + { + // string is too long to fit on line + // scan the string until we find what word is too long, and wrap the end of the sentence after the word + int length = LINE_START; + int tmp_len = 0; + char *last_break = NULL; + for ( char *x = g_szLineBuffer[line]; *x != 0; x++ ) + { + char buf[2]; + buf[1] = 0; + + if( *x == ' ' && x != g_szLineBuffer[line] ) // store each line break, except for the very first character + last_break = x; + + buf[0] = *x; // get the length of the current character + GetConsoleStringSize( buf, &tmp_len, &line_height ); + length += tmp_len; + + if( length > MAX_LINE_WIDTH ) + { + // needs to be broken up + if( !last_break ) + last_break = x-1; + x = last_break; + + // find an empty string slot + for( int j = 0; j < MAX_LINES; j++ ) + { + if( !*g_szLineBuffer[j] ) + break; + } + if( j == MAX_LINES ) + { + j = MAX_LINES - 1; + } + + // copy remaining string into next buffer, making sure it starts with a space character + if( (char)*last_break == (char)' ' ) + { + int linelen = strlen(g_szLineBuffer[j]); + int remaininglen = strlen(last_break); + + if( (linelen - remaininglen) <= MAX_CHARS_PER_LINE ) + strcat( g_szLineBuffer[j], last_break ); + } + else + { + if( (strlen(g_szLineBuffer[j]) - strlen(last_break) - 2) < MAX_CHARS_PER_LINE ) + { + strcat( g_szLineBuffer[j], " " ); + strcat( g_szLineBuffer[j], last_break ); + } + } + + *last_break = 0; // cut off the last string + + EnsureTextFitsInOneLineAndWrapIfHaveTo( j ); + break; + } + } + } +} \ No newline at end of file diff --git a/client/hud/hud_scoreboard.cpp b/client/hud/hud_scoreboard.cpp new file mode 100644 index 00000000..7591be9f --- /dev/null +++ b/client/hud/hud_scoreboard.cpp @@ -0,0 +1,505 @@ +/*** +* +* Copyright (c) 1999, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// Scoreboard.cpp +// +// implementation of CHudScoreboard class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_COMMAND( m_Scoreboard, ShowScores ); +DECLARE_COMMAND( m_Scoreboard, HideScores ); + +DECLARE_MESSAGE( m_Scoreboard, ScoreInfo ); +DECLARE_MESSAGE( m_Scoreboard, TeamInfo ); +DECLARE_MESSAGE( m_Scoreboard, TeamScore ); + +int CHudScoreboard :: Init( void ) +{ + gHUD.AddHudElem( this ); + + // Hook messages & commands here + HOOK_COMMAND( "+showscores", ShowScores ); + HOOK_COMMAND( "-showscores", HideScores ); + + HOOK_MESSAGE( ScoreInfo ); + HOOK_MESSAGE( TeamScore ); + HOOK_MESSAGE( TeamInfo ); + + InitHUDData(); + + return 1; +} + + +int CHudScoreboard :: VidInit( void ) +{ + // Load sprites here + + return 1; +} + +void CHudScoreboard :: InitHUDData( void ) +{ + memset( m_PlayerExtraInfo, 0, sizeof m_PlayerExtraInfo ); + m_iLastKilledBy = 0; + m_fLastKillTime = 0; + m_iPlayerNum = 0; + m_iNumTeams = 0; + memset( m_TeamInfo, 0, sizeof m_TeamInfo ); + + m_iFlags &= ~HUD_ACTIVE; // starts out inactive + + m_iFlags |= HUD_INTERMISSION; // is always drawn during an intermission +} + +/* The scoreboard +We have a minimum width of 1-320 - we could have the field widths scale with it? +*/ + +// X positions +// relative to the side of the scoreboard +#define NAME_RANGE_MIN 20 +#define NAME_RANGE_MAX 145 +#define KILLS_RANGE_MIN 130 +#define KILLS_RANGE_MAX 170 +#define DIVIDER_POS 180 +#define DEATHS_RANGE_MIN 185 +#define DEATHS_RANGE_MAX 210 +#define PING_RANGE_MIN 245 +#define PING_RANGE_MAX 295 + +#define SCOREBOARD_WIDTH 320 + +// Y positions +#define ROW_GAP 13 +#define ROW_RANGE_MIN 15 +#define ROW_RANGE_MAX ( SCREEN_HEIGHT - 50 ) + +int CHudScoreboard :: Draw( float fTime ) +{ + if( !m_iShowscoresHeld && gHUD.m_Health.m_iHealth > 0 && !gHUD.m_iIntermission ) + return 1; + + GetAllPlayersInfo(); + + // just sort the list on the fly + // list is sorted first by frags, then by deaths + float list_slot = 0; + int xpos_rel = (SCREEN_WIDTH - SCOREBOARD_WIDTH) / 2; + + // print the heading line + int ypos = ROW_RANGE_MIN + (list_slot * ROW_GAP); + int xpos = NAME_RANGE_MIN + xpos_rel; + + if ( !gHUD.m_Teamplay ) + gHUD.DrawHudString( xpos, ypos, NAME_RANGE_MAX + xpos_rel, "Player", 255, 140, 0 ); + else + gHUD.DrawHudString( xpos, ypos, NAME_RANGE_MAX + xpos_rel, "Teams", 255, 140, 0 ); + + gHUD.DrawHudStringReverse( KILLS_RANGE_MAX + xpos_rel, ypos, 0, "frags", 255, 140, 0 ); + gHUD.DrawHudString( DIVIDER_POS + xpos_rel, ypos, SCREEN_WIDTH, "/", 255, 140, 0 ); + gHUD.DrawHudString( DEATHS_RANGE_MIN + xpos_rel + 5, ypos, SCREEN_WIDTH, "deaths", 255, 140, 0 ); + gHUD.DrawHudString( PING_RANGE_MAX + xpos_rel - 35, ypos, SCREEN_WIDTH, "ping", 255, 140, 0 ); + + list_slot += 1.2; + ypos = ROW_RANGE_MIN + (list_slot * ROW_GAP); + xpos = NAME_RANGE_MIN + xpos_rel; + FillRGBA( xpos - 5, ypos, PING_RANGE_MAX - 5, 1, 255, 140, 0, 255); // draw the seperator line + + list_slot += 0.8; + + if ( !gHUD.m_Teamplay ) + { + // it's not teamplay, so just draw a simple player list + DrawPlayers( xpos_rel, list_slot ); + return 1; + } + + // clear out team scores + for ( int i = 1; i <= m_iNumTeams; i++ ) + { + if ( !m_TeamInfo[i].scores_overriden ) + m_TeamInfo[i].frags = m_TeamInfo[i].deaths = 0; + m_TeamInfo[i].ping = m_TeamInfo[i].packetloss = 0; + } + + // recalc the team scores, then draw them + for ( i = 1; i < MAX_PLAYERS; i++ ) + { + if ( m_PlayerInfoList[i].name == NULL ) + continue; // empty player slot, skip + + if ( m_PlayerExtraInfo[i].teamname[0] == 0 ) + continue; // skip over players who are not in a team + + // find what team this player is in + for ( int j = 1; j <= m_iNumTeams; j++ ) + { + if ( !stricmp( m_PlayerExtraInfo[i].teamname, m_TeamInfo[j].name ) ) + break; + } + if ( j > m_iNumTeams ) // player is not in a team, skip to the next guy + continue; + + if ( !m_TeamInfo[j].scores_overriden ) + { + m_TeamInfo[j].frags += m_PlayerExtraInfo[i].frags; + m_TeamInfo[j].deaths += m_PlayerExtraInfo[i].deaths; + } + + m_TeamInfo[j].ping += m_PlayerInfoList[i].ping; + m_TeamInfo[j].packetloss += m_PlayerInfoList[i].packetloss; + + if ( m_PlayerInfoList[i].thisplayer ) + m_TeamInfo[j].ownteam = TRUE; + else + m_TeamInfo[j].ownteam = FALSE; + } + + // find team ping/packetloss averages + for ( i = 1; i <= m_iNumTeams; i++ ) + { + m_TeamInfo[i].already_drawn = FALSE; + + if ( m_TeamInfo[i].players > 0 ) + { + m_TeamInfo[i].ping /= m_TeamInfo[i].players; // use the average ping of all the players in the team as the teams ping + m_TeamInfo[i].packetloss /= m_TeamInfo[i].players; // use the average ping of all the players in the team as the teams ping + } + } + + // Draw the teams + while ( 1 ) + { + int highest_frags = -99999; int lowest_deaths = 99999; + int best_team = 0; + + for ( i = 1; i <= m_iNumTeams; i++ ) + { + if ( m_TeamInfo[i].players < 0 ) + continue; + + if ( !m_TeamInfo[i].already_drawn && m_TeamInfo[i].frags >= highest_frags ) + { + if ( m_TeamInfo[i].frags > highest_frags || m_TeamInfo[i].deaths < lowest_deaths ) + { + best_team = i; + lowest_deaths = m_TeamInfo[i].deaths; + highest_frags = m_TeamInfo[i].frags; + } + } + } + + // draw the best team on the scoreboard + if ( !best_team ) + break; + + // draw out the best team + team_info_t *team_info = &m_TeamInfo[best_team]; + + ypos = ROW_RANGE_MIN + (list_slot * ROW_GAP); + + // check we haven't drawn too far down + if ( ypos > ROW_RANGE_MAX ) // don't draw to close to the lower border + break; + + xpos = NAME_RANGE_MIN + xpos_rel; + int r = 255, g = 225, b = 55; // draw the stuff kinda yellowish + + if ( team_info->ownteam ) // if it is their team, draw the background different color + { + // overlay the background in blue, then draw the score text over it + FillRGBA( NAME_RANGE_MIN + xpos_rel - 5, ypos, PING_RANGE_MAX - 5, ROW_GAP, 0, 0, 255, 70 ); + } + + // draw their name (left to right) + gHUD.DrawHudString( xpos, ypos, NAME_RANGE_MAX + xpos_rel, team_info->name, r, g, b ); + + // draw kills (right to left) + xpos = KILLS_RANGE_MAX + xpos_rel; + gHUD.DrawHudNumberString( xpos, ypos, KILLS_RANGE_MIN + xpos_rel, team_info->frags, r, g, b ); + + // draw divider + xpos = DIVIDER_POS + xpos_rel; + gHUD.DrawHudString( xpos, ypos, xpos + 20, "/", r, g, b ); + + // draw deaths + xpos = DEATHS_RANGE_MAX + xpos_rel; + gHUD.DrawHudNumberString( xpos, ypos, DEATHS_RANGE_MIN + xpos_rel, team_info->deaths, r, g, b ); + + // draw ping + // draw ping & packetloss + static char buf[64]; + sprintf( buf, "%d", team_info->ping ); + xpos = ((PING_RANGE_MAX - PING_RANGE_MIN) / 2) + PING_RANGE_MIN + xpos_rel + 25; + UnpackRGB( r, g, b, RGB_YELLOWISH ); + gHUD.DrawHudStringReverse( xpos, ypos, xpos - 50, buf, r, g, b ); + + team_info->already_drawn = TRUE; // set the already_drawn to be TRUE, so this team won't get drawn again + list_slot++; + + // draw all the players that belong to this team, indented slightly + list_slot = DrawPlayers( xpos_rel, list_slot, 10, team_info->name ); + } + + // draw all the players who are not in a team + list_slot += 0.5; + DrawPlayers( xpos_rel, list_slot, 0, "" ); + + return 1; +} + +// returns the ypos where it finishes drawing +int CHudScoreboard :: DrawPlayers( int xpos_rel, float list_slot, int nameoffset, char *team ) +{ + // draw the players, in order, and restricted to team if set + while ( 1 ) + { + // Find the top ranking player + int highest_frags = -99999; int lowest_deaths = 99999; + int best_player = 0; + + for ( int i = 1; i < MAX_PLAYERS; i++ ) + { + if ( m_PlayerInfoList[i].name && m_PlayerExtraInfo[i].frags >= highest_frags ) + { + if ( !(team && stricmp(m_PlayerExtraInfo[i].teamname, team)) ) // make sure it is the specified team + { + extra_player_info_t *pl_info = &m_PlayerExtraInfo[i]; + if ( pl_info->frags > highest_frags || pl_info->deaths < lowest_deaths ) + { + best_player = i; + lowest_deaths = pl_info->deaths; + highest_frags = pl_info->frags; + } + } + } + } + + if ( !best_player ) + break; + + // draw out the best player + hud_player_info_t *pl_info = &m_PlayerInfoList[best_player]; + + int ypos = ROW_RANGE_MIN + (list_slot * ROW_GAP); + + // check we haven't drawn too far down + if ( ypos > ROW_RANGE_MAX ) // don't draw to close to the lower border + break; + + int xpos = NAME_RANGE_MIN + xpos_rel; + int r = 255, g = 255, b = 255; + if ( best_player == m_iLastKilledBy && m_fLastKillTime && m_fLastKillTime > gHUD.m_flTime ) + { + if ( pl_info->thisplayer ) + { // green is the suicide color? i wish this could do grey... + FillRGBA( NAME_RANGE_MIN + xpos_rel - 5, ypos, PING_RANGE_MAX - 5, ROW_GAP, 80, 155, 0, 70 ); + } + else + { // Highlight the killers name - overlay the background in red, then draw the score text over it + FillRGBA( NAME_RANGE_MIN + xpos_rel - 5, ypos, PING_RANGE_MAX - 5, ROW_GAP, 255, 0, 0, ((float)15 * (float)(m_fLastKillTime - gHUD.m_flTime)) ); + } + } + else if ( pl_info->thisplayer ) // if it is their name, draw it a different color + { + // overlay the background in blue, then draw the score text over it + FillRGBA( NAME_RANGE_MIN + xpos_rel - 5, ypos, PING_RANGE_MAX - 5, ROW_GAP, 0, 0, 255, 70 ); + } + + // draw their name (left to right) + gHUD.DrawHudString( xpos + nameoffset, ypos, NAME_RANGE_MAX + xpos_rel, pl_info->name, r, g, b ); + + // draw kills (right to left) + xpos = KILLS_RANGE_MAX + xpos_rel; + gHUD.DrawHudNumberString( xpos, ypos, KILLS_RANGE_MIN + xpos_rel, m_PlayerExtraInfo[best_player].frags, r, g, b ); + + // draw divider + xpos = DIVIDER_POS + xpos_rel; + gHUD.DrawHudString( xpos, ypos, xpos + 20, "/", r, g, b ); + + // draw deaths + xpos = DEATHS_RANGE_MAX + xpos_rel; + gHUD.DrawHudNumberString( xpos, ypos, DEATHS_RANGE_MIN + xpos_rel, m_PlayerExtraInfo[best_player].deaths, r, g, b ); + + // draw ping & packetloss + static char buf[64]; + sprintf( buf, "%d", m_PlayerInfoList[best_player].ping ); + xpos = ((PING_RANGE_MAX - PING_RANGE_MIN) / 2) + PING_RANGE_MIN + xpos_rel + 25; + gHUD.DrawHudStringReverse( xpos, ypos, xpos - 50, buf, r, g, b ); + + pl_info->name = NULL; // set the name to be NULL, so this client won't get drawn again + list_slot++; + } + return list_slot; +} + +void CHudScoreboard :: GetAllPlayersInfo( void ) +{ + for ( int i = 1; i < MAX_PLAYERS; i++ ) + { + GetPlayerInfo( i, &m_PlayerInfoList[i] ); + + if ( m_PlayerInfoList[i].thisplayer ) + m_iPlayerNum = i; // !!!HACK: this should be initialized elsewhere... maybe gotten from the engine + } +} + +int CHudScoreboard :: MsgFunc_ScoreInfo( const char *pszName, int iSize, void *pbuf ) +{ + m_iFlags |= HUD_ACTIVE; + + BEGIN_READ( pszName, iSize, pbuf ); + short cl = READ_BYTE(); + short frags = READ_SHORT(); + short deaths = READ_SHORT(); + + if( cl > 0 && cl <= MAX_PLAYERS ) + { + m_PlayerExtraInfo[cl].frags = frags; + m_PlayerExtraInfo[cl].deaths = deaths; + } + END_READ(); + + return 1; +} + +// Message handler for TeamInfo message +// accepts two values: +// byte: client number +// string: client team name +int CHudScoreboard :: MsgFunc_TeamInfo( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + short cl = READ_BYTE(); + + if( cl > 0 && cl <= MAX_PLAYERS ) + { + // set the players team + strncpy( m_PlayerExtraInfo[cl].teamname, READ_STRING(), MAX_TEAM_NAME ); + } + + // rebuild the list of teams + + // clear out player counts from teams + for ( int i = 1; i <= m_iNumTeams; i++ ) + { + m_TeamInfo[i].players = 0; + } + + // rebuild the team list + GetAllPlayersInfo(); + m_iNumTeams = 0; + for ( i = 1; i < MAX_PLAYERS; i++ ) + { + if ( m_PlayerInfoList[i].name == NULL ) + continue; + + if ( m_PlayerExtraInfo[i].teamname[0] == 0 ) + continue; // skip over players who are not in a team + + // is this player in an existing team? + for ( int j = 1; j <= m_iNumTeams; j++ ) + { + if ( m_TeamInfo[j].name[0] == '\0' ) + break; + + if ( !stricmp( m_PlayerExtraInfo[i].teamname, m_TeamInfo[j].name ) ) + break; + } + + if ( j > m_iNumTeams ) + { // they aren't in a listed team, so make a new one + // search through for an empty team slot + for ( int j = 1; j <= m_iNumTeams; j++ ) + { + if ( m_TeamInfo[j].name[0] == '\0' ) + break; + } + m_iNumTeams = max( j, m_iNumTeams ); + + strncpy( m_TeamInfo[j].name, m_PlayerExtraInfo[i].teamname, MAX_TEAM_NAME ); + m_TeamInfo[j].players = 0; + } + + m_TeamInfo[j].players++; + } + + // clear out any empty teams + for ( i = 1; i <= m_iNumTeams; i++ ) + { + if ( m_TeamInfo[i].players < 1 ) + memset( &m_TeamInfo[i], 0, sizeof(team_info_t) ); + } + END_READ(); + + return 1; +} + +// Message handler for TeamScore message +// accepts three values: +// string: team name +// short: teams kills +// short: teams deaths +// if this message is never received, then scores will simply be the combined totals of the players. +int CHudScoreboard :: MsgFunc_TeamScore( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + char *TeamName = READ_STRING(); + + // find the team matching the name + for ( int i = 1; i <= m_iNumTeams; i++ ) + { + if ( !stricmp( TeamName, m_TeamInfo[i].name ) ) + break; + } + if ( i > m_iNumTeams ) + return 1; + + // use this new score data instead of combined player scores + m_TeamInfo[i].scores_overriden = TRUE; + m_TeamInfo[i].frags = READ_SHORT(); + m_TeamInfo[i].deaths = READ_SHORT(); + END_READ(); + + return 1; +} + +void CHudScoreboard :: DeathMsg( int killer, int victim ) +{ + // if we were the one killed, or the world killed us, set the scoreboard to indicate suicide + if ( victim == m_iPlayerNum || killer == 0 ) + { + m_iLastKilledBy = killer ? killer : m_iPlayerNum; + m_fLastKillTime = gHUD.m_flTime + 10; // display who we were killed by for 10 seconds + + if ( killer == m_iPlayerNum ) + m_iLastKilledBy = m_iPlayerNum; + } +} + +void CHudScoreboard :: UserCmd_ShowScores( void ) +{ + m_iShowscoresHeld = TRUE; +} + +void CHudScoreboard :: UserCmd_HideScores( void ) +{ + m_iShowscoresHeld = FALSE; +} diff --git a/client/hud/hud_sound.cpp b/client/hud/hud_sound.cpp new file mode 100644 index 00000000..f1113661 --- /dev/null +++ b/client/hud/hud_sound.cpp @@ -0,0 +1,357 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +/* +==================== +FMOD definitions + +==================== +*/ +enum FSOUND_OUTPUTTYPES +{ + FSOUND_OUTPUT_NOSOUND, /* NoSound driver, all calls to this succeed but do nothing. */ + FSOUND_OUTPUT_WINMM, /* Windows Multimedia driver. */ + FSOUND_OUTPUT_DSOUND, /* DirectSound driver. You need this to get EAX or EAX2 support. */ + FSOUND_OUTPUT_A3D, /* A3D driver. You need this to get geometry support. */ + FSOUND_OUTPUT_OSS, /* Linux/Unix OSS (Open Sound System) driver, i.e. the kernel sound drivers. */ + FSOUND_OUTPUT_ESD, /* Linux/Unix ESD (Enlightment Sound Daemon) driver. */ + FSOUND_OUTPUT_ALSA /* Linux Alsa driver. */ +}; + +enum FMOD_ERRORS +{ + FMOD_ERR_NONE, /* No errors */ + FMOD_ERR_BUSY, /* Cannot call this command after FSOUND_Init. Call FSOUND_Close first. */ + FMOD_ERR_UNINITIALIZED, /* This command failed because FSOUND_Init or FSOUND_SetOutput was not called */ + FMOD_ERR_INIT, /* Error initializing output device. */ + FMOD_ERR_ALLOCATED, /* Error initializing output device, but more specifically, the output device is already in use and cannot be reused. */ + FMOD_ERR_PLAY, /* Playing the sound failed. */ + FMOD_ERR_OUTPUT_FORMAT, /* Soundcard does not support the features needed for this soundsystem (16bit stereo output) */ + FMOD_ERR_COOPERATIVELEVEL, /* Error setting cooperative level for hardware. */ + FMOD_ERR_CREATEBUFFER, /* Error creating hardware sound buffer. */ + FMOD_ERR_FILE_NOTFOUND, /* File not found */ + FMOD_ERR_FILE_FORMAT, /* Unknown file format */ + FMOD_ERR_FILE_BAD, /* Error loading file */ + FMOD_ERR_MEMORY, /* Not enough memory or resources */ + FMOD_ERR_VERSION, /* The version number of this file format is not supported */ + FMOD_ERR_INVALID_PARAM, /* An invalid parameter was passed to this function */ + FMOD_ERR_NO_EAX, /* Tried to use an EAX command on a non EAX enabled channel or output. */ + FMOD_ERR_CHANNEL_ALLOC, /* Failed to allocate a new channel */ + FMOD_ERR_RECORD, /* Recording is not supported on this machine */ + FMOD_ERR_MEDIAPLAYER, /* Windows Media Player not installed so cannot play wma or use internet streaming. */ + FMOD_ERR_CDDEVICE /* An error occured trying to open the specified CD device */ +}; + +static char *FMOD_ErrorString( int errcode ) +{ + switch (errcode) + { + case FMOD_ERR_NONE: return "No errors"; + case FMOD_ERR_BUSY: return "Cannot call this command after FSOUND_Init. Call FSOUND_Close first."; + case FMOD_ERR_UNINITIALIZED: return "This command failed because FSOUND_Init was not called"; + case FMOD_ERR_PLAY: return "Playing the sound failed."; + case FMOD_ERR_INIT: return "Error initializing output device."; + case FMOD_ERR_ALLOCATED: return "The output device is already in use and cannot be reused."; + case FMOD_ERR_OUTPUT_FORMAT: return "Soundcard does not support the features needed for this soundsystem (16bit stereo output)"; + case FMOD_ERR_COOPERATIVELEVEL: return "Error setting cooperative level for hardware."; + case FMOD_ERR_CREATEBUFFER: return "Error creating hardware sound buffer."; + case FMOD_ERR_FILE_NOTFOUND: return "File not found"; + case FMOD_ERR_FILE_FORMAT: return "Unknown file format"; + case FMOD_ERR_FILE_BAD: return "Error loading file"; + case FMOD_ERR_MEMORY: return "Not enough memory "; + case FMOD_ERR_VERSION: return "The version number of this file format is not supported"; + case FMOD_ERR_INVALID_PARAM: return "An invalid parameter was passed to this function"; + case FMOD_ERR_NO_EAX: return "Tried to use an EAX command on a non EAX enabled channel or output."; + case FMOD_ERR_CHANNEL_ALLOC: return "Failed to allocate a new channel"; + case FMOD_ERR_RECORD: return "Recording not supported on this device"; + case FMOD_ERR_MEDIAPLAYER: return "Required Mediaplayer codec is not installed"; + default : return "Unknown error"; + }; +} + +#define FSOUND_LOOP_OFF 0x00000001 +#define FSOUND_LOOP_NORMAL 0x00000002 +#define FSOUND_LOADMEMORY 0x00008000 +#define FSOUND_MPEGACCURATE 0x00020000 + +enum FSOUND_MIXERTYPES +{ + FSOUND_MIXER_AUTODETECT, /* CE/PS2/GC Only - Non interpolating/low quality mixer. */ + FSOUND_MIXER_BLENDMODE, /* Removed / obsolete. */ + FSOUND_MIXER_MMXP5, /* Removed / obsolete. */ + FSOUND_MIXER_MMXP6, /* Removed / obsolete. */ + + FSOUND_MIXER_QUALITY_AUTODETECT,/* All platforms - Autodetect the fastest quality mixer based on your cpu. */ + FSOUND_MIXER_QUALITY_FPU, /* Win32/Linux only - Interpolating/volume ramping FPU mixer. */ + FSOUND_MIXER_QUALITY_MMXP5, /* Win32/Linux only - Interpolating/volume ramping P5 MMX mixer. */ + FSOUND_MIXER_QUALITY_MMXP6, /* Win32/Linux only - Interpolating/volume ramping ppro+ MMX mixer. */ + + FSOUND_MIXER_MONO, /* CE/PS2/GC only - MONO non interpolating/low quality mixer. For speed*/ + FSOUND_MIXER_QUALITY_MONO, /* CE/PS2/GC only - MONO Interpolating mixer. For speed */ + + FSOUND_MIXER_MAX +}; + +/* +==================== +Cl definitions + +==================== +*/ + +static int (_stdcall *qfmod_geterror)(); +static float (_stdcall *qfmod_getversion)(); +static signed char (_stdcall *qfmod_setoutput)(int outputtype); +static signed char (_stdcall *qfmod_setdriver)(int driver); +static signed char (_stdcall *qfmod_setmixer)(int mixer); +static signed char (_stdcall *qfmod_setbuffersize)(int len_ms); +static signed char (_stdcall *qfmod_init)(int mixrate, int maxsoftwarechannels, unsigned int flags); +static void (_stdcall *qfmod_close)(); +static int (_stdcall *qfmod_getmixer)(); +static signed char (_stdcall *qfmod_freesong)(void *mod); +static signed char (_stdcall *qfmod_playsong)(void *mod); +static signed char (_stdcall *qfmod_stopsong)(void *mod); +static void (_stdcall *qfmod_stopallsongs)(); +static void* (_stdcall *qfmod_loadsong)(const char *name); +static void* (_stdcall *qfmod_loadsongmemory)(void *data, int length); +static signed char (_stdcall *qfmod_setmodpause)(void *data, signed char pause); +static signed char (_stdcall *qfmod_getmodpause)(void *data); +static void* (_stdcall *qfmod_loadstream)(const char *data, unsigned int mode, int memlength); +static int (_stdcall *qfmod_playstream)(int channel, void *data); +static signed char (_stdcall *qfmod_freestream)(void *data); +static signed char (_stdcall *qfmod_getstreampause)(int channel); +static int (_stdcall *qfmod_getstreampos)(void *data); +static signed char (_stdcall *qfmod_setstreampause)(int channel, signed char paused); +static signed char (_stdcall *qfmod_setstreampos)(void *data, unsigned int pos); +static signed char (_stdcall *qfmod_stopstream)(void *data); +static signed char (_stdcall *qfmod_setvolume)(int channel, int vol); + + +static dllfunction_t fmodfuncs[] = +{ + {"_FSOUND_GetError@0", (void **) &qfmod_geterror}, + {"_FSOUND_GetVersion@0", (void **) &qfmod_getversion}, + {"_FSOUND_SetOutput@4", (void **) &qfmod_setoutput}, + {"_FSOUND_SetDriver@4", (void **) &qfmod_setdriver}, + {"_FSOUND_SetMixer@4", (void **) &qfmod_setmixer}, + {"_FSOUND_SetBufferSize@4", (void **) &qfmod_setbuffersize}, + {"_FSOUND_Init@12", (void **) &qfmod_init}, + {"_FSOUND_Close@0", (void **) &qfmod_close}, + {"_FSOUND_GetMixer@0", (void **) &qfmod_getmixer}, + {"_FMUSIC_FreeSong@4", (void **) &qfmod_freesong}, + {"_FMUSIC_PlaySong@4", (void **) &qfmod_playsong}, + {"_FMUSIC_StopSong@4", (void **) &qfmod_stopsong}, + {"_FMUSIC_StopAllSongs@0", (void **) &qfmod_stopallsongs}, + {"_FMUSIC_LoadSong@4", (void **) &qfmod_loadsong}, + {"_FMUSIC_LoadSongMemory@8", (void **) &qfmod_loadsongmemory}, + {"_FMUSIC_SetPaused@8", (void **) &qfmod_setmodpause}, + {"_FMUSIC_GetPaused@4", (void **) &qfmod_getmodpause}, + {"_FSOUND_Stream_OpenFile@12",(void **) &qfmod_loadstream}, + {"_FSOUND_Stream_Play@8", (void **) &qfmod_playstream}, + {"_FSOUND_Stream_Close@4", (void **) &qfmod_freestream}, + {"_FSOUND_GetPaused@4", (void **) &qfmod_getstreampause}, + {"_FSOUND_Stream_GetPosition@4",(void **) &qfmod_getstreampos}, + {"_FSOUND_SetPaused@8", (void **) &qfmod_setstreampause}, + {"_FSOUND_Stream_SetPosition@8",(void **) &qfmod_setstreampos}, + {"_FSOUND_Stream_Stop@4", (void **) &qfmod_stopstream}, + {"_FSOUND_SetVolume@8", (void **) &qfmod_setvolume}, + {NULL, NULL} +}; + +DECLARE_MESSAGE(m_Sound, Fsound) +#define STREAM 1 +#define TRACK 2 + +// Handle for fmod +static dllhandle_t fmod_dll = NULL; +void *fmod_data = NULL; +static char songname[256]; +int last_state = 0; + +int CheckFormat( BOOL skip_buffer ) +{ + int i; + + // check buffer + if( !fmod_data && !skip_buffer ) return false; + + // detect of music type + for( i = 0; songname[i]; i++ ) + { + if( songname[i] == '.' )//found extension + { + if( !strncmp( &songname[i+1], "mp3", 3 )) return STREAM; + else if( !strncmp(&songname[i+1], "wma", 3 )) return STREAM; + else if( !strncmp(&songname[i+1], "ogg", 3 )) return STREAM; + else if( !strncmp(&songname[i+1], "xm", 2 )) return TRACK; + else if( !strncmp(&songname[i+1], "it", 2 )) return TRACK; + else if( !strncmp(&songname[i+1], "s3m", 3 )) return TRACK; + else return false; + } + } + return false; +} + +int CHudSound :: Init( void ) +{ + m_iStatus = 0; + m_iTime = 0; + + HOOK_MESSAGE( Fsound ); + + m_iFlags |= HUD_ACTIVE; + gHUD.AddHudElem( this ); + + // already loaded? + if( fmod_dll ) return 1; + + if( Sys_LoadLibrary( "fmod.dll", &fmod_dll, fmodfuncs )) + { + if( qfmod_getversion() > 3.4f && qfmod_getversion() < 3.3f ) + { + Sys_UnloadLibrary( &fmod_dll ); //free library + ALERT( at_warning, "Invalid fmod version: %g\n", qfmod_getversion()); + return 1; + } + + qfmod_setbuffersize( 100 ); + qfmod_setoutput( FSOUND_OUTPUT_DSOUND ); + qfmod_setdriver( 0 ); + if( !qfmod_init( 48000, 32, 0 )) + { + ALERT( at_error, "%s\n", FMOD_ErrorString( qfmod_geterror() )); + return 1; + } + qfmod_setmixer( FSOUND_MIXER_AUTODETECT ); + } + else ALERT( at_error, "fmod.dll not installed\n" ); + + return 1; +} + +int CHudSound :: VidInit( void ) +{ + // MsgFunc_Fsound( 0, 0, NULL ); + if( fmod_dll ) qfmod_stopallsongs(); // stop all songs + return 1; +} + +int CHudSound :: MsgFunc_Fsound( const char *pszName, int iSize, void *pbuf ) +{ + if( !fmod_dll ) return 1; + + BEGIN_READ( pszName, iSize, pbuf ); + + strcpy( songname, READ_STRING( )); // songname + m_iTime = READ_SHORT(); // song position + m_iStatus = READ_BYTE(); + + if( m_iStatus & 1 ) PlayStream( songname ); + else if( fmod_data ) + { + if( CheckFormat( FALSE ) == TRACK ) qfmod_freesong( fmod_data ); + else if( CheckFormat( FALSE ) == STREAM ) qfmod_freestream( fmod_data ); + memset( fmod_data, 0, sizeof( fmod_data )); + memset( (char*)songname, 0, sizeof( songname )); + m_iTime = 0; + m_iStatus = 0; + } + END_READ(); + + return 1; +} + +int CHudSound :: PlayStream( const char* name ) +{ + int filesize; + char *data; + + if( !fmod_dll ) return false; + + // Load the file + data = (char *)LOAD_FILE( name, &filesize ); + + if( !data ) + { + ALERT( at_console, "couldn't load %s\n", name ); + return false; + } + + // try to open this file as stream default + int flags; + if( m_iStatus & 2 ) flags = FSOUND_LOADMEMORY|FSOUND_MPEGACCURATE|FSOUND_LOOP_NORMAL; + else flags = FSOUND_LOADMEMORY|FSOUND_MPEGACCURATE|FSOUND_LOOP_OFF; + fmod_data = qfmod_loadstream( data, flags, filesize ); + + if( !fmod_data ) // may be it's tracker? + { + // check for .xm .it .s3m headers + if( memcmp( data, "Extended Module:", 16 ) && memcmp( data, "IMPM", 4 ) && memcmp( data + 44, "SCRM", 4 )) + { + for( int i = 0; name[i]; i++ ) + { + if( name[i] == '.' ) // found extension + { + ALERT( at_console, "%s is not a %s file\n", name, &name[i+1] ); + break; + } + } + FREE_FILE( data ); + return 1; + } + + // it's tracker, try to load + fmod_data = qfmod_loadsongmemory( data, filesize ); + + if( !fmod_data ) // what's hell? + { + ALERT( at_console, "%s\n", FMOD_ErrorString(qfmod_geterror())); + FREE_FILE( data ); + return 1; + } + + qfmod_playsong( fmod_data ); + FREE_FILE( data ); + return 1; + } + + qfmod_playstream( 0, fmod_data ); + FREE_FILE( data ); + m_iStatus = 1; + return 1; +} + +int CHudSound :: Draw( float flTime ) +{ + int pause = CVAR_GET_FLOAT( "paused" ); // engine cvar + + if( fmod_dll && fmod_data ) + { + if( last_state != pause ) + { + // detect of music type + if( CheckFormat( FALSE ) == TRACK ) + qfmod_setmodpause( fmod_data, !qfmod_getmodpause( fmod_data )); + else if( CheckFormat( FALSE ) == STREAM ) + qfmod_setstreampause( 0, !qfmod_getstreampause( 0 )); + last_state = pause; + } + } + return 1; +} + +int CHudSound :: Close( void ) +{ + if( fmod_dll ) + { + qfmod_stopallsongs(); // stop all songs + qfmod_close(); + Sys_UnloadLibrary( &fmod_dll ); // free library + } + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_statusbar.cpp b/client/hud/hud_statusbar.cpp new file mode 100644 index 00000000..52448685 --- /dev/null +++ b/client/hud/hud_statusbar.cpp @@ -0,0 +1,246 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// statusbar.cpp +// +// generic text status bar, set by game dll +// runs across bottom of screen +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_StatusBar, StatusText ); +DECLARE_MESSAGE( m_StatusBar, StatusValue ); + +#define STATUSBAR_ID_LINE 1 + +int CHudStatusBar :: Init( void ) +{ + gHUD.AddHudElem( this ); + + HOOK_MESSAGE( StatusText ); + HOOK_MESSAGE( StatusValue ); + + Reset(); + + CVAR_REGISTER( "hud_centerid", "0", CVAR_ARCHIVE, "disables center id" ); + + return 1; +} + +int CHudStatusBar :: VidInit( void ) +{ + // Load sprites here + + return 1; +} + +void CHudStatusBar :: Reset( void ) +{ + int i = 0; + + m_iFlags &= ~HUD_ACTIVE; // start out inactive + for ( i = 0; i < MAX_STATUSBAR_LINES; i++ ) + m_szStatusText[i][0] = 0; + memset( m_iStatusValues, 0, sizeof m_iStatusValues ); + + m_iStatusValues[0] = 1; // 0 is the special index, which always returns true +} + +void CHudStatusBar :: ParseStatusString( int line_num ) +{ + // localise string first + char szBuffer[MAX_STATUSTEXT_LENGTH]; + memset( szBuffer, 0, sizeof szBuffer ); + gHUD.m_TextMessage.LocaliseTextString( m_szStatusText[line_num], szBuffer, MAX_STATUSTEXT_LENGTH ); + + // parse m_szStatusText & m_iStatusValues into m_szStatusBar + memset( m_szStatusBar[line_num], 0, MAX_STATUSTEXT_LENGTH ); + char *src = szBuffer; + char *dst = m_szStatusBar[line_num]; + + char *src_start = src, *dst_start = dst; + + while ( *src != 0 ) + { + while ( *src == '\n' ) + src++; // skip over any newlines + + if ( ((src - src_start) >= MAX_STATUSTEXT_LENGTH) || ((dst - dst_start) >= MAX_STATUSTEXT_LENGTH) ) + break; + + int index = atoi( src ); + // should we draw this line? + if ( (index >= 0 && index < MAX_STATUSBAR_VALUES) && (m_iStatusValues[index] != 0) ) + { // parse this line and append result to the status bar + while ( *src >= '0' && *src <= '9' ) + src++; + + if ( *src == '\n' || *src == 0 ) + continue; // no more left in this text line + + // copy the text, char by char, until we hit a % or a \n + while ( *src != '\n' && *src != 0 ) + { + if ( *src != '%' ) + { // just copy the character + *dst = *src; + dst++, src++; + } + else + { + // get the descriptor + char valtype = *(++src); // move over % + + // if it's a %, draw a % sign + if ( valtype == '%' ) + { + *dst = valtype; + dst++, src++; + continue; + } + + // move over descriptor, then get and move over the index + index = atoi( ++src ); + while ( *src >= '0' && *src <= '9' ) + src++; + + if ( index >= 0 && index < MAX_STATUSBAR_VALUES ) + { + int indexval = m_iStatusValues[index]; + + // get the string to substitute in place of the %XX + char szRepString[MAX_PLAYER_NAME_LENGTH]; + switch ( valtype ) + { + case 'p': // player name + GetPlayerInfo( indexval, &gHUD.m_Scoreboard.m_PlayerInfoList[indexval] ); + if ( gHUD.m_Scoreboard.m_PlayerInfoList[indexval].name != NULL ) + { + strncpy( szRepString, gHUD.m_Scoreboard.m_PlayerInfoList[indexval].name, MAX_PLAYER_NAME_LENGTH ); + } + else + { + strcpy( szRepString, "******" ); + } + break; + case 'i': // number + sprintf( szRepString, "%d", indexval ); + break; + default: + szRepString[0] = 0; + } + + for ( char *cp = szRepString; *cp != 0 && ((dst - dst_start) < MAX_STATUSTEXT_LENGTH); cp++, dst++ ) + *dst = *cp; + } + } + } + } + else + { + // skip to next line of text + while ( *src != 0 && *src != '\n' ) + src++; + } + } +} + +int CHudStatusBar :: Draw( float fTime ) +{ + if ( m_bReparseString ) + { + for ( int i = 0; i < MAX_STATUSBAR_LINES; i++ ) + ParseStatusString( i ); + m_bReparseString = FALSE; + } + + // Draw the status bar lines + for ( int i = 0; i < MAX_STATUSBAR_LINES; i++ ) + { + int TextHeight, TextWidth; + GetConsoleStringSize( m_szStatusBar[i], &TextWidth, &TextHeight ); + + int Y_START = SCREEN_HEIGHT - 45; + int x = 4; + int y = Y_START - ( 4 + TextHeight * i ); // draw along bottom of screen + + // let user set status ID bar centering + if ( (i == STATUSBAR_ID_LINE) && CVAR_GET_FLOAT( "hud_centerid" )) + { + x = max( 0, max( 2, (SCREEN_WIDTH - TextWidth)) / 2 ); + y = (SCREEN_HEIGHT / 2) + (TextHeight * CVAR_GET_FLOAT( "hud_centerid" )); + } + DrawConsoleString( x, y, m_szStatusBar[i] ); + } + + return 1; +} + +// Message handler for StatusText message +// accepts two values: +// byte: line number of status bar text +// string: status bar text +// this string describes how the status bar should be drawn +// a semi-regular expression: +// ( slotnum ([a..z] [%pX] [%iX])*)* +// where slotnum is an index into the Value table (see below) +// if slotnum is 0, the string is always drawn +// if StatusValue[slotnum] != 0, the following string is drawn, upto the next newline - otherwise the text is skipped upto next newline +// %pX, where X is an integer, will substitute a player name here, getting the player index from StatusValue[X] +// %iX, where X is an integer, will substitute a number here, getting the number from StatusValue[X] + +int CHudStatusBar :: MsgFunc_StatusText( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int line = READ_BYTE(); + + if ( line < 0 || line >= MAX_STATUSBAR_LINES ) + return 1; + + strncpy( m_szStatusText[line], READ_STRING(), MAX_STATUSTEXT_LENGTH ); + m_szStatusText[line][MAX_STATUSTEXT_LENGTH-1] = 0; // ensure it's null terminated ( strncpy() won't null terminate if read string too long) + + if( m_szStatusText[0] == 0 ) + m_iFlags &= ~HUD_ACTIVE; + else m_iFlags |= HUD_ACTIVE; // we have status text, so turn on the status bar + + m_bReparseString = TRUE; + END_READ(); + + return 1; +} + +// Message handler for StatusText message +// accepts two values: +// byte: index into the status value array +// short: value to store +int CHudStatusBar :: MsgFunc_StatusValue( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int index = READ_BYTE(); + if( index < 1 || index >= MAX_STATUSBAR_VALUES ) + return 1; // index out of range + + m_iStatusValues[index] = READ_SHORT(); + m_bReparseString = TRUE; + END_READ(); + + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_text.cpp b/client/hud/hud_text.cpp new file mode 100644 index 00000000..d58e64fb --- /dev/null +++ b/client/hud/hud_text.cpp @@ -0,0 +1,204 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// text_message.cpp +// +// implementation of CHudTextMessage class +// +// this class routes messages through titles.txt for localisation +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_TextMessage, TextMsg ); + +int CHudTextMessage::Init(void) +{ + HOOK_MESSAGE( TextMsg ); + + gHUD.AddHudElem( this ); + + Reset(); + + return 1; +}; + +// Searches through the string for any msg names (indicated by a '#') +// any found are looked up in titles.txt and the new message substituted +// the new value is pushed into dst_buffer +char *CHudTextMessage::LocaliseTextString( const char *msg, char *dst_buffer, int buffer_size ) +{ + char *dst = dst_buffer; + for ( char *src = (char*)msg; *src != 0 && buffer_size > 0; buffer_size-- ) + { + if ( *src == '#' ) + { + // cut msg name out of string + static char word_buf[255]; + char *wdst = word_buf, *word_start = src; + for ( ++src ; (*src >= 'A' && *src <= 'z') || (*src >= '0' && *src <= '9'); wdst++, src++ ) + { + *wdst = *src; + } + *wdst = 0; + + // lookup msg name in titles.txt + client_textmessage_t *clmsg = TextMessageGet( word_buf ); + if ( !clmsg || !(clmsg->pMessage) ) + { + src = word_start; + *dst = *src; + dst++, src++; + continue; + } + + // copy string into message over the msg name + for ( char *wsrc = (char*)clmsg->pMessage; *wsrc != 0; wsrc++, dst++ ) + { + *dst = *wsrc; + } + *dst = 0; + } + else + { + *dst = *src; + dst++, src++; + *dst = 0; + } + } + + dst_buffer[buffer_size-1] = 0; // ensure null termination + return dst_buffer; +} + +// As above, but with a local static buffer +char *CHudTextMessage::BufferedLocaliseTextString( const char *msg ) +{ + static char dst_buffer[1024]; + LocaliseTextString( msg, dst_buffer, 1024 ); + return dst_buffer; +} + +// Simplified version of LocaliseTextString; assumes string is only one word +char *CHudTextMessage::LookupString( const char *msg, int *msg_dest ) +{ + if ( !msg ) + return ""; + + // '#' character indicates this is a reference to a string in titles.txt, and not the string itself + if ( msg[0] == '#' ) + { + // this is a message name, so look up the real message + client_textmessage_t *clmsg = TextMessageGet( msg+1 ); + + if ( !clmsg || !(clmsg->pMessage) ) + return (char*)msg; // lookup failed, so return the original string + + if ( msg_dest ) + { + // check to see if titles.txt info overrides msg destination + // if clmsg->effect is less than 0, then clmsg->effect holds -1 * message_destination + if ( clmsg->effect < 0 ) // + *msg_dest = -clmsg->effect; + } + + return (char*)clmsg->pMessage; + } + else + { // nothing special about this message, so just return the same string + return (char*)msg; + } +} + +void StripEndNewlineFromString( char *str ) +{ + int s = strlen( str ) - 1; + if ( str[s] == '\n' || str[s] == '\r' ) + str[s] = 0; +} + +// converts all '\r' characters to '\n', so that the engine can deal with the properly +// returns a pointer to str +char* ConvertCRtoNL( char *str ) +{ + for ( char *ch = str; *ch != 0; ch++ ) + if ( *ch == '\r' ) + *ch = '\n'; + return str; +} + +// Message handler for text messages +// displays a string, looking them up from the titles.txt file, which can be localised +// parameters: +// byte: message direction ( HUD_PRINTCONSOLE, HUD_PRINTNOTIFY, HUD_PRINTCENTER, HUD_PRINTTALK ) +// string: message +// optional parameters: +// string: message parameter 1 +// string: message parameter 2 +// string: message parameter 3 +// string: message parameter 4 +// any string that starts with the character '#' is a message name, and is used to look up the real message in titles.txt +// the next (optional) one to four strings are parameters for that string (which can also be message names if they begin with '#') +int CHudTextMessage::MsgFunc_TextMsg( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + int msg_dest = READ_BYTE(); + + static char szBuf[6][128]; + char *msg_text = LookupString( READ_STRING(), &msg_dest ); + msg_text = strcpy( szBuf[0], msg_text ); + + // keep reading strings and using C format strings for subsituting the strings into the localised text string + char *sstr1 = LookupString( READ_STRING() ); + sstr1 = strcpy( szBuf[1], sstr1 ); + StripEndNewlineFromString( sstr1 ); // these strings are meant for subsitution into the main strings, so cull the automatic end newlines + char *sstr2 = LookupString( READ_STRING() ); + sstr2 = strcpy( szBuf[2], sstr2 ); + StripEndNewlineFromString( sstr2 ); + char *sstr3 = LookupString( READ_STRING() ); + sstr3 = strcpy( szBuf[3], sstr3 ); + StripEndNewlineFromString( sstr3 ); + char *sstr4 = LookupString( READ_STRING() ); + sstr4 = strcpy( szBuf[4], sstr4 ); + StripEndNewlineFromString( sstr4 ); + char *psz = szBuf[5]; + + switch ( msg_dest ) + { + case HUD_PRINTCENTER: + sprintf( psz, msg_text, sstr1, sstr2, sstr3, sstr4 ); + CenterPrint( ConvertCRtoNL( psz ), SCREEN_HEIGHT / 2, BIGCHAR_WIDTH ); + break; + case HUD_PRINTNOTIFY: + psz[0] = 1; // mark this message to go into the notify buffer + sprintf( psz+1, msg_text, sstr1, sstr2, sstr3, sstr4 ); + ConsolePrint( ConvertCRtoNL( psz ) ); + break; + case HUD_PRINTTALK: + sprintf( psz, msg_text, sstr1, sstr2, sstr3, sstr4 ); + gHUD.m_SayText.SayTextPrint( ConvertCRtoNL( psz ), 128 ); + break; + case HUD_PRINTCONSOLE: + sprintf( psz, msg_text, sstr1, sstr2, sstr3, sstr4 ); + ConsolePrint( ConvertCRtoNL( psz ) ); + break; + } + END_READ(); + + return 1; +} diff --git a/client/hud/hud_train.cpp b/client/hud/hud_train.cpp new file mode 100644 index 00000000..05ed4d95 --- /dev/null +++ b/client/hud/hud_train.cpp @@ -0,0 +1,79 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// Train.cpp +// +// implementation of CHudAmmo class +// + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +DECLARE_MESSAGE( m_Train, Train ) + +int CHudTrain :: Init( void ) +{ + HOOK_MESSAGE( Train ); + + m_iPos = 0; + m_iFlags = 0; + gHUD.AddHudElem( this ); + + return 1; +} + +int CHudTrain::VidInit( void ) +{ + m_hSprite = 0; + return 1; +} + +int CHudTrain::Draw(float fTime) +{ + if( !m_hSprite ) + m_hSprite = LOAD_SHADER( "hud_train" ); + + if( m_iPos ) + { + int r, g, b, x, y; + + UnpackRGB( r, g, b, gHUD.m_iHUDColor ); + SPR_Set( m_hSprite, r, g, b ); + + // This should show up to the right and part way up the armor number + y = SCREEN_HEIGHT - SPR_Height( m_hSprite, 0 ) - gHUD.m_iFontHeight; + x = SCREEN_WIDTH / 3 + SPR_Width( m_hSprite, 0 ) / 4; + + SPR_DrawAdditive( m_iPos - 1, x, y, NULL); + } + return 1; +} + + +int CHudTrain::MsgFunc_Train( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + + // update Train data + m_iPos = READ_BYTE(); + + if( m_iPos ) + m_iFlags |= HUD_ACTIVE; + else m_iFlags &= ~HUD_ACTIVE; + END_READ(); + + return 1; +} diff --git a/client/hud/hud_utils.cpp b/client/hud/hud_utils.cpp new file mode 100644 index 00000000..f24adf12 --- /dev/null +++ b/client/hud/hud_utils.cpp @@ -0,0 +1,356 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// hud_utils.cpp - client game utilities code +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +// NOTE: modify these functions with caution + +typedef struct +{ + char *name; + byte *buf; + int size; + int read; + BOOL badRead; + char string[2048]; // using for store strings +} user_message_t; + +static user_message_t gMsg; + +void BEGIN_READ( const char *pszName, int iSize, void *pBuf ) +{ + memset( &gMsg, 0, sizeof( gMsg )); + + gMsg.size = iSize; + gMsg.buf = (byte *)pBuf; +} + +void END_READ( void ) +{ + if( gMsg.badRead ) + { + ALERT( at_console, "%s was received with errors\n", gMsg.name ); + } +} + +int READ_CHAR( void ) +{ + int c; + + if( gMsg.read + 1 > gMsg.size ) + { + gMsg.badRead = true; + return -1; + } + + c = (signed char)gMsg.buf[gMsg.read]; + gMsg.read++; + + return c; +} + +int READ_BYTE( void ) +{ + int c; + + if( gMsg.read+1 > gMsg.size ) + { + gMsg.badRead = true; + return -1; + } + + c = (unsigned char)gMsg.buf[gMsg.read]; + gMsg.read++; + + return c; +} + +int READ_SHORT( void ) +{ + int c; + + if( gMsg.read + 2 > gMsg.size ) + { + gMsg.badRead = true; + return -1; + } + + c = (short)( gMsg.buf[gMsg.read] + ( gMsg.buf[gMsg.read+1] << 8 )); + gMsg.read += 2; + + return c; +} + +int READ_WORD( void ) { return READ_SHORT(); } + +int READ_LONG( void ) +{ + int c; + + if( gMsg.read + 4 > gMsg.size ) + { + gMsg.badRead = true; + return -1; + } + c = gMsg.buf[gMsg.read]+(gMsg.buf[gMsg.read+1]<<8)+(gMsg.buf[gMsg.read+2]<<16)+(gMsg.buf[gMsg.read+3]<<24); + gMsg.read += 4; + + return c; +} + +float READ_FLOAT( void ) +{ + union { float f; int l; } dat; + + dat.l = READ_LONG(); + return dat.f; +} + +char* READ_STRING( void ) +{ + int l, c; + + gMsg.string[0] = 0; + + l = 0; + do + { + if( gMsg.read+1 > gMsg.size ) break; // no more characters + + c = READ_CHAR(); + if( c == -1 || c == '\0' ) + break; + + // translate all fmt spec to avoid crash bugs + if( c == '%' ) c = '.'; + + gMsg.string[l] = c; + l++; + } while( l < sizeof( gMsg.string ) - 1 ); + + gMsg.string[l] = 0; // terminator + + return gMsg.string; +} + +// +// Xash3D network specs. Don't modify! +// +float READ_COORD( void ) +{ + return READ_FLOAT(); +} + +float READ_ANGLE( void ) +{ + return READ_FLOAT(); +} + +float READ_ANGLE16( void ) +{ + return (float)(READ_SHORT() * (360.0 / 65536)); +} + +// +// Sprites draw stuff +// +typedef struct +{ + // temp handle + HSPRITE hSprite; + + // crosshair members + HSPRITE hCrosshair; + wrect_t rcCrosshair; + Vector rgbCrosshair; +} draw_stuff_t; + +typedef struct +{ + float fadeSpeed; // How fast to fade (tics / second) (+ fade in, - fade out) + float fadeEnd; // When the fading hits maximum + float fadeTotalEnd; // Total End Time of the fade (used for FFADE_OUT) + float fadeReset; // When to reset to not fading (for fadeout and hold) + Vector fadeColor; + float fadealpha; + int fadeFlags; // Fading flags +} screenfade_t; + +static draw_stuff_t ds; +static screenfade_t sf; + +int SPR_Frames( HSPRITE hPic ) +{ + // FIXME: engfuncs GetImageFrames + return 1; +} + +int SPR_Height( HSPRITE hPic, int frame ) +{ + int Height; + + GetImageSize( NULL, &Height, hPic ); + + return Height; +} + +int SPR_Width( HSPRITE hPic, int frame ) +{ + int Width; + + GetImageSize( &Width, NULL, hPic ); + + return Width; +} + +void SPR_Set( HSPRITE hPic, int r, int g, int b ) +{ + ds.hSprite = hPic; + SetColor((r / 255.0f), (g / 255.0f), (b / 255.0f), 1.0f ); +} + +void SPR_Draw( int frame, int x, int y, const wrect_t *prc ) +{ + // FIXME: switch rendermode + DrawImage( ds.hSprite, x, y, prc->right, prc->bottom, frame ); +} + +void SPR_Draw( int frame, int x, int y, int width, int height ) +{ + // FIXME: switch rendermode + DrawImage( ds.hSprite, x, y, width, height, frame ); +} + +void SPR_DrawHoles( int frame, int x, int y, const wrect_t *prc ) +{ + // FIXME: switch rendermode + DrawImage( ds.hSprite, x, y, prc->right, prc->bottom, frame ); +} + +void SPR_DrawHoles( int frame, int x, int y, int width, int height ) +{ + // FIXME: switch rendermode + DrawImage( ds.hSprite, x, y, width, height, frame ); +} + +void SPR_DrawAdditive( int frame, int x, int y, const wrect_t *prc ) +{ + // FIXME: switch rendermode + DrawImage( ds.hSprite, x, y, prc->right, prc->bottom, frame ); +} + +void SetCrosshair( HSPRITE hspr, wrect_t rc, int r, int g, int b ) +{ + ds.rgbCrosshair.x = (float)(r / 255.0f); + ds.rgbCrosshair.y = (float)(g / 255.0f); + ds.rgbCrosshair.z = (float)(b / 255.0f); + ds.hCrosshair = hspr; + ds.rcCrosshair = rc; +} + +void DrawCrosshair( void ) +{ + if( ds.hCrosshair == 0 ) return; + + // find center of screen + int x = (SCREEN_WIDTH - ds.rcCrosshair.right) / 2; + int y = (SCREEN_HEIGHT - ds.rcCrosshair.bottom) / 2; + + // FIXME: apply crosshair angles + SetColor( ds.rgbCrosshair.x, ds.rgbCrosshair.y, ds.rgbCrosshair.z, 1.0f ); + DrawImage( ds.hCrosshair, x, y, ds.rcCrosshair.right, ds.rcCrosshair.bottom, 0 ); +} + +void SetScreenFade( Vector fadeColor, float alpha, float duration, float holdTime, int fadeFlags ) +{ + sf.fadeColor = fadeColor; + sf.fadealpha = alpha; + sf.fadeFlags = fadeFlags; + sf.fadeEnd = gHUD.m_flTime + duration; + sf.fadeTotalEnd = sf.fadeEnd + holdTime; + sf.fadeSpeed = duration / gHUD.m_flTimeDelta; +} + +void DrawScreenFade( void ) +{ + // FIXME: implement +} + +/* +==================== +Sys LoadGameDLL + +==================== +*/ +BOOL Sys_LoadLibrary( const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts ) +{ + const dllfunction_t *gamefunc; + char dllpath[256], gamedir[256]; + dllhandle_t dllhandle = 0; + + if( handle == NULL ) return false; + + // Initializations + for( gamefunc = fcts; gamefunc && gamefunc->name != NULL; gamefunc++ ) + *gamefunc->funcvariable = NULL; + + GET_GAME_DIR( gamedir ); + sprintf( dllpath, "%s/bin/%s", gamedir, dllname ); + dllhandle = LoadLibrary( dllpath ); + + // No DLL found + if( !dllhandle ) return false; + + // Get the function adresses + for( gamefunc = fcts; gamefunc && gamefunc->name != NULL; gamefunc++ ) + if(!( *gamefunc->funcvariable = (void *) Sys_GetProcAddress( dllhandle, gamefunc->name ))) + { + Sys_UnloadLibrary( &dllhandle ); + return false; + } + + ALERT( at_loading, "%s loaded succesfully!\n", dllname ); + *handle = dllhandle; + return true; +} + +void Sys_UnloadLibrary( dllhandle_t *handle ) +{ + if( handle == NULL || *handle == NULL ) + return; + + FreeLibrary( *handle ); + *handle = NULL; +} + +void* Sys_GetProcAddress( dllhandle_t handle, const char* name ) +{ + return (void *)GetProcAddress( handle, name ); +} + +/* +============ +va + +does a varargs printf into a temp buffer, so I don't need to have +varargs versions of all text functions. +FIXME: make this buffer size safe someday +============ +*/ +char *va( const char *format, ... ) +{ + va_list argptr; + static char string[16][1024], *s; + static int stringindex = 0; + + s = string[stringindex]; + stringindex = (stringindex + 1) & 15; + va_start( argptr, format ); + _vsnprintf( s, sizeof( string[0] ), format, argptr ); + va_end( argptr ); + return s; +} \ No newline at end of file diff --git a/client/hud/hud_warhead.cpp b/client/hud/hud_warhead.cpp new file mode 100644 index 00000000..bed0fcd0 --- /dev/null +++ b/client/hud/hud_warhead.cpp @@ -0,0 +1,141 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// warhead.cpp - hud for weapon_redeemer +// +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +#define GUIDE_S SPR_Width( m_hCrosshair, 0 ) +#define READOUT_S 128 +DECLARE_MESSAGE( m_Redeemer, WarHUD ) + +int CHudRedeemer::Init( void ) +{ + m_iHudMode = 0; + + HOOK_MESSAGE( WarHUD ); + m_iFlags |= HUD_ACTIVE; + gHUD.AddHudElem( this ); + + return 1; +} + +int CHudRedeemer :: VidInit( void ) +{ + m_hSprite = LOAD_SHADER( "wh_readout" ); + m_hCrosshair = LOAD_SHADER( "wh_guidedx"); + m_hStatic = LOAD_SHADER( "wh_static"); + m_hCamera = LOAD_SHADER( "wh_camera"); + m_hCamRec = LOAD_SHADER( "wh_cam_rec"); + + m_iHudMode = 0; + return 1; +} + +int CHudRedeemer :: MsgFunc_WarHUD( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + m_iHudMode = READ_BYTE(); + END_READ(); + + return 1; +} + +int CHudRedeemer :: Draw( float flTime ) +{ + int x, y, w, h; + wrect_t rc; + int frame; + + // setup screen rectangle + rc.left = rc.top = 0; + rc.right = SCREEN_WIDTH; + rc.bottom = SCREEN_HEIGHT; + + if( m_iHudMode == 1 ) // draw crosshair and readout + { + + y = (SCREEN_WIDTH - GUIDE_S) / 2; + x = (SCREEN_HEIGHT - GUIDE_S) / 2; + + SPR_Set( m_hCrosshair, 255, 128, 128 ); + SPR_DrawAdditive( 0, y, x, NULL); + + int yOffset = ((int)(flTime * 850) % READOUT_S) - READOUT_S; + SPR_Set( m_hSprite, 255, 128, 128 ); + for( ; yOffset < SCREEN_HEIGHT; yOffset += READOUT_S ) + SPR_DrawAdditive( 0, 0, yOffset, NULL ); + SetScreenFade( Vector( 1, 0, 0 ), 0.25, 0, 0, FFADE_STAYOUT ); // enable red fade + } + else if( m_iHudMode == 2 ) // draw alpha noise + { + SPR_Set(m_hStatic, 255, 255, 255 ); + + // play at 15fps + frame = (int)(flTime * 15) % SPR_Frames( m_hStatic ); + + y = x = 0; + w = SPR_Width( m_hStatic, 0); + h = SPR_Height( m_hStatic, 0); + + for( y = -(rand() % h); y < SCREEN_HEIGHT; y += h ) + for( x = -(rand() % w); x < SCREEN_WIDTH; x += w ) + SPR_DrawAdditive ( frame, x, y, NULL ); + + y = (SCREEN_WIDTH - GUIDE_S) / 2; + x = (SCREEN_HEIGHT - GUIDE_S) / 2; + + SPR_Set( m_hCrosshair, 255, 128, 128 ); + SPR_DrawAdditive( 0, y, x, NULL ); + + int yOffset = ((int)(flTime * 850) % READOUT_S) - READOUT_S; + SPR_Set(m_hSprite, 255, 128, 128 ); + for( ; yOffset < SCREEN_HEIGHT; yOffset += READOUT_S ) + SPR_DrawAdditive( 0, 0, yOffset, NULL ); + SetScreenFade( Vector( 1, 0, 0 ), 0.25, 0, 0, FFADE_STAYOUT ); // enable red fade + } + else if( m_iHudMode == 3 ) // draw static noise + { + // play at 25fps + frame = (int)(flTime * 25) % SPR_Frames( m_hStatic ); + + SPR_Set( m_hStatic, 255, 255, 255 ); + SPR_Draw( frame, 0, 0, &rc ); + + // disable fade + SetScreenFade( Vector( 1, 1, 1 ), 0, 0, 0, FFADE_OUT ); + } + else if( m_iHudMode == 4 ) // draw videocamera screen + { + // play at 15fps + frame = (int)(flTime * 1.5f) % SPR_Frames( m_hCamRec ); + + // draw interlaces + SPR_Set( m_hCamera, 16, 96, 16 ); //, 64 ); // give this value from breaklight.bsp (xash03) + SPR_DrawAdditive( frame, 0, 0, &rc ); + + // draw recorder icon + SPR_Set( m_hCamRec, 255, 0, 0 ); //, 255 ); // give this value from breaklight.bsp (xash03) + + float scale = 2.5f; // REC[*] sprite scale + wrect_t m_rcCamRec; + + // calculating pos with different resolutions + w = SPR_Width( m_hCamRec, 0 ) * scale; + h = SPR_Height( m_hCamRec, 0 ) * scale; + x = SCREEN_WIDTH - (w * 1.5f); + y = w * 0.4f; + m_rcCamRec.left = x; + m_rcCamRec.right = x + w; + m_rcCamRec.top = y; + m_rcCamRec.bottom = y + h; + SPR_DrawAdditive( frame, 0, 0, &m_rcCamRec ); + + // disable fade + SetScreenFade( Vector( 1, 1, 1 ), 0, 0, 0, FFADE_OUT ); + } + return 1; +} \ No newline at end of file diff --git a/client/hud/hud_zoom.cpp b/client/hud/hud_zoom.cpp new file mode 100644 index 00000000..2a8641de --- /dev/null +++ b/client/hud/hud_zoom.cpp @@ -0,0 +1,80 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// warhead.cpp - hud for weapon_redeemer +// +//======================================================================= + +#include "extdll.h" +#include "hud_iface.h" +#include "hud.h" + +void DrawQuad(float xmin, float ymin, float xmax, float ymax) +{ + //top left + g_engfuncs.pTriAPI->TexCoord2f(0,0); + g_engfuncs.pTriAPI->Vertex3f(xmin, ymin, 0); + //bottom left + g_engfuncs.pTriAPI->TexCoord2f(0,1); + g_engfuncs.pTriAPI->Vertex3f(xmin, ymax, 0); + //bottom right + g_engfuncs.pTriAPI->TexCoord2f(1,1); + g_engfuncs.pTriAPI->Vertex3f(xmax, ymax, 0); + //top right + g_engfuncs.pTriAPI->TexCoord2f(1,0); + g_engfuncs.pTriAPI->Vertex3f(xmax, ymin, 0); +} + +DECLARE_MESSAGE( m_Zoom, ZoomHUD ) + +int CHudZoom :: Init( void ) +{ + m_iHudMode = 0; + HOOK_MESSAGE( ZoomHUD ); + + m_iFlags |= HUD_ACTIVE; + + gHUD.AddHudElem( this ); + return 1; +} + +int CHudZoom::VidInit( void ) +{ + m_hLines = LOAD_SHADER( "sniper_lines" ); + m_hCrosshair = LOAD_SHADER( "sniper_zoom" ); + m_iHudMode = 0; + return 1; +} + +int CHudZoom :: MsgFunc_ZoomHUD( const char *pszName, int iSize, void *pbuf ) +{ + BEGIN_READ( pszName, iSize, pbuf ); + m_iHudMode = READ_BYTE(); + END_READ(); + + return 1; +} + +int CHudZoom :: Draw( float flTime ) +{ + if( !m_hLines || !m_hCrosshair ) return 0; + if( !m_iHudMode ) return 0; // draw scope + + float left = (SCREEN_WIDTH - SCREEN_HEIGHT)/2; + float right = left + SCREEN_HEIGHT; + float centerx = SCREEN_WIDTH / 2; + float centery = SCREEN_HEIGHT / 2; + + // draw crosshair + SPR_Set( m_hCrosshair, 255, 255, 255 ); + SPR_DrawHoles( 0, left, 0, centerx, centery); + SPR_DrawHoles( 1, centerx, 0, right, centery); + SPR_DrawHoles( 2, centerx, centery, right, SCREEN_HEIGHT); + SPR_DrawHoles( 3, left, centery, centerx, SCREEN_HEIGHT); + + // draw side-lines + SPR_Set( m_hLines, 255, 255, 255 ); + SPR_Draw( 0, 0, 0, left, SCREEN_HEIGHT ); + SPR_Draw( 0, right, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); + + return 1; +} diff --git a/client/r_main.h b/client/r_main.h new file mode 100644 index 00000000..d47ccb7d --- /dev/null +++ b/client/r_main.h @@ -0,0 +1,348 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#pragma once +#define WIN32_LEAN_AND_MEAN +#define GLEW_STATIC +#include "windows.h" +#include "glew.h" +#include +#include "cva.h" +#include "ref_params.h" +#include "com_model.h" + +#pragma warning(disable:4244) + +#define SAFE_GET_PROC( func, type, name) if (!func) func = (type) wglGetProcAddress( name ); \ + assert(func != NULL) + +#define VIEWPORT_SIZE 512 +#define GL_TEXTURE_NUM_BASE (1<<25) + +#define SURF_PLANEBACK 2 +#define SURF_DRAWTURB 0x10 + +#define SURF_MIRROR (1<<16) +#define SURF_WARPMIRROR (1<<17) +#define SURF_SCREEN (1<<18) +#define SURF_PORTAL (1<<19) + +// 0-2 are axial planes +#define PLANE_X 0 +#define PLANE_Y 1 +#define PLANE_Z 2 + +#define MAX_CVA_VERTS 2048 + +//We need the following extensions: + +//ARB_multitexture +extern PFNGLACTIVETEXTUREARBPROC glActiveTextureARB; +extern PFNGLCLIENTACTIVETEXTUREARBPROC glClientActiveTextureARB; +extern PFNGLMULTITEXCOORD1FARBPROC glMultiTexCoord1fARB; +extern PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2fARB; +extern PFNGLMULTITEXCOORD2FVARBPROC glMultiTexCoord2fvARB; +extern PFNGLMULTITEXCOORD3FARBPROC glMultiTexCoord3fARB; +extern PFNGLMULTITEXCOORD3FVARBPROC glMultiTexCoord3fvARB; +extern PFNGLMULTITEXCOORD4FARBPROC glMultiTexCoord4fARB; +extern PFNGLMULTITEXCOORD4FVARBPROC glMultiTexCoord4fvARB; + +//EXT_texture_3d +extern PFNGLTEXIMAGE3DEXTPROC glTexImage3DEXT; + +//ARB_texture_cube_map + +//ARB_vertex_program +//ARB_fragment_program +extern PFNGLPROGRAMSTRINGARBPROC glProgramStringARB; +extern PFNGLBINDPROGRAMARBPROC glBindProgramARB; +extern PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB; +extern PFNGLGENPROGRAMSARBPROC glGenProgramsARB; +extern PFNGLGETPROGRAMIVARBPROC glGetProgramivARB; +extern PFNGLGETPROGRAMSTRINGARBPROC glGetProgramStringARB; +extern PFNGLISPROGRAMARBPROC glIsProgramARB; + +//NV_register_combiners +extern PFNGLCOMBINERPARAMETERFVNVPROC glCombinerParameterfvNV; +extern PFNGLCOMBINERPARAMETERIVNVPROC glCombinerParameterivNV; +extern PFNGLCOMBINERPARAMETERFNVPROC glCombinerParameterfNV; +extern PFNGLCOMBINERPARAMETERINVPROC glCombinerParameteriNV; +extern PFNGLCOMBINERINPUTNVPROC glCombinerInputNV; +extern PFNGLCOMBINEROUTPUTNVPROC glCombinerOutputNV; +extern PFNGLFINALCOMBINERINPUTNVPROC glFinalCombinerInputNV; + +//ATI_pn_triangles +extern PFNGLPNTRIANGLESIATIPROC glPNTrianglesiATI; +extern PFNGLPNTRIANGLESFATIPROC glPNTrianglesfATI; + +inline bool GLEXT_CheckExtension(const char *ext) +{ + return (strstr((const char *)glGetString(GL_EXTENSIONS), ext) != NULL); +} + +bool GLEXT_Setup_ARB_multitexture(); +bool GLEXT_Setup_EXT_texture_3d(); +bool GLEXT_Setup_ARB_texture_cubemap(); +bool GLEXT_Setup_ARB_vertex_program(); +bool GLEXT_Setup_ARB_fragment_program(); +bool GLEXT_Setup_NV_register_combiners(); +bool GLEXT_Setup_ATI_pn_triangles(); + +void GL_Init( void ); +void GL_VidInit( void ); +void GL_Shutdown( void ); +void GL_PreRender( void ); +void GL_Render( bool trans ); +void GL_ExtractFrustum( void ); +void GL_MarkTextures( void ); +void R_Draw( bool transparent ); + +//Utils +float R_SphereInFrustum( vec3_t o, float radius ); +cl_entity_t *UTIL_GetClientEntityWithServerIndex( int sv_index ); + +extern bool g_HardwareCapable; +extern bool g_HardwareShaderCapable; + +extern int g_iTotalMirrors; +extern int g_iTotalVisibleMirrors; +extern int g_iTotalScreens; +extern int g_iTotalVisibleScreens; +extern int g_iTotalPortals; +extern int g_iTotalVisiblePortals; +extern double realtime; + +extern bool g_FirstFrame; +extern bool g_RenderReady; +extern bool g_bFinalPass; +extern bool g_bEndCalc; +extern int m_RenderRefCount; //refcounter (use for debug) + +//passes info +extern bool g_bMirrorShouldpass; +extern bool g_bScreenShouldpass; +extern bool g_bPortalShouldpass; +extern bool g_bSkyShouldpass; +extern bool g_bMirrorPass; +extern bool g_bPortalPass; +extern bool g_bScreenPass; +extern bool g_bSkyPass; + +//base origin and angles +extern vec3_t g_vecBaseOrigin; //base origin - transformed always +extern vec3_t g_vecBaseAngles; //base angles - transformed always +extern vec3_t g_vecCurrentOrigin; //current origin +extern vec3_t g_vecCurrentAngles; //current angles + +extern float r_projection_matrix[16]; +extern float r_world_matrix[16]; + +extern unsigned int g_uiNoiseTex; +extern unsigned int g_uiNoiseTexDsDt; +extern unsigned int pTexId; +extern unsigned int AllocateTextureIndex(void); + +typedef struct mirror_s +{ + int index; + bool enabled; + bool visible; + float origin[3]; + float angles[3]; + float normal[3]; + float radius; + float alpha; + + float pr[16]; + + bool water; + bool twoside; + + //CVA-related + int firstvertex; + int numvertices; + + unsigned int texture; + msurface_t *surface; + + int sv_entindex; + cl_entity_t *ent; //for brush model mirrors + + struct mirror_s *next; +}mirror_t; + +typedef struct screen_s +{ + int index; + bool enabled; + bool visible; + float origin[3]; + float radius; + float alpha; + + //current camera member + int cam_idx; + float cam_origin[3]; + float cam_angles[3]; + + float pr[16]; + + //CVA-related + int firstvertex; + int numvertices; + + unsigned int texture; + msurface_t *surface; + + int sv_entindex; + cl_entity_t *ent; + bool color; + + struct screen_s *next; +}screen_t; + +typedef struct portal_s +{ + int index; + bool enabled; + bool visible; + float origin[3]; + float angles[3]; + float normal[3]; + float radius; + float alpha; + + //current camera member + int cam_idx; + float cam_origin[3]; + float cam_angles[3]; + + float pr[16]; + + //CVA-related + int firstvertex; + int numvertices; + + unsigned int texture; + msurface_t *surface; + + int sv_entindex; + cl_entity_t *ent; //for brush model mirrors + + struct portal_s *next; +}portal_t; + +typedef struct cl_sbe_s +{ + int index; + bool initialized; +}cl_sbe_t; + +extern cvar_t *r_debug; //show renderer info +extern cvar_t *r_shadows; //original HL shadows +extern cvar_t *r_mirrors; //mirrors +extern cvar_t *r_screens; //screens +extern cvar_t *r_portals; //portals + +mirror_t *R_AllocateMirror(msurface_t *surf); +void R_FreeMirrors(void); +void R_InitMirrors(void); +void R_SetupMirrorRenderPass(ref_params_t *pparams); +void R_SetupNewMirror(ref_params_t *pparams); +void R_NewMirrorRenderPass(void); +void R_RenderMirrors(bool trans); +void R_InitMirrorsForFrame(void); +void R_ResetMirrors(void); +void AddMirrorBrushEntity( int idx ); +void R_DrawMirror(bool trans); + +screen_t *R_AllocateScreen(msurface_t *surf); +void R_ResetScreens( void ); +void R_FreeScreens( void ); +void R_InitScreens( void ); +void R_RenderScreens( bool trans ); +void AddScreenBrushEntity( int idx ); +void R_SetupScreenRenderPass(ref_params_t *pparams); +void R_SetupNewScreen(ref_params_t *pparams); +void R_NewScreenRenderPass( void ); +void R_InitScreensForFrame(void); +void R_DrawScreen(bool trans); + +portal_t *R_AllocatePortal(msurface_t *surf); +void R_FreePortals(void); +void R_InitPortals(void); +void R_SetupPortalRenderPass(ref_params_t *pparams); +void R_SetupNewPortal(ref_params_t *pparams); +void R_NewPortalRenderPass(void); +void R_RenderPortals(bool trans); +void R_InitPortalsForFrame(void); +void R_ResetPortals(void); +void AddPortalBrushEntity( int idx ); +void R_DrawPortal(bool trans); + +//Noise +void CreateNoiseTexture3D (float scale); +void CreateNoiseTextureDsDt (float scale); + +//Water +void CreateWaterShader_ARB(void); +void DeleteWaterShader_ARB(void); +void BindWaterShader_ARB(mirror_t *mir); +void UnbindWaterShader_ARB(void); + +//Black & white screen +void CreateScreenShader_ARB(void); +void DeleteScreenShader_ARB(void); + +//Logging +void logInitLog( char *filename ); +void logCloseLog(); +void logPrintOpenGLInformation( void ); +void logPrint( char *str ); +void logPrintf( char *fmt, ...); + +extern mirror_t *m_pCurrentMirror; +extern screen_t *m_pCurrentScreen; +extern portal_t *m_pCurrentPortal; + +//GL state management +struct glstate_curstate_t +{ + unsigned char + blend : 1, + depth_test : 1, + : 6; +}; + +struct glstate_curstagestate_t +{ + unsigned char + texture2d : 1, + texture3d : 1, + : 6; +}; + +typedef struct +{ + struct glstate_curstate_t curstate; + struct glstate_curstagestate_t curstagestate[8]; //assume 8 will be max texture stages + unsigned int current_texture_stage; +} glstate_t; + +extern glstate_t glstate; + +#define GLSTATE_ENABLE_BLEND if(!glstate.curstate.blend) { glEnable(GL_BLEND); glstate.curstate.blend=1; } +#define GLSTATE_ENABLE_DEPTH_TEST if(!glstate.curstate.depth_test) { glEnable(GL_DEPTH_TEST); glstate.curstate.depth_test=1; } +#define GLSTATE_ENABLE_TEXTURE if(!glstate.curstagestate[glstate.current_texture_stage].texture2d) { glEnable(GL_TEXTURE_2D); glstate.curstagestate[glstate.current_texture_stage].texture2d=1; } +#define GLSTATE_ENABLE_TEXTURE3D if(!glstate.curstagestate[glstate.current_texture_stage].texture3d) { glEnable(GL_TEXTURE_3D); glstate.curstagestate[glstate.current_texture_stage].texture3d=1; } +#define GLSTATE_DISABLE_BLEND if(glstate.curstate.blend) { glDisable(GL_BLEND); glstate.curstate.blend=0; } +#define GLSTATE_DISABLE_DEPTH_TEST if(glstate.curstate.depth_test) { glDisable(GL_DEPTH_TEST); glstate.curstate.depth_test=0; } +#define GLSTATE_DISABLE_TEXTURE if(glstate.curstagestate[glstate.current_texture_stage].texture2d) { glDisable(GL_TEXTURE_2D); glstate.curstagestate[glstate.current_texture_stage].texture2d=0; } +#define GLSTATE_DISABLE_TEXTURE3D if(glstate.curstagestate[glstate.current_texture_stage].texture3d) { glDisable(GL_TEXTURE_3D); glstate.curstagestate[glstate.current_texture_stage].texture3d=0; } + +void GL_SelectTexture( GLenum mode ); + +//client sprite renderer +void R_DrawSprite ( cl_entity_t *e, HSPRITE m_hSprite ); +mspriteframe_t *R_GetSpriteFrame (model_t *mod, int frame); \ No newline at end of file diff --git a/client/r_particle.cpp b/client/r_particle.cpp new file mode 100644 index 00000000..db6610c2 --- /dev/null +++ b/client/r_particle.cpp @@ -0,0 +1,960 @@ +#include "hud.h" +#include "r_main.h" +#include "r_util.h" +#include "const.h" +#include "entity_state.h" +#include "event_api.h" +#include "cl_entity.h" +#include "triangleapi.h" +#include "r_particle.h" +#include "com_model.h" +#include "pmtrace.h" // for contents and traceline +#include "pm_defs.h" + +ParticleSystemManager* g_pParticleSystems = NULL; + +void CreateAurora( int idx, char *file ) +{ + ParticleSystem *pSystem = new ParticleSystem( idx, file ); + g_pParticleSystems->AddSystem(pSystem); +} + + +ParticleSystemManager::ParticleSystemManager( void ) +{ + m_pFirstSystem = NULL; +} + +ParticleSystemManager::~ParticleSystemManager( void ) +{ + ClearSystems(); +} + +void ParticleSystemManager::AddSystem( ParticleSystem* pNewSystem ) +{ + pNewSystem->m_pNextSystem = m_pFirstSystem; + m_pFirstSystem = pNewSystem; +} + +ParticleSystem *ParticleSystemManager::FindSystem( cl_entity_t* pEntity ) +{ + for (ParticleSystem *pSys = m_pFirstSystem; pSys; pSys = pSys->m_pNextSystem) + { + if (pEntity->index == pSys->m_iEntIndex) return pSys; + } + return NULL; +} + +void ParticleSystemManager::UpdateSystems( void ) +{ + static float fOldTime, fTime; + fOldTime = fTime; + fTime = gEngfuncs.GetClientTime(); + float frametime = fTime - fOldTime; + + ParticleSystem* pSystem; + ParticleSystem* pLast = NULL; + ParticleSystem*pLastSorted = NULL; + + pSystem = m_pFirstSystem; + while( pSystem ) + { + if( pSystem->UpdateSystem(frametime)) + { + pSystem->DrawSystem(); + pLast = pSystem; + pSystem = pSystem->m_pNextSystem; + } + else // delete this system + { + if (pLast) + { + pLast->m_pNextSystem = pSystem->m_pNextSystem; + delete pSystem; + pSystem = pLast->m_pNextSystem; + } + else // deleting the first system + { + m_pFirstSystem = pSystem->m_pNextSystem; + delete pSystem; + pSystem = m_pFirstSystem; + } + } + } + gEngfuncs.pTriAPI->RenderMode(kRenderNormal); +} + +void ParticleSystemManager::ClearSystems( void ) +{ + ParticleSystem* pSystem = m_pFirstSystem; + ParticleSystem* pTemp; + + while( pSystem ) + { + pTemp = pSystem->m_pNextSystem; + delete pSystem; + pSystem = pTemp; + } + + m_pFirstSystem = NULL; +} + +float ParticleSystem::c_fCosTable[360 + 90]; +bool ParticleSystem::c_bCosTableInit = false; + +ParticleType::ParticleType( ParticleType *pNext ) +{ + m_pSprayType = m_pOverlayType = NULL; + m_StartAngle = RandomRange(45); + m_hSprite = 0; + m_pNext = pNext; + m_szName[0] = 0; + m_StartRed = m_StartGreen = m_StartBlue = m_StartAlpha = RandomRange(1); + m_EndRed = m_EndGreen = m_EndBlue = m_EndAlpha = RandomRange(1); + m_iRenderMode = kRenderTransAdd; + m_iDrawCond = 0; + m_bEndFrame = false; + + m_bIsDefined = false; + m_iCollision = 0; +} + +particle* ParticleType::CreateParticle(ParticleSystem *pSys)//particle *pPart) +{ + if (!pSys) return NULL; + + particle *pPart = pSys->ActivateParticle(); + if (!pPart) return NULL; + + pPart->age = 0.0; + pPart->age_death = m_Life.GetInstance(); + + InitParticle(pPart, pSys); + + return pPart; +} + +void ParticleType::InitParticle(particle *pPart, ParticleSystem *pSys) +{ + float fLifeRecip = 1/pPart->age_death; + + particle *pOverlay = NULL; + if (m_pOverlayType) + { + // create an overlay for this particle + pOverlay = pSys->ActivateParticle(); + if (pOverlay) + { + pOverlay->age = pPart->age; + pOverlay->age_death = pPart->age_death; + m_pOverlayType->InitParticle(pOverlay, pSys); + } + } + + pPart->m_pOverlay = pOverlay; + + pPart->pType = this; + pPart->velocity[0] = pPart->velocity[1] = pPart->velocity[2] = 0; + pPart->accel[0] = pPart->accel[1] = 0; + pPart->accel[2] = m_Gravity.GetInstance(); + pPart->m_iEntIndex = 0; + + if (m_pSprayType) + { + pPart->age_spray = 1/m_SprayRate.GetInstance(); + } + else + { + pPart->age_spray = 0.0f; + } + + pPart->m_fSize = m_StartSize.GetInstance(); + + if (m_EndSize.IsDefined()) + pPart->m_fSizeStep = m_EndSize.GetOffset(pPart->m_fSize) * fLifeRecip; + else + pPart->m_fSizeStep = m_SizeDelta.GetInstance(); + //pPart->m_fSizeStep = m_EndSize.GetOffset(pPart->m_fSize) * fLifeRecip; + + pPart->frame = m_StartFrame.GetInstance(); + if (m_EndFrame.IsDefined()) + pPart->m_fFrameStep = m_EndFrame.GetOffset(pPart->frame) * fLifeRecip; + else pPart->m_fFrameStep = m_FrameRate.GetInstance(); + + pPart->m_fAlpha = m_StartAlpha.GetInstance(); + pPart->m_fAlphaStep = m_EndAlpha.GetOffset(pPart->m_fAlpha) * fLifeRecip; + pPart->m_fRed = m_StartRed.GetInstance(); + pPart->m_fRedStep = m_EndRed.GetOffset(pPart->m_fRed) * fLifeRecip; + pPart->m_fGreen = m_StartGreen.GetInstance(); + pPart->m_fGreenStep = m_EndGreen.GetOffset(pPart->m_fGreen) * fLifeRecip; + pPart->m_fBlue = m_StartBlue.GetInstance(); + pPart->m_fBlueStep = m_EndBlue.GetOffset(pPart->m_fBlue) * fLifeRecip; + + pPart->m_fAngle = m_StartAngle.GetInstance(); + pPart->m_fAngleStep = m_AngleDelta.GetInstance(); + + pPart->m_fDrag = m_Drag.GetInstance(); + + float fWindStrength = m_WindStrength.GetInstance(); + float fWindYaw = m_WindYaw.GetInstance(); + pPart->m_vecWind.x = fWindStrength*ParticleSystem::CosLookup(fWindYaw); + pPart->m_vecWind.y = fWindStrength*ParticleSystem::SinLookup(fWindYaw); + pPart->m_vecWind.z = 0; +} + +//============================================ + + +RandomRange::RandomRange( char *szToken ) +{ + char *cOneDot = NULL; + m_bDefined = true; + + for (char *c = szToken; *c; c++) + { + if (*c == '.') + { + if (cOneDot != NULL) + { + // found two dots in a row - it's a range + + *cOneDot = 0; // null terminate the first number + m_fMin = atof(szToken); // parse the first number + *cOneDot = '.'; // change it back, just in case + c++; + m_fMax = atof(c); // parse the second number + return; + } + else + { + cOneDot = c; + } + } + else + { + cOneDot = NULL; + } + } + + // no range, just record the number + m_fMin = m_fMax = atof(szToken); +} + +//============================================ + +ParticleSystem::ParticleSystem( int iEntIndex, char *szFilename ) +{ + int iParticles = 100; // default + + m_iEntIndex = iEntIndex; + m_pNextSystem = NULL; + m_pFirstType = NULL; + if (!c_bCosTableInit) + { + for (int i = 0; i < 360+90; i++) + { + c_fCosTable[i] = cos(i*M_PI/180.0); + } + c_bCosTableInit = true; + } + + char *szFile = (char *)gEngfuncs.COM_LoadFile( szFilename, 5, NULL); + char szToken[1024]; + + if (!szFile) + { + Msg("Particle %s not found.\n", szFilename ); + return; + } + else + { + szFile = gEngfuncs.COM_ParseFile(szFile, szToken); + + while (szFile) + { + if ( !stricmp( szToken, "particles" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + iParticles = atof(szToken); + } + else if ( !stricmp( szToken, "maintype" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + m_pMainType = AddPlaceholderType(szToken); + } + else if ( !stricmp( szToken, "attachment" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + m_iEntAttachment = atof(szToken); + } + else if ( !stricmp( szToken, "killcondition" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + if ( !stricmp( szToken, "empty" ) ) + { + m_iKillCondition = CONTENTS_EMPTY; + } + else if ( !stricmp( szToken, "water" ) ) + { + m_iKillCondition = CONTENTS_WATER; + } + else if ( !stricmp( szToken, "solid" ) ) + { + m_iKillCondition = CONTENTS_SOLID; + } + } + else if ( !stricmp( szToken, "{" ) ) + { + // parse new type + this->ParseType( szFile ); // parses the type, moves the file pointer + } + + szFile = gEngfuncs.COM_ParseFile(szFile, szToken); + } + } + + gEngfuncs.COM_FreeFile( szFile ); + AllocateParticles(iParticles); +} + +void ParticleSystem::AllocateParticles( int iParticles ) +{ + m_pAllParticles = new particle[iParticles]; + m_pFreeParticle = m_pAllParticles; + m_pActiveParticle = NULL; + m_pMainParticle = NULL; + + // initialise the linked list + particle *pLast = m_pAllParticles; + particle *pParticle = pLast+1; + + for( int i = 1; i < iParticles; i++ ) + { + pLast->nextpart = pParticle; + + pLast = pParticle; + pParticle++; + } + pLast->nextpart = NULL; +} + +ParticleSystem::~ParticleSystem( void ) +{ + delete[] m_pAllParticles; + + ParticleType *pType = m_pFirstType; + ParticleType *pNext; + while (pType) + { + pNext = pType->m_pNext; + delete pType; + pType = pNext; + } +} + + + +// returns the ParticleType with the given name, if there is one +ParticleType *ParticleSystem::GetType( const char *szName ) +{ + for (ParticleType *pType = m_pFirstType; pType; pType = pType->m_pNext) + { + if (!stricmp(pType->m_szName, szName)) + return pType; + } + return NULL; +} + +ParticleType *ParticleSystem::AddPlaceholderType( const char *szName ) +{ + m_pFirstType = new ParticleType( m_pFirstType ); + strncpy(m_pFirstType->m_szName, szName, sizeof(m_pFirstType->m_szName) ); + return m_pFirstType; +} + +// creates a new particletype from the given file +// NB: this changes the value of szFile. +ParticleType *ParticleSystem::ParseType( char *&szFile ) +{ + ParticleType *pType = new ParticleType(); + + // parse the .aur file + char szToken[1024]; + + szFile = gEngfuncs.COM_ParseFile(szFile, szToken); + while ( stricmp( szToken, "}" ) ) + { + if (!szFile) + break; + + if ( !stricmp( szToken, "name" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + strncpy(pType->m_szName, szToken, sizeof(pType->m_szName) ); + + ParticleType *pTemp = GetType(szToken); + if (pTemp) + { + // there's already a type with this name + if (pTemp->m_bIsDefined) + Msg("Warning: Particle type %s is defined more than once!\n", szToken); + + // copy all our data into the existing type, throw away the type we were making + *pTemp = *pType; + delete pType; + pType = pTemp; + pType->m_bIsDefined = true; // record the fact that it's defined, so we won't need to add it to the list + } + } + else if ( !stricmp( szToken, "gravity" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_Gravity = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "windyaw" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_WindYaw = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "windstrength" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_WindStrength = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "sprite" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_hSprite = SPR_Load( szToken ); + } + else if ( !stricmp( szToken, "startalpha" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_StartAlpha = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "endalpha" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_EndAlpha = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "startred" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_StartRed = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "endred" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_EndRed = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "startgreen" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_StartGreen = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "endgreen" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_EndGreen = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "startblue" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_StartBlue = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "endblue" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_EndBlue = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "startsize" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_StartSize = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "sizedelta" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_SizeDelta = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "endsize" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_EndSize = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "startangle" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_StartAngle = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "angledelta" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_AngleDelta = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "startframe" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_StartFrame = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "endframe" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_EndFrame = RandomRange( szToken ); + pType->m_bEndFrame = true; + } + else if ( !stricmp( szToken, "framerate" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_FrameRate = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "lifetime" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_Life = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "spraytype" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + ParticleType *pTemp = GetType(szToken); + + if (pTemp) pType->m_pSprayType = pTemp; + else pType->m_pSprayType = AddPlaceholderType(szToken); + } + else if ( !stricmp( szToken, "overlaytype" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + ParticleType *pTemp = GetType(szToken); + + if (pTemp) pType->m_pOverlayType = pTemp; + else pType->m_pOverlayType = AddPlaceholderType(szToken); + } + else if ( !stricmp( szToken, "sprayrate" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_SprayRate = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "sprayforce" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_SprayForce = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "spraypitch" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_SprayPitch = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "sprayyaw" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_SprayYaw = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "drag" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_Drag = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "bounce" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_Bounce = RandomRange( szToken ); + if (pType->m_Bounce.m_fMin != 0 || pType->m_Bounce.m_fMax != 0) + pType->m_bBouncing = true; + } + else if ( !stricmp( szToken, "bouncefriction" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + pType->m_BounceFriction = RandomRange( szToken ); + } + else if ( !stricmp( szToken, "rendermode" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + if ( !stricmp( szToken, "additive" ) ) + { + pType->m_iRenderMode = kRenderTransAdd; + } + else if ( !stricmp( szToken, "solid" ) ) + { + pType->m_iRenderMode = kRenderTransAlpha; + } + else if ( !stricmp( szToken, "texture" ) ) + { + pType->m_iRenderMode = kRenderTransTexture; + } + else if ( !stricmp( szToken, "color" ) ) + { + pType->m_iRenderMode = kRenderTransColor; + } + } + else if ( !stricmp( szToken, "drawcondition" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + if ( !stricmp( szToken, "empty" ) ) + { + pType->m_iDrawCond = CONTENTS_EMPTY; + } + else if ( !stricmp( szToken, "water" ) ) + { + pType->m_iDrawCond = CONTENTS_WATER; + } + else if ( !stricmp( szToken, "solid" ) ) + { + pType->m_iDrawCond = CONTENTS_SOLID; + } + else if ( !stricmp( szToken, "special" ) || !stricmp( szToken, "special1" ) ) + { + pType->m_iDrawCond = CONTENT_SPECIAL1; + } + else if ( !stricmp( szToken, "special2" ) ) + { + pType->m_iDrawCond = CONTENT_SPECIAL2; + } + else if ( !stricmp( szToken, "special3" ) ) + { + pType->m_iDrawCond = CONTENT_SPECIAL3; + } + } + else if ( !stricmp( szToken, "collision" ) ) + { + szFile = gEngfuncs.COM_ParseFile(szFile,szToken); + if ( !stricmp( szToken, "none" ) ) + { + pType->m_iCollision = COLLISION_NONE; + } + else if ( !stricmp( szToken, "die" ) ) + { + pType->m_iCollision = COLLISION_DIE; + } + else if ( !stricmp( szToken, "bounce" ) ) + { + pType->m_iCollision = COLLISION_BOUNCE; + } + } + // get the next token + szFile = gEngfuncs.COM_ParseFile(szFile, szToken); + } + + if (!pType->m_bIsDefined) + { + // if this is a newly-defined type, we need to add it to the list + pType->m_pNext = m_pFirstType; + m_pFirstType = pType; + pType->m_bIsDefined = true; + } + + return pType; +} + +particle *ParticleSystem::ActivateParticle() +{ + particle* pActivated = m_pFreeParticle; + if (pActivated) + { + m_pFreeParticle = pActivated->nextpart; + pActivated->nextpart = m_pActiveParticle; + m_pActiveParticle = pActivated; + } + return pActivated; +} + +extern vec3_t v_origin; + +void ParticleSystem::CalculateDistance() +{ + if (!m_pActiveParticle) + return; + + vec3_t offset = v_origin - m_pActiveParticle->origin; // just pick one + m_fViewerDist = offset[0]*offset[0] + offset[1]*offset[1] + offset[2]*offset[2]; +} + + +bool ParticleSystem::UpdateSystem( float frametime ) +{ + // the entity emitting this system + cl_entity_t *source = UTIL_GetClientEntityWithServerIndex( m_iEntIndex ); + if(!source) return false; + + // Don't update if the system is outside the player's PVS. + if (source->curstate.msg_time < gEngfuncs.GetClientTime()) + { //remove particles + enable = 0; + } + else enable = (source->curstate.renderfx == kRenderFxAurora); + //check for contents to remove + if(m_iKillCondition == gEngfuncs.PM_PointContents(source->curstate.origin, NULL)) + { + enable = 0; + } + if (m_pMainParticle == NULL) + { + if (enable) + { + ParticleType *pType = m_pMainType; + if (pType) + { + m_pMainParticle = pType->CreateParticle(this);//m_pMainParticle); + if (m_pMainParticle) + { + m_pMainParticle->m_iEntIndex = m_iEntIndex; + m_pMainParticle->age_death = -1; // never die + } + } + } + } + else if (!enable) + { + m_pMainParticle->age_death = 0; // die now + m_pMainParticle = NULL; + } + + particle* pParticle = m_pActiveParticle; + particle* pLast = NULL; + + while( pParticle ) + { + if( UpdateParticle( pParticle, frametime ) ) + { + pLast = pParticle; + pParticle = pParticle->nextpart; + } + else // deactivate it + { + if (pLast) + { + pLast->nextpart = pParticle->nextpart; + pParticle->nextpart = m_pFreeParticle; + m_pFreeParticle = pParticle; + pParticle = pLast->nextpart; + } + else // deactivate the first particle in the list + { + m_pActiveParticle = pParticle->nextpart; + pParticle->nextpart = m_pFreeParticle; + m_pFreeParticle = pParticle; + pParticle = m_pActiveParticle; + } + } + } + + return true; + +} + +void ParticleSystem::DrawSystem() +{ + vec3_t normal, forward, right, up; + + if(g_HardwareCapable)//merge particle pos for openGL + { + double r_world_matrix[16]; + glGetDoublev (GL_MODELVIEW_MATRIX, r_world_matrix); + + right.x = r_world_matrix[0]; + right.y = r_world_matrix[4]; + right.z = r_world_matrix[8]; + + up.x = r_world_matrix[1]; + up.y = r_world_matrix[5]; + up.z = r_world_matrix[9]; + } + else //otherwise + { + gEngfuncs.GetViewAngles((float*)normal); + AngleVectors(normal, forward, right, up); + } + particle* pParticle = m_pActiveParticle; + for( pParticle = m_pActiveParticle; pParticle; pParticle = pParticle->nextpart ) + { + DrawParticle( pParticle, right, up ); + } +} + +bool ParticleSystem::ParticleIsVisible( particle* part ) +{ + float dv[3]; + bool visible = true; + VectorSubtract(part->origin, v_origin, dv); + float d = Length(dv); + + if (fabs(d) > 36.0f) + { + visible = (R_SphereInFrustum(part->origin, 100 ) > 0); + } + return visible; +} + +bool ParticleSystem::UpdateParticle(particle *part, float frametime) +{ + if (frametime == 0 ) return true; + part->age += frametime; + + cl_entity_t *source = UTIL_GetClientEntityWithServerIndex( m_iEntIndex ); + + // is this particle bound to an entity? + if (part->m_iEntIndex) + { + if (enable) + { + if(m_iEntAttachment) + { + part->velocity = (source->attachment[m_iEntAttachment - 1] - part->origin)/frametime; + part->origin = source->attachment[m_iEntAttachment - 1]; + } + else + { + part->velocity = (source->curstate.origin - part->origin)/frametime; + part->origin = source->curstate.origin; + } + } + else + { + // entity is switched off, die + return false; + } + } + else + { + // not tied to an entity, check whether it's time to die + if (part->age_death >= 0 && part->age > part->age_death) + return false; + + // apply acceleration and velocity + vec3_t vecOldPos = part->origin; + if (part->m_fDrag) + VectorMA(part->velocity, -part->m_fDrag*frametime, part->velocity - part->m_vecWind, part->velocity); + VectorMA(part->velocity, frametime, part->accel, part->velocity); + VectorMA(part->origin, frametime, part->velocity, part->origin); + + if (part->pType->m_bBouncing) + { + vec3_t vecTarget; + VectorMA(part->origin, frametime, part->velocity, vecTarget); + pmtrace_t *tr = gEngfuncs.PM_TraceLine( part->origin, vecTarget, PM_TRACELINE_PHYSENTSONLY, 2, -1 ); + if (tr->fraction < 1) + { + part->origin = tr->endpos; + float bounceforce = DotProduct(tr->plane.normal, part->velocity); + float newspeed = (1 - part->pType->m_BounceFriction.GetInstance()); + part->velocity = part->velocity * newspeed; + VectorMA(part->velocity, -bounceforce*(newspeed+part->pType->m_Bounce.GetInstance()), tr->plane.normal, part->velocity); + } + } + } + + + // spray children + if (part->age_spray && part->age > part->age_spray) + { + part->age_spray = part->age + 1/part->pType->m_SprayRate.GetInstance(); + + //particle *pChild = ActivateParticle(); + if (part->pType->m_pSprayType) + { + particle *pChild = part->pType->m_pSprayType->CreateParticle(this); + if (pChild) + { + pChild->origin = part->origin; + float fSprayForce = part->pType->m_SprayForce.GetInstance(); + pChild->velocity = part->velocity; + if (fSprayForce) + { + float fSprayPitch = part->pType->m_SprayPitch.GetInstance() - source->curstate.angles.x; + float fSprayYaw = part->pType->m_SprayYaw.GetInstance() - source->curstate.angles.y; + float fSprayRoll = source->curstate.angles.z; + float fForceCosPitch = fSprayForce*CosLookup(fSprayPitch); + pChild->velocity.x += CosLookup(fSprayYaw) * fForceCosPitch; + pChild->velocity.y += SinLookup(fSprayYaw) * fForceCosPitch + SinLookup(fSprayYaw) * fSprayForce * SinLookup(fSprayRoll); + pChild->velocity.z -= SinLookup(fSprayPitch) * fSprayForce * CosLookup(fSprayRoll); + } + } + } + } + + part->m_fSize += part->m_fSizeStep * frametime; + part->m_fAlpha += part->m_fAlphaStep * frametime; + part->m_fRed += part->m_fRedStep * frametime; + part->m_fGreen += part->m_fGreenStep * frametime; + part->m_fBlue += part->m_fBlueStep * frametime; + part->frame += part->m_fFrameStep * frametime; + if (part->m_fAngleStep) + { + part->m_fAngle += part->m_fAngleStep * frametime; + while (part->m_fAngle < 0) part->m_fAngle += 360; + while (part->m_fAngle > 360) part->m_fAngle -= 360; + } + return true; +} + +void ParticleSystem::DrawParticle(particle *part, vec3_t &right, vec3_t &up) +{ + float fSize = part->m_fSize; + vec3_t point1,point2,point3,point4; + vec3_t origin = part->origin; + + // nothing to draw? + if (fSize == 0) return; + + //frustrum visible check + if(!ParticleIsVisible(part)) return; + + float fCosSize = CosLookup(part->m_fAngle)*fSize; + float fSinSize = SinLookup(part->m_fAngle)*fSize; + + // calculate the four corners of the sprite + VectorMA (origin, fSinSize, up, point1); + VectorMA (point1, -fCosSize, right, point1); + + VectorMA (origin, fCosSize, up, point2); + VectorMA (point2, fSinSize, right, point2); + + VectorMA (origin, -fSinSize, up, point3); + VectorMA (point3, fCosSize, right, point3); + + VectorMA (origin, -fCosSize, up, point4); + VectorMA (point4, -fSinSize, right, point4); + + struct model_s * pModel; + int iContents = 0; + + for (particle *pDraw = part; pDraw; pDraw = pDraw->m_pOverlay) + { + if (pDraw->pType->m_hSprite == 0) + continue; + + if (pDraw->pType->m_iDrawCond) + { + if (iContents == 0) + iContents = gEngfuncs.PM_PointContents(origin, NULL); + + if (iContents != pDraw->pType->m_iDrawCond) + continue; + } + + pModel = (struct model_s *)gEngfuncs.GetSpritePointer( pDraw->pType->m_hSprite ); + + //Msg("UpdParticle %d: age %f, life %f, R:%f G:%f, B, %f \n", pDraw->pType->m_hSprite, part->age, part->age_death, pDraw->m_fRed, pDraw->m_fGreen, pDraw->m_fBlue); + + // if we've reached the end of the sprite's frames, loop back + while (pDraw->frame > pModel->numframes) pDraw->frame -= pModel->numframes; + + while (pDraw->frame < 0) pDraw->frame += pModel->numframes; + + if ( !gEngfuncs.pTriAPI->SpriteTexture( pModel, int(pDraw->frame) ))continue; + + gEngfuncs.pTriAPI->RenderMode(pDraw->pType->m_iRenderMode); + gEngfuncs.pTriAPI->Color4f( pDraw->m_fRed, pDraw->m_fGreen, pDraw->m_fBlue, pDraw->m_fAlpha ); + gEngfuncs.pTriAPI->Begin( TRI_QUADS ); + gEngfuncs.pTriAPI->TexCoord2f (0, 0); + gEngfuncs.pTriAPI->Vertex3fv(point1); + + gEngfuncs.pTriAPI->TexCoord2f (1, 0); + gEngfuncs.pTriAPI->Vertex3fv (point2); + + gEngfuncs.pTriAPI->TexCoord2f (1, 1); + gEngfuncs.pTriAPI->Vertex3fv (point3); + + gEngfuncs.pTriAPI->TexCoord2f (0, 1); + gEngfuncs.pTriAPI->Vertex3fv (point4); + gEngfuncs.pTriAPI->End(); + } +} \ No newline at end of file diff --git a/client/r_particle.h b/client/r_particle.h new file mode 100644 index 00000000..d4c61c6f --- /dev/null +++ b/client/r_particle.h @@ -0,0 +1,208 @@ +// 02/08/02 November235: Particle System +#pragma once +#include "r_main.h" + +class ParticleType; +class ParticleSystem; +void CreateAurora( int idx, char *file ); //make new partsystem + +#define COLLISION_NONE 0 +#define COLLISION_DIE 1 +#define COLLISION_BOUNCE 2 + +struct particle +{ + particle* nextpart; + + particle* m_pOverlay; // for making multi-layered particles + + ParticleType *pType; + + vec3_t origin; + vec3_t velocity; + vec3_t accel; + vec3_t m_vecWind; + + int m_iEntIndex; // if non-zero, this particle is tied to the given entity + + float m_fRed; + float m_fGreen; + float m_fBlue; + float m_fRedStep; + float m_fGreenStep; + float m_fBlueStep; + + float m_fAlpha; + float m_fAlphaStep; + + float frame; + float m_fFrameStep; + + float m_fAngle; + float m_fAngleStep; + + float m_fSize; + float m_fSizeStep; + + float m_fDrag; + + float age; + float age_death; + float age_spray; +}; + + +class RandomRange +{ +public: + RandomRange() { m_fMin = m_fMax = 0; m_bDefined = false; } + RandomRange(float fValue) { m_fMin = m_fMax = fValue; m_bDefined = true; } + RandomRange(float fMin, float fMax) { m_fMin = fMin; m_fMax = fMax; m_bDefined = true; } + RandomRange( char *szToken ); + + float m_fMax; + float m_fMin; + bool m_bDefined; + + float GetInstance() + { return gEngfuncs.pfnRandomFloat(m_fMin, m_fMax); } + + float GetOffset(float fBasis) + { return GetInstance()-fBasis; } + + bool IsDefined() + { return m_bDefined; } //(m_fMin == 0 && m_fMax == 0); } +}; + +#define MAX_TYPENAME 30 + +class ParticleType +{ +public: + ParticleType( ParticleType *pNext = NULL ); + ParticleType(char *szFilename); + + bool m_bIsDefined; // is this ParticleType just a placeholder? + int m_iRenderMode; + int m_iDrawCond; + int m_iCollision; + RandomRange m_Bounce; + RandomRange m_BounceFriction; + bool m_bBouncing; + + RandomRange m_Life; + + RandomRange m_StartAlpha; + RandomRange m_EndAlpha; + RandomRange m_StartRed; + RandomRange m_EndRed; + RandomRange m_StartGreen; + RandomRange m_EndGreen; + RandomRange m_StartBlue; + RandomRange m_EndBlue; + + RandomRange m_StartSize; + RandomRange m_SizeDelta; + RandomRange m_EndSize; + + RandomRange m_StartFrame; + RandomRange m_EndFrame; + RandomRange m_FrameRate; // incompatible with EndFrame + bool m_bEndFrame; + + RandomRange m_StartAngle; + RandomRange m_AngleDelta; + + RandomRange m_SprayRate; + RandomRange m_SprayForce; + RandomRange m_SprayPitch; + RandomRange m_SprayYaw; + RandomRange m_SprayRoll; + ParticleType *m_pSprayType; + + RandomRange m_Gravity; + RandomRange m_WindStrength; + RandomRange m_WindYaw; + + HSPRITE m_hSprite; + ParticleType *m_pOverlayType; + + RandomRange m_Drag; + + ParticleType *m_pNext; + + char m_szName[MAX_TYPENAME]; + + // here is a particle system. Add a (set of) particles according to this type, and initialise their values. + particle* CreateParticle(ParticleSystem *pSys);//particle *pPart); + + // initialise this particle. Does not define velocity or age. + void InitParticle(particle *pPart, ParticleSystem *pSys); +}; + +class ParticleSystem +{ +public: + ParticleSystem( int entindex, char *szFilename ); + ~ParticleSystem( void ); + void AllocateParticles( int iParticles ); + void CalculateDistance(); + + ParticleType *GetType( const char *szName ); + ParticleType *AddPlaceholderType( const char *szName ); + ParticleType *ParseType( char *&szFile ); + + cl_entity_t *GetEntity() { return UTIL_GetClientEntityWithServerIndex(m_iEntIndex); } + + static float c_fCosTable[360 + 90]; + static bool c_bCosTableInit; + + // General functions + bool UpdateSystem( float frametime ); //If this function returns false, the manager deletes the system + void DrawSystem(); + particle *ActivateParticle(); // adds one of the free particles to the active list, and returns it for initialisation. + + static float CosLookup(int angle) { return angle < 0? c_fCosTable[angle+360]: c_fCosTable[angle]; } + static float SinLookup(int angle) { return angle < -90? c_fCosTable[angle+450]: c_fCosTable[angle+90]; } + + // returns false if the particle has died + bool UpdateParticle( particle *part, float frametime ); + void DrawParticle( particle* part, vec3_t &right, vec3_t &up ); + + // Utility functions that have to be public + bool ParticleIsVisible( particle* part ); + + // Pointer to next system for linked list structure + ParticleSystem* m_pNextSystem; + + particle* m_pActiveParticle; + float m_fViewerDist; + int m_iEntIndex; + int m_iEntAttachment; + int m_iKillCondition; + int enable; +private: + // the block of allocated particles + particle* m_pAllParticles; + // First particles in the linked list for the active particles and the dead particles + particle* m_pFreeParticle; + particle* m_pMainParticle; // the "source" particle. + + ParticleType *m_pFirstType; + + ParticleType *m_pMainType; +}; + +class ParticleSystemManager +{ +public: + ParticleSystemManager( void ); + ~ParticleSystemManager( void ); + ParticleSystem *FindSystem( cl_entity_t* pEntity ); + void AddSystem( ParticleSystem* ); + void UpdateSystems( void ); + void ClearSystems( void ); + ParticleSystem* m_pFirstSystem; +}; + +extern ParticleSystemManager* g_pParticleSystems; \ No newline at end of file diff --git a/client/r_util.cpp b/client/r_util.cpp new file mode 100644 index 00000000..44c745d1 --- /dev/null +++ b/client/r_util.cpp @@ -0,0 +1,818 @@ +/*** +* +* Copyright (c) 2001-2006, Chain Studios. All rights reserved. +* +* This product contains software technology that is a part of Half-Life FX (R) +* from Chain Studios ("HLFX Technology"). All rights reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Chain Studios. +* +****/ +#include +#include +#include + +#include "hud.h" +#include "r_main.h" +#include "r_util.h" +#include + +vec3_t vec3_origin( 0, 0, 0 ); + +double sqrt(double x); + +HSPRITE m_hHudFont; + +int *GetRect( void ) +{ + RECT wrect; + static int extent[4]; + + if(GetWindowRect( GetActiveWindow(), &wrect )) + { + if(!wrect.left) + { + extent[0] = wrect.left; //+4 + extent[1] = wrect.top; //+30 + extent[2] = wrect.right; //-4 + extent[3] = wrect.bottom; //-4 + } + else + { + extent[0] = wrect.left + 4; //+4 + extent[1] = wrect.top + 30; //+30 + extent[2] = wrect.right - 4; //-4 + extent[3] = wrect.bottom - 4; //-4 + } + } + return extent; +} + +void ScaleColors( int &r, int &g, int &b, int a ) +{ + float x = (float)a / 255; + r = (int)(r * x); + g = (int)(g * x); + b = (int)(b * x); +} + +unsigned int nextpow2(unsigned int x) +{ + unsigned int y = 1; + while (x > y) y = y << 1; + return y; +} + +float Length(const float *v) +{ + int i; + float length; + + length = 0; + for (i=0 ; i< 3 ; i++) + length += v[i]*v[i]; + length = sqrt (length); // FIXME + + return length; +} + +float Distance(const vec3_t v1, const vec3_t v2) +{ + vec3_t d; + VectorSubtract(v2,v1,d); + return Length(d); +} + +void VectorAngles( const float *forward, float *angles ) +{ + float tmp, yaw, pitch; + + if (forward[1] == 0 && forward[0] == 0) + { + yaw = 0; + if (forward[2] > 0) + pitch = 90; + else + pitch = 270; + } + else + { + yaw = (atan2(forward[1], forward[0]) * 180 / M_PI); + if (yaw < 0) + yaw += 360; + + tmp = sqrt (forward[0]*forward[0] + forward[1]*forward[1]); + pitch = (atan2(forward[2], tmp) * 180 / M_PI); + if (pitch < 0) + pitch += 360; + } + + angles[0] = pitch; + angles[1] = yaw; + angles[2] = 0; +} + +float VectorNormalize (float *v) +{ + float length, ilength; + + length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2]; + length = sqrt (length); // FIXME + + if (length) + { + ilength = 1/length; + v[0] *= ilength; + v[1] *= ilength; + v[2] *= ilength; + } + + return length; + +} + +void VectorInverse ( float *v ) +{ + v[0] = -v[0]; + v[1] = -v[1]; + v[2] = -v[2]; +} + +void VectorScale (const float *in, float scale, float *out) +{ + out[0] = in[0]*scale; + out[1] = in[1]*scale; + out[2] = in[2]*scale; +} + +void VectorMA (const float *veca, float scale, const float *vecb, float *vecc) +{ + vecc[0] = veca[0] + scale*vecb[0]; + vecc[1] = veca[1] + scale*vecb[1]; + vecc[2] = veca[2] + scale*vecb[2]; +} + +HSPRITE LoadSprite(const char *pszName) +{ + int i; + char sz[256]; + + if (ScreenWidth < 640) + i = 320; + else + i = 640; + + sprintf(sz, pszName, i); + + return SPR_Load(sz); +} + +float TransformColor ( float color ) +{ + float trns_clr; + if(color >= 0 ) trns_clr = color / 255.0f; + else trns_clr = 1.0;//default value + return trns_clr; +} + +#define NOISE_SIZE 64 + +inline float fade ( float t ) +{ + return t * t * t * (t * (t * 6 - 15) + 10); +} + +inline float lerp ( float t, float a, float b ) +{ + return a + t * (b - a); +} + +inline float grad ( int hash, float x, float y, float z ) +{ + int h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE + float u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS. + v = h<4 ? y : h==12 || h==14 ? x : z; + + return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v); +} + +int permutation [256] = +{ + 151,160,137,91,90,15, + 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, + 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, + 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, + 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, + 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, + 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, + 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, + 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, + 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, + 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, + 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, + 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 +}; + +int p[512]; + +void InitImprovedNoise() +{ + for ( int i = 0; i < 256; i++ ) + p [ 256 + i ] = p [ i ] = permutation [i]; +} + +float noise( float vx, float vy, float vz ) +{ + + float fx = floor ( vx ); // get floor values of coords + float fy = floor ( vy ); + float fz = floor ( vz ); + + int X = ((int) fx) & 255, // FIND UNIT CUBE THAT + Y = ((int) fy) & 255, // CONTAINS POINT. + Z = ((int) fz) & 255; + + float x = vx - fx; // FIND RELATIVE X,Y,Z + float y = vy - fy; // OF POINT IN CUBE. + float z = vz - fz; + + float u = fade ( x ), // COMPUTE FADE CURVES + v = fade ( y ), // FOR EACH OF X,Y,Z. + w = fade ( z ); + + int a = p [X ]+Y, aa = p [a]+Z, ab = p [a+1]+Z, // HASH COORDINATES OF + b = p [X+1]+Y, ba = p [b]+Z, bb = p [b+1]+Z; // THE 8 CUBE CORNERS, + + return lerp(w, lerp(v, lerp(u, grad(p[aa ], x , y , z ), // AND ADD + grad(p[ba ], x-1, y , z )), // BLENDED + lerp(u, grad(p[ab ], x , y-1, z ), // RESULTS + grad(p[bb ], x-1, y-1, z ))),// FROM 8 + lerp(v, lerp(u, grad(p[aa+1], x , y , z-1 ), // CORNERS + grad(p[ba+1], x-1, y , z-1 )), // OF CUBE + lerp(u, grad(p[ab+1], x , y-1, z-1 ), + grad(p[bb+1], x-1, y-1, z-1 )))); +} + + +void CreateNoiseTexture3D (float scale) +{ + int i; + + GLfloat *img = new GLfloat[NOISE_SIZE * NOISE_SIZE * NOISE_SIZE * 3]; + GLfloat *p = img; + + InitImprovedNoise(); + + for (i=0; i < NOISE_SIZE*NOISE_SIZE*NOISE_SIZE*3 ; i++) + { + float x = i + sin(i); + float y = i + cos(i); + *p++ = noise(x, y, i); + } + + g_uiNoiseTex = pTexId++; + glBindTexture(GL_TEXTURE_3D, g_uiNoiseTex); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage3DEXT(GL_TEXTURE_3D, 0, 3, NOISE_SIZE, NOISE_SIZE, NOISE_SIZE, 0, + GL_RGB, GL_FLOAT, img); + + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT ); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT ); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT ); + + delete [] img; +} + +// +// R_SphereInFrustum +// +// Purpose: sphere visibility test +// +extern float frustumPlanes[6][4]; + +float R_SphereInFrustum( vec3_t o, float radius ) +{ + int p; + float d; + + for( p = 0; p < 6; p++ ) + { + d = frustumPlanes[p][0] * o[0] + frustumPlanes[p][1] * o[1] + frustumPlanes[p][2] * o[2] + frustumPlanes[p][3]; + if( d <= -radius ) + return 0; + } + return d + radius; +} + +// +// UTIL_GetClientEntityWithServerIndex +// +// Purpose: searches for the server entity on client +// Assumes: colormap has server entity index +// +cl_entity_t *UTIL_GetClientEntityWithServerIndex( int sv_index ) +{ + cl_entity_t *e; + + for (int ic=1;icmodel) + continue; + + if (e->curstate.colormap == sv_index) + return e; + } + + return NULL; +} + +/* +==================== +AngleMatrix + +==================== +*/ +void AngleMatrix (const float *angles, float (*matrix)[4] ) +{ + float angle; + float sr, sp, sy, cr, cp, cy; + + angle = angles[YAW] * (M_PI*2 / 360); + sy = sin(angle); + cy = cos(angle); + angle = angles[PITCH] * (M_PI*2 / 360); + sp = sin(angle); + cp = cos(angle); + angle = angles[ROLL] * (M_PI*2 / 360); + sr = sin(angle); + cr = cos(angle); + + // matrix = (YAW * PITCH) * ROLL + matrix[0][0] = cp*cy; + matrix[1][0] = cp*sy; + matrix[2][0] = -sp; + matrix[0][1] = sr*sp*cy+cr*-sy; + matrix[1][1] = sr*sp*sy+cr*cy; + matrix[2][1] = sr*cp; + matrix[0][2] = (cr*sp*cy+-sr*-sy); + matrix[1][2] = (cr*sp*sy+-sr*cy); + matrix[2][2] = cr*cp; + matrix[0][3] = 0.0; + matrix[1][3] = 0.0; + matrix[2][3] = 0.0; +} + +/* +==================== +VectorCompare + +==================== +*/ +int VectorCompare (const float *v1, const float *v2) +{ + int i; + + for (i=0 ; i<3 ; i++) + if (v1[i] != v2[i]) return 0; + + return 1; +} + +/* +==================== +CrossProduct + +==================== +*/ +void CrossProduct (const float *v1, const float *v2, float *cross) +{ + cross[0] = v1[1]*v2[2] - v1[2]*v2[1]; + cross[1] = v1[2]*v2[0] - v1[0]*v2[2]; + cross[2] = v1[0]*v2[1] - v1[1]*v2[0]; +} + +/* +==================== +VectorTransform + +==================== +*/ +void VectorTransform (const float *in1, float in2[3][4], float *out) +{ + out[0] = DotProduct(in1, in2[0]) + in2[0][3]; + out[1] = DotProduct(in1, in2[1]) + in2[1][3]; + out[2] = DotProduct(in1, in2[2]) + in2[2][3]; +} + +/* +================ +ConcatTransforms + +================ +*/ +void ConcatTransforms (float in1[3][4], float in2[3][4], float out[3][4]) +{ + out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] + + in1[0][2] * in2[2][0]; + out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] + + in1[0][2] * in2[2][1]; + out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] + + in1[0][2] * in2[2][2]; + out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] + + in1[0][2] * in2[2][3] + in1[0][3]; + out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] + + in1[1][2] * in2[2][0]; + out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] + + in1[1][2] * in2[2][1]; + out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] + + in1[1][2] * in2[2][2]; + out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] + + in1[1][2] * in2[2][3] + in1[1][3]; + out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] + + in1[2][2] * in2[2][0]; + out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] + + in1[2][2] * in2[2][1]; + out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] + + in1[2][2] * in2[2][2]; + out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] + + in1[2][2] * in2[2][3] + in1[2][3]; +} + +// angles index are not the same as ROLL, PITCH, YAW + +/* +==================== +AngleQuaternion + +==================== +*/ +void AngleQuaternion( float *angles, vec4_t quaternion ) +{ + float angle; + float sr, sp, sy, cr, cp, cy; + + // FIXME: rescale the inputs to 1/2 angle + angle = angles[2] * 0.5; + sy = sin(angle); + cy = cos(angle); + angle = angles[1] * 0.5; + sp = sin(angle); + cp = cos(angle); + angle = angles[0] * 0.5; + sr = sin(angle); + cr = cos(angle); + + quaternion[0] = sr*cp*cy-cr*sp*sy; // X + quaternion[1] = cr*sp*cy+sr*cp*sy; // Y + quaternion[2] = cr*cp*sy-sr*sp*cy; // Z + quaternion[3] = cr*cp*cy+sr*sp*sy; // W +} + +/* +==================== +QuaternionSlerp + +==================== +*/ +void QuaternionSlerp( vec4_t p, vec4_t q, float t, vec4_t qt ) +{ + int i; + float omega, cosom, sinom, sclp, sclq; + + // decide if one of the quaternions is backwards + float a = 0; + float b = 0; + + for (i = 0; i < 4; i++) + { + a += (p[i]-q[i])*(p[i]-q[i]); + b += (p[i]+q[i])*(p[i]+q[i]); + } + if (a > b) + { + for (i = 0; i < 4; i++) + { + q[i] = -q[i]; + } + } + + cosom = p[0]*q[0] + p[1]*q[1] + p[2]*q[2] + p[3]*q[3]; + + if ((1.0 + cosom) > 0.000001) + { + if ((1.0 - cosom) > 0.000001) + { + omega = acos( cosom ); + sinom = sin( omega ); + sclp = sin( (1.0 - t)*omega) / sinom; + sclq = sin( t*omega ) / sinom; + } + else + { + sclp = 1.0 - t; + sclq = t; + } + for (i = 0; i < 4; i++) { + qt[i] = sclp * p[i] + sclq * q[i]; + } + } + else + { + qt[0] = -q[1]; + qt[1] = q[0]; + qt[2] = -q[3]; + qt[3] = q[2]; + sclp = sin( (1.0 - t) * (0.5 * M_PI)); + sclq = sin( t * (0.5 * M_PI)); + for (i = 0; i < 3; i++) + { + qt[i] = sclp * p[i] + sclq * qt[i]; + } + } +} + +/* +==================== +QuaternionMatrix + +==================== +*/ +void QuaternionMatrix( vec4_t quaternion, float (*matrix)[4] ) +{ + matrix[0][0] = 1.0 - 2.0 * quaternion[1] * quaternion[1] - 2.0 * quaternion[2] * quaternion[2]; + matrix[1][0] = 2.0 * quaternion[0] * quaternion[1] + 2.0 * quaternion[3] * quaternion[2]; + matrix[2][0] = 2.0 * quaternion[0] * quaternion[2] - 2.0 * quaternion[3] * quaternion[1]; + + matrix[0][1] = 2.0 * quaternion[0] * quaternion[1] - 2.0 * quaternion[3] * quaternion[2]; + matrix[1][1] = 1.0 - 2.0 * quaternion[0] * quaternion[0] - 2.0 * quaternion[2] * quaternion[2]; + matrix[2][1] = 2.0 * quaternion[1] * quaternion[2] + 2.0 * quaternion[3] * quaternion[0]; + + matrix[0][2] = 2.0 * quaternion[0] * quaternion[2] + 2.0 * quaternion[3] * quaternion[1]; + matrix[1][2] = 2.0 * quaternion[1] * quaternion[2] - 2.0 * quaternion[3] * quaternion[0]; + matrix[2][2] = 1.0 - 2.0 * quaternion[0] * quaternion[0] - 2.0 * quaternion[1] * quaternion[1]; +} + +/* +==================== +MatrixCopy + +==================== +*/ +void MatrixCopy( float in[3][4], float out[3][4] ) +{ + memcpy( out, in, sizeof( float ) * 3 * 4 ); +} + +/* +==================== +VectorIRotate + +==================== +*/ +void VectorIRotate (const vec3_t in1, const float in2[3][4], vec3_t out) +{ + out[0] = in1[0]*in2[0][0] + in1[1]*in2[1][0] + in1[2]*in2[2][0]; + out[1] = in1[0]*in2[0][1] + in1[1]*in2[1][1] + in1[2]*in2[2][1]; + out[2] = in1[0]*in2[0][2] + in1[1]*in2[1][2] + in1[2]*in2[2][2]; +} + +/* +==================== +VectorRotateByMatrix +VectorTransformByMatrix +==================== +*/ +void VectorRotateByMatrix (const vec3_t &in1, const float *in2, vec3_t &out) +{ + out[0] = in1[0]*in2[0] + in1[1]*in2[4] + in1[2]*in2[8]; + out[1] = in1[0]*in2[1] + in1[1]*in2[5] + in1[2]*in2[9]; + out[2] = in1[0]*in2[2] + in1[1]*in2[6] + in1[2]*in2[10]; +} + +void VectorTransformByMatrix (const vec3_t &in1, const float *in2, vec3_t &out) +{ + out[0] = in1[0]*in2[0] + in1[1]*in2[4] + in1[2]*in2[8] + in2[12]; + out[1] = in1[0]*in2[1] + in1[1]*in2[5] + in1[2]*in2[9] + in2[13]; + out[2] = in1[0]*in2[2] + in1[1]*in2[6] + in1[2]*in2[10] + in2[14]; +} + +void VectorRotateByMatrix (const float *in1, const float *in2, float *out) +{ + out[0] = in1[0]*in2[0] + in1[1]*in2[4] + in1[2]*in2[8]; + out[1] = in1[0]*in2[1] + in1[1]*in2[5] + in1[2]*in2[9]; + out[2] = in1[0]*in2[2] + in1[1]*in2[6] + in1[2]*in2[10]; +} + +void VectorTransformByMatrix (const float *in1, const float *in2, float *out) +{ + out[0] = in1[0]*in2[0] + in1[1]*in2[4] + in1[2]*in2[8] + in2[12]; + out[1] = in1[0]*in2[1] + in1[1]*in2[5] + in1[2]*in2[9] + in2[13]; + out[2] = in1[0]*in2[2] + in1[1]*in2[6] + in1[2]*in2[10] + in2[14]; +} + +/* +==================== +ObjectToWorldMatrix + +==================== +*/ +void ObjectToWorldMatrix(cl_entity_t *e, float *result) +{ + glPushMatrix(); + glLoadIdentity(); + + glTranslatef(e->origin[0], e->origin[1], e->origin[2]); + glRotatef ( e->angles[1], 0, 0, 1); + glRotatef ( e->angles[0], 0, 1, 0); + glRotatef ( e->angles[2], 1, 0, 0); + + glGetFloatv (GL_MODELVIEW_MATRIX, result); + glPopMatrix(); +} + +/* +==================== +SPRITE_GetList + +==================== +*/ + +char *ParseHudSprite( char *pfile, char *psz, client_sprite_t *result ) +{ + char token[256]; + client_sprite_t *p = new client_sprite_t; + int x = 0, y = 0, width = 0, height = 0; + int section = 0; + + memset( p, 0, sizeof(client_sprite_t) ); + + while( pfile ) + { + pfile = gEngfuncs.COM_ParseFile( pfile, token ); + + if( !stricmp( token, psz )) + { + pfile = gEngfuncs.COM_ParseFile( pfile, token ); + if( !stricmp( token, "{" )) section = 1; + } + if(section)//parse section + { + if( !stricmp( token, "}" )) break;//end section + + if ( !stricmp( token, "file" )) + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + strcpy(p->szSprite, token ); + if( !gEngfuncs.COM_LoadFile( p->szSprite, 5, NULL )) return pfile; + else + { + gEngfuncs.COM_FreeFile( p->szSprite); + //fill structure at default + HSPRITE m_hSprite = SPR_Load(p->szSprite); + x = y = 0; + width = SPR_Width( m_hSprite, 0 ); + height = SPR_Height( m_hSprite, 0 ); + } + } + else if ( !stricmp( token, "name" )) + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + strcpy(p->szName, token ); + } + else if ( !stricmp( token, "x" )) + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + x = atoi(token); + } + else if ( !stricmp( token, "y" )) + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + y = atoi(token); + } + else if ( !stricmp( token, "width" )) + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + width = atoi(token); + } + else if ( !stricmp( token, "height" )) + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + height = atoi(token); + } + } + } + + if(!section) return pfile;//data not found + + //calculate sprite position + p->rc.left = x; + p->rc.right = x + width; + p->rc.top = y; + p->rc.bottom = y + height; + + //write resolution for backward compatibility + if (ScreenWidth < 640) p->iRes = 320; + else p->iRes = 640; + + memcpy( result, p, sizeof(client_sprite_t)); + return pfile; +} + +client_sprite_t *SPR_GetList( char *psz, int *piCount ) +{ + int iSprCount = 0; + char *pfile = (char *)gEngfuncs.COM_LoadFile( psz, 5, NULL); + if (!pfile) + { + *piCount = iSprCount; + return NULL; + } + + char token[256]; + char *plist = pfile; + + while ( pfile ) //calculate count of sprites + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + if ( !stricmp( token, "hudsprite" )) iSprCount++; + } + + client_sprite_t *phud = new client_sprite_t[iSprCount]; + + for(int i = 0; i < iSprCount; i++ ) //parse structures + { + plist = ParseHudSprite( plist, "hudsprite", &phud[i] ); + } + + if(!iSprCount)Msg("SPR_GetList: %s don't have sprites\n", psz ); + gEngfuncs.COM_FreeFile( pfile ); + + *piCount = iSprCount; + return phud; +} + +/* +==================== +Sys LoadGameDLL + +==================== +*/ +bool Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts) +{ + const dllfunction_t *gamefunc; + char dllpath[128]; + dllhandle_t dllhandle = 0; + + if (handle == NULL) return false; + + // Initializations + for (gamefunc = fcts; gamefunc && gamefunc->name != NULL; gamefunc++) + *gamefunc->funcvariable = NULL; + + // Try every possible name + + + sprintf(dllpath, "%s/cl_dlls/%s", gEngfuncs.pfnGetGameDirectory(), dllname); + dllhandle = LoadLibrary (dllpath); + + // No DLL found + if (! dllhandle) return false; + + // Get the function adresses + for (gamefunc = fcts; gamefunc && gamefunc->name != NULL; gamefunc++) + if (!(*gamefunc->funcvariable = (void *) Sys_GetProcAddress (dllhandle, gamefunc->name))) + { + Sys_UnloadLibrary (&dllhandle); + return false; + } + + Msg("%s loaded succesfully!\n", dllname); + *handle = dllhandle; + return true; +} + + +void Sys_UnloadLibrary (dllhandle_t* handle) +{ + if (handle == NULL || *handle == NULL) + return; + + FreeLibrary (*handle); + *handle = NULL; +} + +void* Sys_GetProcAddress (dllhandle_t handle, const char* name) +{ + return (void *)GetProcAddress (handle, name); +} + diff --git a/client/r_view.cpp b/client/r_view.cpp new file mode 100644 index 00000000..c23ac644 --- /dev/null +++ b/client/r_view.cpp @@ -0,0 +1,1117 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// r_view.cpp - multipass renderer +//======================================================================= + +#include "hud.h" +#include "r_main.h" +#include "r_view.h" +#include "r_util.h" +#include "cvardef.h" +#include "usercmd.h" +#include "const.h" +#include "entity_state.h" +#include "cl_entity.h" +#include "ref_params.h" +#include "in_defs.h" +#include "pm_movevars.h" +#include "pm_shared.h" +#include "pm_defs.h" +#include "event_api.h" +#include "pmtrace.h" +#include "screenfade.h" +#include "shake.h" +#include "studio.h" +#include "r_studioint.h" +#include "r_studio.h" +#include "com_model.h" + +//============================================================================== +// INIT GLOBAL VARIABLES & CONSTANTS +//============================================================================== + +//fundamental engine callbacks +extern "C" +{ + void DLLEXPORT V_CalcRefdef( struct ref_params_s *pparams ); + void DLLEXPORT CAM_Think( void ); + int DLLEXPORT CL_IsThirdPerson( void ); + void DLLEXPORT CL_CameraOffset( float *ofs ); + float vJumpOrigin[3]; + float vJumpAngles[3]; +} + +//fog external variables +extern vec3_t FogColor; +extern float g_fStartDist; +extern float g_fEndDist; +extern engine_studio_api_t IEngineStudio;//"view at monster" camera use this +extern CGameStudioModelRenderer g_StudioRenderer;//control over player drawing + +float v_idlescale, v_lastDistance; + +//global view containers +vec3_t v_origin, v_angles, v_cl_angles;//base client vectors +vec3_t ev_punchangle; //client punchangles + +extern int PM_GetPhysEntInfo( int ent );//thirdperson clip camera use this +extern float Distance(const vec3_t v1, const vec3_t v2); +static viewinterp_t ViewInterp; +cl_entity_t *spot; +int pause = 0; + +//thirdperson camera +void DLLEXPORT CAM_Think( void ){} +void DLLEXPORT CL_CameraOffset( float *ofs ) {VectorCopy( vec3_origin, ofs );} +int DLLEXPORT CL_IsThirdPerson( void ){return (gHUD.m_iCameraMode ? 1 : 0 );} + +//View CVARS +cvar_t *scr_ofsx; +cvar_t *scr_ofsy; +cvar_t *scr_ofsz; +extern cvar_t *cl_forwardspeed; +extern cvar_t *scr_ofsx, *scr_ofsy, *scr_ofsz; +extern cvar_t *cl_vsmoothing; + +cvar_t *cl_bobcycle; +cvar_t *cl_bob; +cvar_t *cl_bobup; +cvar_t *cl_waterdist; +cvar_t *cl_chasedist; +cvar_t *v_centermove; +cvar_t *v_centerspeed; + +cvar_t v_iyaw_cycle = {"v_iyaw_cycle", "2", 0, 2}; +cvar_t v_iroll_cycle = {"v_iroll_cycle", "0.5", 0, 0.5}; +cvar_t v_ipitch_cycle = {"v_ipitch_cycle", "1", 0, 1}; +cvar_t v_iyaw_level = {"v_iyaw_level", "0.3", 0, 0.3}; +cvar_t v_iroll_level = {"v_iroll_level", "0.1", 0, 0.1}; +cvar_t v_ipitch_level = {"v_ipitch_level", "0.3", 0, 0.3}; +//============================================================================== +// PASS MANAGER GLOBALS +//============================================================================== +//render manager global variables +bool g_FirstFrame = false; +bool g_RenderReady = false; +bool g_bFinalPass = false; +bool g_bEndCalc = false; +int m_RenderRefCount = 0; //refcounter (use for debug) + +//passes info +bool g_bMirrorShouldpass = false; +bool g_bScreenShouldpass = false; +bool g_bPortalShouldpass = false; +bool g_bSkyShouldpass = false; +bool g_bMirrorPass = false; +bool g_bPortalPass = false; +bool g_bScreenPass = false; +bool g_bSkyPass = false; + +//base origin and angles +vec3_t g_vecBaseOrigin; //base origin - transformed always +vec3_t g_vecBaseAngles; //base angles - transformed always +vec3_t g_vecCurrentOrigin; //current origin +vec3_t g_vecCurrentAngles; //current angles +//============================================================================== +// VIEW RENDERING +//============================================================================== + +//========================== +// V_ThirdPerson +//========================== +void V_ThirdPerson(void) +{ + if ( gEngfuncs.GetMaxClients() > 1 ) return;//no thirdperson in multiplayer + if (!gHUD.viewFlags) + gHUD.m_iLastCameraMode = gHUD.m_iCameraMode = 1; + else gHUD.m_iLastCameraMode = 1; //set new view after release camera +} + +//========================== +// V_FirstPerson +//========================== +void V_FirstPerson(void) +{ + if (!gHUD.viewFlags) + gHUD.m_iLastCameraMode = gHUD.m_iCameraMode = 0; + else gHUD.m_iLastCameraMode = 0; //set new view after release camera +} + +//========================== +// V_Init +//========================== +void V_Init (void) +{ + gEngfuncs.pfnAddCommand ("centerview", V_StartPitchDrift ); + + scr_ofsx = gEngfuncs.pfnRegisterVariable( "scr_ofsx","0", 0 ); + scr_ofsy = gEngfuncs.pfnRegisterVariable( "scr_ofsy","0", 0 ); + scr_ofsz = gEngfuncs.pfnRegisterVariable( "scr_ofsz","0", 0 ); + + v_centermove = gEngfuncs.pfnRegisterVariable( "v_centermove", "0.15", 0 ); + v_centerspeed = gEngfuncs.pfnRegisterVariable( "v_centerspeed","500", 0 ); + + cl_bobcycle = gEngfuncs.pfnRegisterVariable( "cl_bobcycle","0.8", 0 ); + cl_bob = gEngfuncs.pfnRegisterVariable( "cl_bob","0.01", 0 ); + cl_bobup = gEngfuncs.pfnRegisterVariable( "cl_bobup","0.5", 0 ); + cl_waterdist = gEngfuncs.pfnRegisterVariable( "cl_waterdist","4", 0 ); + cl_chasedist = gEngfuncs.pfnRegisterVariable( "cl_chasedist","112", 0 ); + gEngfuncs.pfnAddCommand ( "thirdperson", V_ThirdPerson ); + gEngfuncs.pfnAddCommand ( "firstperson", V_FirstPerson ); +} + +//========================== +// V_StartPitchDrift +//========================== +void V_StartPitchDrift( void ) +{ + if ( pd.laststop == gEngfuncs.GetClientTime())return; + if ( pd.nodrift || !pd.pitchvel ) + { + pd.pitchvel = v_centerspeed->value; + pd.nodrift = 0; + pd.driftmove = 0; + } +} + +//========================== +// V_StopPitchDrift +//========================== +void V_StopPitchDrift ( void ) +{ + pd.laststop = gEngfuncs.GetClientTime(); + pd.nodrift = 1; + pd.pitchvel = 0; +} + +//========================== +// V_DriftPitch +//========================== +void V_DriftPitch ( struct ref_params_s *pparams ) +{ + float delta, move; + + if( gEngfuncs.IsNoClipping() || !pparams->onground || pparams->demoplayback ) + { + pd.driftmove = 0; + pd.pitchvel = 0; + return; + } + + if (pd.nodrift) + { + if ( fabs( pparams->cmd->forwardmove ) < cl_forwardspeed->value ) + pd.driftmove = 0; + else pd.driftmove += pparams->frametime; + if ( pd.driftmove > v_centermove->value)V_StartPitchDrift (); + return; + } + + delta = pparams->idealpitch - pparams->cl_viewangles[PITCH]; + + if (!delta) + { + pd.pitchvel = 0; + return; + } + + move = pparams->frametime * pd.pitchvel; + pd.pitchvel += pparams->frametime * v_centerspeed->value; + + if (delta > 0) + { + if (move > delta) + { + pd.pitchvel = 0; + move = delta; + } + pparams->cl_viewangles[PITCH] += move; + } + else if (delta < 0) + { + if (move > -delta) + { + pd.pitchvel = 0; + move = -delta; + } + pparams->cl_viewangles[PITCH] -= move; + } +} + +//========================== +// V_DropPunchAngle +//========================== +void V_DropPunchAngle( float frametime, float *ev_punchangle ) +{ + float len; + len = VectorNormalize ( ev_punchangle ); + len -= (10.0 + len * 0.5) * frametime; + len = max( len, 0.0 ); + VectorScale ( ev_punchangle, len, ev_punchangle ); +} + +//========================== +// V_CalcGunAngle +//========================== +void V_CalcGunAngle( struct ref_params_s *pparams ) +{ + cl_entity_t *viewent; + + viewent = gEngfuncs.GetViewModel(); + if ( !viewent )return; + + viewent->angles[YAW] = pparams->viewangles[YAW] + pparams->crosshairangle[YAW]; + viewent->angles[PITCH] = -pparams->viewangles[PITCH] + pparams->crosshairangle[PITCH] * 0.25; + viewent->angles[ROLL] -= v_idlescale * sin(pparams->time*v_iroll_cycle.value) * v_iroll_level.value; + + // don't apply all of the v_ipitch to prevent normally unseen parts of viewmodel from coming into view. + viewent->angles[PITCH] -= v_idlescale * sin(pparams->time*v_ipitch_cycle.value) * (v_ipitch_level.value * 0.5); + viewent->angles[YAW] -= v_idlescale * sin(pparams->time*v_iyaw_cycle.value) * v_iyaw_level.value; + + VectorCopy( viewent->angles, viewent->curstate.angles ); + VectorCopy( viewent->angles, viewent->latched.prevangles ); +} + +//========================== +// V_CalcGlobalFog +//========================== +void V_CalcGlobalFog( struct ref_params_s *pparams ) +{ + float g_fFogColor[4] = { FogColor.x, FogColor.y, FogColor.z, 1.0 }; + bool bFog = pparams->waterlevel < 2 && g_fStartDist > 0 && g_fEndDist > 0; + if (!bFog) return; + + //rendering global fog + glEnable(GL_FOG); + glFogf(GL_FOG_DENSITY, 0.0025); + glFogi(GL_FOG_MODE, GL_LINEAR); + glFogfv(GL_FOG_COLOR, g_fFogColor); + glFogf(GL_FOG_START, g_fStartDist); + glFogf(GL_FOG_END, g_fEndDist); + glHint(GL_FOG_HINT, GL_NICEST); +} + +//========================== +// V_CalcBob +//========================== +float V_CalcBob ( struct ref_params_s *pparams ) +{ + static double bobtime; + static float bob; + float cycle; + static float lasttime; + vec3_t vel; + + if ( pparams->onground == -1 || pparams->time == lasttime ) return bob; + lasttime = pparams->time; + + bobtime += pparams->frametime; + cycle = bobtime - (int)( bobtime / cl_bobcycle->value ) * cl_bobcycle->value; + cycle /= cl_bobcycle->value; + + if ( cycle < cl_bobup->value )cycle = M_PI * cycle / cl_bobup->value; + else cycle = M_PI + M_PI * ( cycle - cl_bobup->value )/( 1.0 - cl_bobup->value ); + + VectorCopy( pparams->simvel, vel ); + vel[2] = 0; + + bob = sqrt( vel[0] * vel[0] + vel[1] * vel[1] ) * cl_bob->value; + bob = bob * 0.3 + bob * 0.7 * sin(cycle); + bob = min( bob, 4 ); + bob = max( bob, -7 ); + return bob; +} + +//========================== +// V_AddIdle +//========================== +void V_AddIdle( struct ref_params_s *pparams ) +{ + pparams->viewangles[ROLL] += v_idlescale * sin(pparams->time*v_iroll_cycle.value) * v_iroll_level.value; + pparams->viewangles[PITCH] += v_idlescale * sin(pparams->time*v_ipitch_cycle.value) * v_ipitch_level.value; + pparams->viewangles[YAW] += v_idlescale * sin(pparams->time*v_iyaw_cycle.value) * v_iyaw_level.value; +} + +//========================== +// V_CalcViewRoll +//========================== +void V_CalcViewRoll( struct ref_params_s *pparams ) +{ + float sign, side, value; + vec3_t forward, right, up; + cl_entity_t *viewentity; + + viewentity = gEngfuncs.GetEntityByIndex( pparams->viewentity ); + if ( !viewentity ) return; + + AngleVectors ( viewentity->angles, forward, right, up ); + side = DotProduct (pparams->simvel, right); + sign = side < 0 ? -1 : 1; + side = fabs( side ); + value = pparams->movevars->rollangle; + if (side < pparams->movevars->rollspeed)side = side * value / pparams->movevars->rollspeed; + else side = value; + side = side * sign; + pparams->viewangles[ROLL] += side; + + if ( pparams->health <= 0 && ( pparams->viewheight[2] != 0 ) ) + { + pparams->viewangles[ROLL] = 80; // dead view angle + return; + } +} + +//========================== +// V_SetViewportRefdef +//========================== +void V_SetViewportRefdef (struct ref_params_s *pparams) +{ + pparams->viewport[0] = 0; + pparams->viewport[1] = 0; + pparams->viewport[2] = VIEWPORT_SIZE; + pparams->viewport[3] = VIEWPORT_SIZE; +} + +//========================== +// V_KillViewportRefdef +//========================== +void V_KillViewportRefdef(struct ref_params_s *pparams) +{ + pparams->viewport[0] = 0; + pparams->viewport[1] = 0; + pparams->viewport[2] = 1; + pparams->viewport[3] = 1; +} + +//========================== +// V_ResetViewportRefdef +//========================== +void V_ResetViewportRefdef (struct ref_params_s *pparams) +{ + pparams->viewport[0] = 0; + pparams->viewport[1] = 0; + pparams->viewport[2] = ScreenWidth; + pparams->viewport[3] = ScreenHeight; +} + +//========================== +// V_GetChaseOrigin +//========================== +void V_GetChaseOrigin( float * angles, float * origin, float distance, float * returnvec ) +{ + vec3_t vecEnd; + vec3_t forward; + vec3_t vecStart; + pmtrace_t * trace; + int maxLoops = 8; + + int ignoreent = -1; // first, ignore no entity + cl_entity_t* ent = NULL; + + // Trace back from the target using the player's view angles + AngleVectors(angles, forward, NULL, NULL); + VectorScale(forward,-1,forward); + VectorCopy( origin, vecStart ); + VectorMA(vecStart, distance , forward, vecEnd); + + while ( maxLoops > 0) + { + trace = gEngfuncs.PM_TraceLine( vecStart, vecEnd, PM_TRACELINE_PHYSENTSONLY, 2, ignoreent ); + if ( trace->ent <= 0) break;// we hit the world or nothing, stop trace + + ent = gEngfuncs.GetEntityByIndex( PM_GetPhysEntInfo( trace->ent ) ); + if ( ent == NULL ) break; + + // hit non-player solid BSP , stop here + if ( ent->curstate.solid == SOLID_BSP && !ent->player ) break; + + // if close enought to end pos, stop, otherwise continue trace + if( Distance(trace->endpos, vecEnd ) < 1.0f )break; + else + { + ignoreent = trace->ent; // ignore last hit entity + VectorCopy( trace->endpos, vecStart); + } + maxLoops--; + } + VectorMA( trace->endpos, 4, trace->plane.normal, returnvec ); + v_lastDistance = Distance(trace->endpos, origin); // real distance without offset +} + +//========================== +// V_GetChasePos +//========================== +void V_GetChasePos(cl_entity_t *ent, float *cl_angles, float *origin, float *angles) +{ + if (!ent) + { + // just copy a save in-map position + VectorCopy ( vJumpAngles, angles ); + VectorCopy ( vJumpOrigin, origin ); + return; + } + if ( cl_angles == NULL ) // no mouse angles given, use entity angles ( locked mode ) + { + VectorCopy ( ent->angles, angles); + angles[0]*=-1; + } + else VectorCopy ( cl_angles, angles); + + VectorCopy ( ent->origin, origin); + origin[2]+= 28; // DEFAULT_VIEWHEIGHT - some offset + V_GetChaseOrigin( angles, origin, cl_chasedist->value, origin ); +} + +//========================== +// V_CalcNextView +//========================== +void V_CalcNextView (struct ref_params_s *pparams) +{ + if (g_FirstFrame)//first time not actually + { + g_FirstFrame = false; + g_RenderReady = false; + + //First time in this function + GL_PreRender();//this set g_RenderReady at true if all is'ok + + if (!g_RenderReady )//no hardware capable? + { + g_bMirrorShouldpass = false; + g_bScreenShouldpass = false; + g_bPortalShouldpass = false; + g_bSkyShouldpass = (gHUD.m_iSkyMode ? true : false);//sky must drawing in D3D + } + else //all is'ok + { + g_bMirrorShouldpass = (g_iTotalVisibleMirrors > 0 && r_mirrors->value); + g_bScreenShouldpass = (g_iTotalVisibleScreens > 0 && r_screens->value); + g_bPortalShouldpass = (g_iTotalVisiblePortals > 0 && r_portals->value); + g_bSkyShouldpass = (gHUD.m_iSkyMode ? true : false); + } + m_RenderRefCount = 0;//reset debug counter + g_bMirrorPass = false; + g_bScreenPass = false; + g_bPortalPass = false; + g_bSkyPass = false; + g_bFinalPass = false; + } +} +//========================== +// V_CalcCameraRefdef +//========================== +void V_CalcCameraRefdef (struct ref_params_s *pparams) +{ + if(pparams->intermission) return;//disable in intermission mode + + if (gHUD.viewFlags & CAMERA_ON ) + { + //get viewentity and monster eyeposition + cl_entity_t *viewentity = UTIL_GetClientEntityWithServerIndex( gHUD.viewEntityIndex ); + if (viewentity) + { + studiohdr_t *viewmonster = (studiohdr_t *)IEngineStudio.Mod_Extradata (viewentity->model); + VectorCopy ( pparams->viewangles, v_angles );//copy angles to transform + VectorCopy ( pparams->vieworg, v_origin ); //copy origin to transform + + if(gHUD.viewFlags & MONSTER_VIEW && viewmonster)//calc monster view + { + VectorAdd(viewentity->origin, viewmonster->eyeposition, v_origin); + } + else + { + VectorCopy(viewentity->origin, v_origin); + } + VectorCopy(viewentity->angles, v_angles); + + if (gHUD.viewFlags & INVERSE_X)//inverse X coordinate + v_angles[0] = -v_angles[0]; + pparams->crosshairangle[PITCH] = 100; //hide crosshair + + //refresh position + VectorCopy ( v_angles, pparams->viewangles ) + VectorCopy ( v_origin, pparams->vieworg ); + } + } + else pparams->crosshairangle[PITCH] = 0;//show crosshair again +} + +cl_entity_t *V_FindIntermisionSpot( struct ref_params_s *pparams ) +{ + cl_entity_t *ent; + int spotindex[9];//max number of intermission spot + int k = 0, j = 0; + + //found intermission point + for( int i = 0; i < pparams->max_entities; i++ ) + { + ent = UTIL_GetClientEntityWithServerIndex( i ); + if(ent && ent->curstate.eflags & FL_INTERMISSION) + { + spotindex[j] = ent->index;//save entindex + j++; + if(j > 8) break;//spotlist is full + } + } + + //ok, we have list of intermission spots + if( j ) + { + if(j > 1) k = gEngfuncs.pfnRandomLong(0, j-1); + ent = UTIL_GetClientEntityWithServerIndex( spotindex[k] ); + } + else ent = gEngfuncs.GetLocalPlayer();//just get view from player + + return ent; +} + +//========================== +// V_CalcIntermisionRefdef +//========================== +void V_CalcIntermisionRefdef( struct ref_params_s *pparams ) +{ + if(!pparams->intermission) return;//intermission mode + cl_entity_t *view; + + float old; + + if(!spot)spot = V_FindIntermisionSpot( pparams ); + view = gEngfuncs.GetViewModel(); + + VectorCopy ( spot->origin, pparams->vieworg ); + VectorCopy ( spot->angles, pparams->viewangles ); + + view->model = NULL; + + // allways idle in intermission + old = v_idlescale; + v_idlescale = 1; + V_AddIdle ( pparams ); + + v_idlescale = old; + v_cl_angles = pparams->cl_viewangles; + v_origin = pparams->vieworg; + v_angles = pparams->viewangles; +} + +//========================== +// V_PrintDebugInfo +//========================== +void V_PrintDebugInfo (struct ref_params_s *pparams) //for future extensions +{ + if (!r_debug->value)return; //show OpenGL renderer debug info + Msg("Xash Renderer Info: "); + if(m_RenderRefCount > 1)Msg("Total %d passes\n", m_RenderRefCount); + else Msg("Use normal view, make one pass\n"); + if(g_iTotalMirrors) Msg("Visible mirrors: %d from %d\n",g_iTotalVisibleMirrors, g_iTotalMirrors); + if(g_iTotalScreens) Msg("Visible screens: %d from %d\n",g_iTotalVisibleScreens, g_iTotalScreens); + if(g_iTotalPortals) Msg("Visible portals: %d from %d\n",g_iTotalVisiblePortals, g_iTotalPortals); +} + +//========================== +// V_CalcFinalPass +//========================== +void V_CalcFinalPass ( struct ref_params_s *pparams ) +{ + g_FirstFrame = true;//enable calc next passes + g_StudioRenderer.g_bLocalPlayerDrawn = true;//hide thirdperson + + V_ResetViewportRefdef(pparams);//reset view port as default + m_RenderRefCount++;//increase counter +} + +//========================== +// V_CalcThirdPersonRefdef +//========================== +void V_CalcThirdPersonRefdef( struct ref_params_s * pparams ) +{ + //passed only in third person + if(gHUD.m_iCameraMode == 0 || pparams->intermission) return; + + // get old values + VectorCopy ( pparams->cl_viewangles, v_cl_angles ); + VectorCopy ( pparams->viewangles, v_angles ); + VectorCopy ( pparams->vieworg, v_origin ); + + V_GetChasePos(gEngfuncs.GetLocalPlayer(), v_cl_angles, v_origin, v_angles ); + // write back new values into pparams + VectorCopy ( v_cl_angles, pparams->cl_viewangles ); + VectorCopy ( v_angles, pparams->viewangles ) + VectorCopy ( v_origin, pparams->vieworg ); +} + +//========================== +// V_CalcSendOrigin +//========================== +void V_CalcSendOrigin ( struct ref_params_s *pparams ) +{ + // never let view origin sit exactly on a node line, because a water plane can + // dissapear when viewed with the eye exactly on it. + // FIXME, we send origin at 1/128 now, change this? + // the server protocol only specifies to 1/16 pixel, so add 1/32 in each axis + + pparams->vieworg[0] += 1.0/32; + pparams->vieworg[1] += 1.0/32; + pparams->vieworg[2] += 1.0/32; +} + +//========================== +// V_CalcWaterLevel +//========================== +float V_CalcWaterLevel ( struct ref_params_s *pparams ) +{ + cl_entity_t *pwater; + float waterOffset = 0; + + if ( pparams->waterlevel >= 2 ) + { + int i, contents, waterDist, waterEntity; + vec3_t point; + waterDist = cl_waterdist->value; + + if ( pparams->hardware ) + { + waterEntity = gEngfuncs.PM_WaterEntity( pparams->simorg ); + if ( waterEntity >= 0 && waterEntity < pparams->max_entities ) + { + pwater = gEngfuncs.GetEntityByIndex( waterEntity ); + if (pwater && (pwater->model != NULL)) waterDist += ( pwater->curstate.scale * 16 ); + } + } + else waterEntity = 0; + VectorCopy( pparams->vieworg, point ); + + // Eyes are above water, make sure we're above the waves + if ( pparams->waterlevel == 2 ) + { + point[2] -= waterDist; + for ( i = 0; i < waterDist; i++ ) + { + contents = gEngfuncs.PM_PointContents( point, NULL ); + if ( contents > CONTENTS_WATER )break; + point[2] += 1; + } + waterOffset = (point[2] + waterDist) - pparams->vieworg[2]; + } + else + { + // eyes are under water. Make sure we're far enough under + point[2] += waterDist; + for ( i = 0; i < waterDist; i++ ) + { + contents = gEngfuncs.PM_PointContents( point, NULL ); + if ( contents <= CONTENTS_WATER )break; + point[2] -= 1; + } + waterOffset = (point[2] - waterDist) - pparams->vieworg[2]; + } + } + pparams->vieworg[2] += waterOffset; + return waterOffset; +} + +//========================== +// V_CalcScrOffset +//========================== +void V_CalcScrOffset( struct ref_params_s *pparams ) +{ + // don't allow cheats in multiplayer + if ( pparams->maxclients <= 1 ) + { + for (int i=0 ; i<3; i++ ) + { + pparams->vieworg[i] += scr_ofsx->value*pparams->forward[i] + scr_ofsy->value*pparams->right[i] + scr_ofsz->value*pparams->up[i]; + } + } +} + +//========================== +// V_CalcSmoothAngles +//========================== +void V_CalcSmoothAngles( struct ref_params_s *pparams ) +{ + cl_entity_t *view; + + // view is the weapon model (only visible from inside body ) + view = gEngfuncs.GetViewModel(); + + if (cl_vsmoothing && cl_vsmoothing->value && ( pparams->smoothing && ( pparams->maxclients > 1 ))) + { + int foundidx; + int i; + float t; + + if ( cl_vsmoothing->value < 0.0 ) gEngfuncs.Cvar_SetValue( "cl_vsmoothing", 0.0 ); + t = pparams->time - cl_vsmoothing->value; + for ( i = 1; i < ORIGIN_MASK; i++ ) + { + foundidx = ViewInterp.CurrentOrigin - 1 - i; + if ( ViewInterp.OriginTime[ foundidx & ORIGIN_MASK ] <= t ) break; + } + + if ( i < ORIGIN_MASK && ViewInterp.OriginTime[ foundidx & ORIGIN_MASK ] != 0.0 ) + { + // Interpolate + vec3_t delta; + double frac; + double dt; + vec3_t neworg; + + dt = ViewInterp.OriginTime[ (foundidx + 1) & ORIGIN_MASK ] - ViewInterp.OriginTime[ foundidx & ORIGIN_MASK ]; + if ( dt > 0.0 ) + { + frac = ( t - ViewInterp.OriginTime[ foundidx & ORIGIN_MASK] ) / dt; + frac = min( 1.0, frac ); + VectorSubtract( ViewInterp.Origins[ ( foundidx + 1 ) & ORIGIN_MASK ], ViewInterp.Origins[ foundidx & ORIGIN_MASK ], delta ); + VectorMA( ViewInterp.Origins[ foundidx & ORIGIN_MASK ], frac, delta, neworg ); + + // Dont interpolate large changes + if ( Length( delta ) < 64 ) + { + VectorSubtract( neworg, pparams->simorg, delta ); + VectorAdd( pparams->simorg, delta, pparams->simorg ); + VectorAdd( pparams->vieworg, delta, pparams->vieworg ); + VectorAdd( view->origin, delta, view->origin ); + + } + } + } + } +} + +//========================== +// V_CalcFirstPersonRefdef +//========================== +void V_CalcFirstPersonRefdef( struct ref_params_s *pparams ) +{ + //don't pass in thirdperson or intermission + if(gHUD.m_iCameraMode || pparams->intermission) return; + + int i; + vec3_t angles; + float bob, waterOffset; + static float lasttime; + cl_entity_t *view = gEngfuncs.GetViewModel(); + + if(g_bFinalPass)V_DriftPitch( pparams );//calculate this only in final pass + bob = V_CalcBob( pparams ); + + // refresh position + VectorCopy ( pparams->simorg, pparams->vieworg ); + pparams->vieworg[2] += ( bob ); + VectorAdd( pparams->vieworg, pparams->viewheight, pparams->vieworg ); + VectorCopy ( pparams->cl_viewangles, pparams->viewangles ); + + gEngfuncs.V_CalcShake(); + gEngfuncs.V_ApplyShake( pparams->vieworg, pparams->viewangles, 1.0 ); + + V_CalcSendOrigin( pparams ); + waterOffset = V_CalcWaterLevel( pparams ); + V_CalcViewRoll( pparams ); + V_AddIdle( pparams ); + + // offsets + VectorCopy( pparams->cl_viewangles, angles ); + AngleVectors ( angles, pparams->forward, pparams->right, pparams->up ); + V_CalcScrOffset( pparams ); + + VectorCopy ( pparams->cl_viewangles, view->angles ); + V_CalcGunAngle ( pparams ); + + // Use predicted origin as view origin. + VectorCopy ( pparams->simorg, view->origin ); + view->origin[2] += ( waterOffset ); + VectorAdd( view->origin, pparams->viewheight, view->origin ); + + // Let the viewmodel shake at about 10% of the amplitude + gEngfuncs.V_ApplyShake( view->origin, view->angles, 0.9 ); + + for ( i = 0; i < 3; i++)view->origin[ i ] += bob * 0.4 * pparams->forward[ i ]; + view->origin[2] += bob; + + view->angles[YAW] -= bob * 0.5; + view->angles[ROLL] -= bob * 1; + view->angles[PITCH] -= bob * 0.3; + view->origin[2] -= 1; + + if(!g_bFinalPass)VectorInverse(pparams->punchangle); //make inverse for mirror + + VectorAdd ( pparams->viewangles, pparams->punchangle, pparams->viewangles ); + VectorAdd ( pparams->viewangles, (float *)&ev_punchangle, pparams->viewangles); + V_DropPunchAngle ( pparams->frametime, (float *)&ev_punchangle ); + + static float oldz = 0; + + if ( !pparams->smoothing && pparams->onground && pparams->simorg[2] - oldz > 0) + { + float steptime; + + steptime = pparams->time - lasttime; + if (steptime < 0)steptime = 0; + + oldz += steptime * 150; + if (oldz > pparams->simorg[2]) + oldz = pparams->simorg[2]; + if (pparams->simorg[2] - oldz > 18) + oldz = pparams->simorg[2]- 18; + pparams->vieworg[2] += oldz - pparams->simorg[2]; + view->origin[2] += oldz - pparams->simorg[2]; + } + else oldz = pparams->simorg[2]; + + static float lastorg[3]; + vec3_t delta; + + VectorSubtract( pparams->simorg, lastorg, delta ); + + if ( Length( delta ) != 0.0 ) + { + VectorCopy( pparams->simorg, ViewInterp.Origins[ ViewInterp.CurrentOrigin & ORIGIN_MASK ] ); + ViewInterp.OriginTime[ ViewInterp.CurrentOrigin & ORIGIN_MASK ] = pparams->time; + ViewInterp.CurrentOrigin++; + + VectorCopy( pparams->simorg, lastorg ); + } + V_CalcSmoothAngles( pparams );//smooth angles in multiplayer + + lasttime = pparams->time; + v_origin = pparams->vieworg; +} + +//========================== +// V_CalcMainRefdef +//========================== +void V_CalcMainRefdef (struct ref_params_s *pparams) +{ + if(g_FirstFrame)g_bFinalPass = true; + V_CalcFirstPersonRefdef( pparams ); + V_CalcThirdPersonRefdef( pparams ); + V_CalcIntermisionRefdef( pparams ); + V_CalcCameraRefdef( pparams ); + VectorCopy(pparams->vieworg, g_vecBaseOrigin); + VectorCopy(pparams->viewangles, g_vecBaseAngles); +} + +//========================== +// V_CalcSkyRefdef +//========================== +bool V_CalcSkyRefdef(struct ref_params_s *pparams) +{ + if (g_bSkyShouldpass) + { + g_bSkyShouldpass = false; + V_CalcMainRefdef( pparams );//refresh position + VectorCopy(gHUD.m_vecSkyPos, pparams->vieworg); + V_ResetViewportRefdef(pparams); + pparams->nextView = 1; + g_bSkyPass = true; + m_RenderRefCount++; + return true;//new pass + } + if(g_bSkyPass) + { + pparams->nextView = 0;//reset refcounter + g_bSkyPass = false; + return false; + } + return false;//no drawing +} + +//========================== +// V_CalcMirrorsRefdef +//========================== +bool V_CalcMirrorsRefdef (struct ref_params_s *pparams) +{ + if (g_bMirrorShouldpass) + { + if (pparams->nextView == 0) + { + g_bMirrorPass = true; + //This is first pass rendering (setup mirror viewport and nextView's) + V_SetViewportRefdef(pparams); + glEnable(GL_CLIP_PLANE0);//enable clip plane once in first mirror! + g_StudioRenderer.g_bLocalPlayerDrawn = false; + m_pCurrentMirror = NULL; + V_CalcMainRefdef( pparams ); + R_SetupNewMirror(pparams); + R_SetupMirrorRenderPass(pparams); + pparams->nextView = g_iTotalVisibleMirrors; + + m_RenderRefCount++; + return true;//end of pass + } + else if (pparams->nextView == 1) + { + R_NewMirrorRenderPass(); //capture view to texture + glDisable(GL_CLIP_PLANE0); + m_pCurrentMirror = NULL; + + //restore for final pass + glViewport(0,0,ScreenWidth,ScreenHeight); + pparams->nextView = 0; + + g_bMirrorPass = false; + g_bMirrorShouldpass = false; + return false; //false nextview + } + else + { + R_NewMirrorRenderPass(); //capture view to texture + R_SetupNewMirror(pparams); + R_SetupMirrorRenderPass(pparams); //setup origin and angles + pparams->nextView--; + g_StudioRenderer.g_bLocalPlayerDrawn = false; + + m_RenderRefCount++; + return true;//end of pass + } + } + return false;//no drawing +} + +//========================== +// V_CalcScreensRefdef +//========================== +bool V_CalcScreensRefdef (struct ref_params_s *pparams) +{ + if (g_bScreenShouldpass) + { + if (pparams->nextView == 0) + { + //This is first pass rendering (setup screen viewport and nextView's) + g_bScreenPass = true; + V_SetViewportRefdef(pparams); + + g_StudioRenderer.g_bLocalPlayerDrawn = false; + m_pCurrentScreen = NULL; + R_SetupNewScreen(pparams); + R_SetupScreenRenderPass(pparams); + pparams->nextView = g_iTotalVisibleScreens; + m_RenderRefCount++; + return true;//end of pass + } + else if (pparams->nextView == 1) + { + R_NewScreenRenderPass(); //capture view to texture + m_pCurrentScreen = NULL; + + //restore for final pass + V_ResetViewportRefdef(pparams); + glViewport(0,0,ScreenWidth,ScreenHeight); + pparams->nextView = 0; + g_bScreenShouldpass = false; + return false; //false nextview + } + else + { + R_NewScreenRenderPass(); //capture view to texture + R_SetupNewScreen(pparams); + R_SetupScreenRenderPass(pparams); //setup origin and angles + pparams->nextView--; + g_StudioRenderer.g_bLocalPlayerDrawn = false; + + m_RenderRefCount++; + return true;//end of pass + } + } + return false; //false nextview +} + +//========================== +// V_CalcPortalsRefdef +//========================== +bool V_CalcPortalsRefdef (struct ref_params_s *pparams) +{ + if (g_bPortalShouldpass) + { + if (pparams->nextView == 0) + { + //This is first pass rendering (setup mirror viewport and nextView's) + g_bPortalPass = true; + V_SetViewportRefdef(pparams); + + g_StudioRenderer.g_bLocalPlayerDrawn = false; + m_pCurrentPortal = NULL; + V_CalcMainRefdef( pparams ); + R_SetupNewPortal(pparams); + R_SetupPortalRenderPass(pparams); + pparams->nextView = g_iTotalVisiblePortals; + m_RenderRefCount++; + return true;//end of pass + } + else if (pparams->nextView == 1) + { + R_NewPortalRenderPass(); //capture view to texture + m_pCurrentPortal = NULL; + + //restore for final pass + V_ResetViewportRefdef(pparams); + glViewport(0,0,ScreenWidth,ScreenHeight); + pparams->nextView = 0; + g_bPortalShouldpass = false; + return false; //false nextview + } + else + { + R_NewPortalRenderPass(); //capture view to texture + R_SetupNewPortal(pparams); + R_SetupPortalRenderPass(pparams); //setup origin and angles + pparams->nextView--; + g_StudioRenderer.g_bLocalPlayerDrawn = false; + + m_RenderRefCount++; + return true;//end of pass + } + } + return false;//no drawing +} + +//========================== +// V_CalcHardwareRefdef +//========================== +void V_CalcHardwareRefdef( struct ref_params_s *pparams ) +{ + //check for renderer + if(!pparams->hardware) return; + + V_CalcNextView( pparams );//calculate passes + + if(V_CalcSkyRefdef(pparams)) return; + if(V_CalcScreensRefdef(pparams)) return; + if(V_CalcPortalsRefdef(pparams)) return; + if(V_CalcMirrorsRefdef(pparams)) return; + + V_CalcGlobalFog( pparams ); + V_CalcFinalPass ( pparams ); + V_CalcMainRefdef( pparams ); + + V_PrintDebugInfo( pparams ); +} + +//========================== +// V_CalcSoftwareRefdef +//========================== +void V_CalcSoftwareRefdef( struct ref_params_s *pparams ) +{ + //check for renderer + if(pparams->hardware) return; + + V_CalcNextView( pparams ); + + V_CalcFinalPass ( pparams ); + V_CalcMainRefdef( pparams ); + + V_PrintDebugInfo( pparams ); +} + +//========================== +// V_CalcRefdef +//========================== +void DLLEXPORT V_CalcRefdef( struct ref_params_s *pparams ) +{ + pause = pparams->paused; + if ( pause ) return; + + V_CalcHardwareRefdef( pparams ); + V_CalcSoftwareRefdef( pparams ); +} \ No newline at end of file diff --git a/client/r_view.h b/client/r_view.h new file mode 100644 index 00000000..942dfebb --- /dev/null +++ b/client/r_view.h @@ -0,0 +1,45 @@ +//========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============ +// +// Purpose: +// +// $NoKeywords: $ +//============================================================================= + +#ifndef VIEW_H +#define VIEW_H + +void V_StartPitchDrift( void ); +void V_StopPitchDrift( void ); + +//camera flags +#define CAMERA_ON 1 +#define DRAW_HUD 2 +#define INVERSE_X 4 +#define MONSTER_VIEW 8 + +typedef struct pitchdrift_s +{ + float pitchvel; + int nodrift; + float driftmove; + double laststop; +}pitchdrift_t; +static pitchdrift_t pd; + + +#define ORIGIN_BACKUP 64 +#define ORIGIN_MASK ( ORIGIN_BACKUP - 1 ) + +typedef struct +{ + float Origins[ ORIGIN_BACKUP ][3]; + float OriginTime[ ORIGIN_BACKUP ]; + + float Angles[ ORIGIN_BACKUP ][3]; + float AngleTime[ ORIGIN_BACKUP ]; + + int CurrentOrigin; + int CurrentAngle; +}viewinterp_t; + +#endif // VIEW_H \ No newline at end of file diff --git a/client/r_weather.cpp b/client/r_weather.cpp new file mode 100644 index 00000000..76690f26 --- /dev/null +++ b/client/r_weather.cpp @@ -0,0 +1,680 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// rain.cpp - TriAPI weather effects +// based on original code from BUzer +//======================================================================= + +#include "hud.h" +#include "r_main.h" +#include "r_util.h" +#include "const.h" +#include "entity_types.h" +#include "cdll_int.h" +#include "pm_defs.h" +#include "event_api.h" +#include "triangleapi.h" +#include "r_weather.h" + +void WaterLandingEffect(cl_drip *drip); +void ParseRainFile( void ); + +rain_properties Rain; + +cl_drip FirstChainDrip; +cl_rainfx FirstChainFX; + +double rain_curtime; // current time +double rain_oldtime; // last time we have updated drips +double rain_timedelta; // difference between old time and current time +double rain_nextspawntime; // when the next drip should be spawned + +int dripcounter = 0; +int fxcounter = 0; + + +/* +================================= +ProcessRain + +Must think every frame. +================================= +*/ +void ProcessRain( void ) +{ + rain_oldtime = rain_curtime; // save old time + rain_curtime = gEngfuncs.GetClientTime(); + rain_timedelta = rain_curtime - rain_oldtime; + + // first frame + if (rain_oldtime == 0) + { + // fix first frame bug with nextspawntime + rain_nextspawntime = gEngfuncs.GetClientTime(); + ParseRainFile(); + return; + } + + if (Rain.dripsPerSecond == 0 && FirstChainDrip.p_Next == NULL) + { + // keep nextspawntime correct + rain_nextspawntime = rain_curtime; + return; + } + + if (rain_timedelta == 0) + return; // not in pause + + double timeBetweenDrips = 1 / (double)Rain.dripsPerSecond; + + cl_drip* curDrip = FirstChainDrip.p_Next; + cl_drip* nextDrip = NULL; + + cl_entity_t *player = gEngfuncs.GetLocalPlayer(); + + // save debug info + float debug_lifetime = 0; + int debug_howmany = 0; + int debug_attempted = 0; + int debug_dropped = 0; + + while (curDrip != NULL) // go through list + { + nextDrip = curDrip->p_Next; // save pointer to next drip + + if (Rain.weatherMode == 0) + curDrip->origin.z -= rain_timedelta * DRIPSPEED; // rain + else + curDrip->origin.z -= rain_timedelta * SNOWSPEED; // snow + + curDrip->origin.x += rain_timedelta * curDrip->xDelta; + curDrip->origin.y += rain_timedelta * curDrip->yDelta; + + // remove drip if its origin lower than minHeight + if (curDrip->origin.z < curDrip->minHeight) + { + if (curDrip->landInWater/* && Rain.weatherMode == 0*/) + WaterLandingEffect(curDrip); // create water rings + + if (r_debug->value > 1) + { + debug_lifetime += (rain_curtime - curDrip->birthTime); + debug_howmany++; + } + + curDrip->p_Prev->p_Next = curDrip->p_Next; // link chain + if (nextDrip != NULL) + nextDrip->p_Prev = curDrip->p_Prev; + delete curDrip; + + dripcounter--; + } + + curDrip = nextDrip; // restore pointer, so we can continue moving through chain + } + + int maxDelta; // maximum height randomize distance + float falltime; + if (Rain.weatherMode == 0) + { + maxDelta = DRIPSPEED * rain_timedelta; // for rain + falltime = (Rain.globalHeight + 4096) / DRIPSPEED; + } + else + { + maxDelta = SNOWSPEED * rain_timedelta; // for snow + falltime = (Rain.globalHeight + 4096) / SNOWSPEED; + } + + while (rain_nextspawntime < rain_curtime) + { + rain_nextspawntime += timeBetweenDrips; + if (r_debug->value > 1) debug_attempted++; + + if (dripcounter < MAXDRIPS) // check for overflow + { + float deathHeight; + vec3_t vecStart, vecEnd; + + vecStart[0] = gEngfuncs.pfnRandomFloat(player->origin.x - Rain.distFromPlayer, player->origin.x + Rain.distFromPlayer); + vecStart[1] = gEngfuncs.pfnRandomFloat(player->origin.y - Rain.distFromPlayer, player->origin.y + Rain.distFromPlayer); + vecStart[2] = Rain.globalHeight; + + float xDelta = Rain.windX + gEngfuncs.pfnRandomFloat(Rain.randX * -1, Rain.randX); + float yDelta = Rain.windY + gEngfuncs.pfnRandomFloat(Rain.randY * -1, Rain.randY); + + // find a point at bottom of map + vecEnd[0] = falltime * xDelta; + vecEnd[1] = falltime * yDelta; + vecEnd[2] = -4096; + + pmtrace_t pmtrace; + gEngfuncs.pEventAPI->EV_SetTraceHull( 2 ); + gEngfuncs.pEventAPI->EV_PlayerTrace( vecStart, vecStart + vecEnd, PM_STUDIO_IGNORE, -1, &pmtrace ); + + if (pmtrace.startsolid || pmtrace.allsolid) + { + if (r_debug->value > 1) debug_dropped++; + continue; // drip cannot be placed + } + + // falling to water? + int contents = gEngfuncs.PM_PointContents( pmtrace.endpos, NULL ); + if (contents == CONTENTS_WATER) + { + int waterEntity = gEngfuncs.PM_WaterEntity( pmtrace.endpos ); + if ( waterEntity > 0 ) + { + cl_entity_t *pwater = UTIL_GetClientEntityWithServerIndex( waterEntity ); + if ( pwater && ( pwater->model != NULL ) ) + { + deathHeight = pwater->curstate.maxs[2]; + } + else + { + Msg("Rain error: can't get water entity\n"); + continue; + } + } + else + { + Msg("Rain error: water is not func_water entity\n"); + continue; + } + } + else + { + deathHeight = pmtrace.endpos[2]; + } + + // just in case.. + if (deathHeight > vecStart[2]) + { + Msg("Rain error: can't create drip in water\n"); + continue; + } + + + cl_drip *newClDrip = new cl_drip; + if (!newClDrip) + { + Rain.dripsPerSecond = 0; // disable rain + Msg( "Rain error: failed to allocate object!\n"); + return; + } + + vecStart[2] -= gEngfuncs.pfnRandomFloat(0, maxDelta); // randomize a bit + + newClDrip->alpha = gEngfuncs.pfnRandomFloat(0.12, 0.2); + VectorCopy(vecStart, newClDrip->origin); + + newClDrip->xDelta = xDelta; + newClDrip->yDelta = yDelta; + + newClDrip->birthTime = rain_curtime; // store time when it was spawned + newClDrip->minHeight = deathHeight; + + if (contents == CONTENTS_WATER) + newClDrip->landInWater = 1; + else + newClDrip->landInWater = 0; + + // add to first place in chain + newClDrip->p_Next = FirstChainDrip.p_Next; + newClDrip->p_Prev = &FirstChainDrip; + if (newClDrip->p_Next != NULL) + newClDrip->p_Next->p_Prev = newClDrip; + FirstChainDrip.p_Next = newClDrip; + + dripcounter++; + } + else + { + Msg( "Rain error: Drip limit overflow!\n" ); + return; + } + } + + if (r_debug->value > 1) // print debug info + { + Msg( "Rain info: Drips exist: %i\n", dripcounter ); + Msg( "Rain info: FX's exist: %i\n", fxcounter ); + Msg( "Rain info: Attempted/Dropped: %i, %i\n", debug_attempted, debug_dropped); + if (debug_howmany) + { + float ave = debug_lifetime / (float)debug_howmany; + Msg( "Rain info: Average drip life time: %f\n", ave); + } + else + Msg( "Rain info: Average drip life time: --\n"); + } + return; +} + +/* +================================= +WaterLandingEffect +================================= +*/ +void WaterLandingEffect(cl_drip *drip) +{ + if (fxcounter >= MAXFX) + { + Msg( "Rain error: FX limit overflow!\n" ); + return; + } + + cl_rainfx *newFX = new cl_rainfx; + if (!newFX) + { + Msg( "Rain error: failed to allocate FX object!\n"); + return; + } + + newFX->alpha = gEngfuncs.pfnRandomFloat(0.6, 0.9); + VectorCopy(drip->origin, newFX->origin); + newFX->origin[2] = drip->minHeight; // correct position + + newFX->birthTime = gEngfuncs.GetClientTime(); + newFX->life = gEngfuncs.pfnRandomFloat(0.7, 1); + + // add to first place in chain + newFX->p_Next = FirstChainFX.p_Next; + newFX->p_Prev = &FirstChainFX; + if (newFX->p_Next != NULL) + newFX->p_Next->p_Prev = newFX; + FirstChainFX.p_Next = newFX; + + fxcounter++; +} + +/* +================================= +ProcessFXObjects + +Remove all fx objects with out time to live +Call every frame before ProcessRain +================================= +*/ +void ProcessFXObjects( void ) +{ + float curtime = gEngfuncs.GetClientTime(); + + cl_rainfx* curFX = FirstChainFX.p_Next; + cl_rainfx* nextFX = NULL; + + while (curFX != NULL) // go through FX objects list + { + nextFX = curFX->p_Next; // save pointer to next + + // delete current? + if ((curFX->birthTime + curFX->life) < curtime) + { + curFX->p_Prev->p_Next = curFX->p_Next; // link chain + if (nextFX != NULL) + nextFX->p_Prev = curFX->p_Prev; + delete curFX; + fxcounter--; + } + curFX = nextFX; // restore pointer + } +} + +/* +================================= +ResetRain +clear memory, delete all objects +================================= +*/ +void ResetRain( void ) +{ +// delete all drips + cl_drip* delDrip = FirstChainDrip.p_Next; + FirstChainDrip.p_Next = NULL; + + while (delDrip != NULL) + { + cl_drip* nextDrip = delDrip->p_Next; // save pointer to next drip in chain + delete delDrip; + delDrip = nextDrip; // restore pointer + dripcounter--; + } + +// delete all FX objects + cl_rainfx* delFX = FirstChainFX.p_Next; + FirstChainFX.p_Next = NULL; + + while (delFX != NULL) + { + cl_rainfx* nextFX = delFX->p_Next; + delete delFX; + delFX = nextFX; + fxcounter--; + } + + InitRain(); + return; +} + +/* +================================= +InitRain +initialze system +================================= +*/ +void InitRain( void ) +{ + Rain.dripsPerSecond = 0; + Rain.distFromPlayer = 0; + Rain.windX = 0; + Rain.windY = 0; + Rain.randX = 0; + Rain.randY = 0; + Rain.weatherMode = 0; + Rain.globalHeight = 0; + + FirstChainDrip.birthTime = 0; + FirstChainDrip.minHeight = 0; + FirstChainDrip.origin[0]=0; + FirstChainDrip.origin[1]=0; + FirstChainDrip.origin[2]=0; + FirstChainDrip.alpha = 0; + FirstChainDrip.xDelta = 0; + FirstChainDrip.yDelta = 0; + FirstChainDrip.landInWater = 0; + FirstChainDrip.p_Next = NULL; + FirstChainDrip.p_Prev = NULL; + + FirstChainFX.alpha = 0; + FirstChainFX.birthTime = 0; + FirstChainFX.life = 0; + FirstChainFX.origin[0] = 0; + FirstChainFX.origin[1] = 0; + FirstChainFX.origin[2] = 0; + FirstChainFX.p_Next = NULL; + FirstChainFX.p_Prev = NULL; + + rain_oldtime = 0; + rain_curtime = 0; + rain_nextspawntime = 0; + + return; +} + +/* +=========================== +ParseRainFile + +List of settings: + drips - max raindrips\snowflakes + distance - radius of rain\snow + windx - wind shift X + windy - wind shift Y + randx - random shift X + randy - random shift Y + mode - rain = 0\snow =1 + height - max height to create raindrips\snowflakes +=========================== +*/ +void ParseRainFile( void ) +{ + if (Rain.distFromPlayer != 0 || Rain.dripsPerSecond != 0 || Rain.globalHeight != 0) + return; + + char mapname[64]; + char token[64]; + char *pfile; + + strcpy( mapname, gEngfuncs.pfnGetLevelName() ); + if (strlen(mapname) == 0) + { + Msg( "Rain error: unable to read map name\n"); + return; + } + + mapname[strlen(mapname)-4] = 0; + sprintf(mapname, "%s.pcs", mapname); + + pfile = (char *)gEngfuncs.COM_LoadFile( mapname, 5, NULL); + if (!pfile) + { + if (r_debug->value > 1) + Msg("Rain: couldn't open rain settings file %s\n", mapname); + return; + } + + while (true) + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + if (!pfile) + break; + + if (!stricmp(token, "drips")) // dripsPerSecond + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.dripsPerSecond = atoi(token); + } + else if (!stricmp(token, "distance")) // distFromPlayer + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.distFromPlayer = atof(token); + } + else if (!stricmp(token, "windx")) // windX + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.windX = atof(token); + } + else if (!stricmp(token, "windy")) // windY + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.windY = atof(token); + } + else if (!stricmp(token, "randx")) // randX + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.randX = atof(token); + } + else if (!stricmp(token, "randy")) // randY + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.randY = atof(token); + } + else if (!stricmp(token, "mode")) // weatherMode + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.weatherMode = atoi(token); + } + else if (!stricmp(token, "height")) // globalHeight + { + pfile = gEngfuncs.COM_ParseFile(pfile, token); + Rain.globalHeight = atof(token); + } + else + Msg("Rain error: unknown token %s in file %s\n", token, mapname); + } + + gEngfuncs.COM_FreeFile( pfile ); +} + +//----------------------------------------------------- + +void SetPoint( float x, float y, float z, float (*matrix)[4]) +{ + vec3_t point, result; + point[0] = x; + point[1] = y; + point[2] = z; + + VectorTransform(point, matrix, result); + gEngfuncs.pTriAPI->Vertex3f(result[0], result[1], result[2]); +} + +/* +================================= +DrawRain + +draw raindrips and snowflakes +================================= +*/ +void DrawRain( void ) +{ + if (FirstChainDrip.p_Next == NULL) + return; // no drips to draw + + HSPRITE hsprTexture; + const model_s *pTexture; + float visibleHeight = Rain.globalHeight - SNOWFADEDIST; + + if (Rain.weatherMode == 0) + hsprTexture = LoadSprite( "sprites/raindrop.spr" ); // load rain sprite + else hsprTexture = LoadSprite( "sprites/snowflake.spr" ); // load snow sprite + + if(!hsprTexture) return; + // usual triapi stuff + pTexture = gEngfuncs.GetSpritePointer( hsprTexture ); + gEngfuncs.pTriAPI->SpriteTexture( (struct model_s *)pTexture, 0 ); + gEngfuncs.pTriAPI->RenderMode( kRenderTransAdd ); + gEngfuncs.pTriAPI->CullFace( TRI_NONE ); + + // go through drips list + cl_drip* Drip = FirstChainDrip.p_Next; + cl_entity_t *player = gEngfuncs.GetLocalPlayer(); + + if ( Rain.weatherMode == 0 ) // draw rain + { + while (Drip != NULL) + { + cl_drip* nextdDrip = Drip->p_Next; + + Vector2D toPlayer; + toPlayer.x = player->origin[0] - Drip->origin[0]; + toPlayer.y = player->origin[1] - Drip->origin[1]; + toPlayer = toPlayer.Normalize(); + + toPlayer.x *= DRIP_SPRITE_HALFWIDTH; + toPlayer.y *= DRIP_SPRITE_HALFWIDTH; + + float shiftX = (Drip->xDelta / DRIPSPEED) * DRIP_SPRITE_HALFHEIGHT; + float shiftY = (Drip->yDelta / DRIPSPEED) * DRIP_SPRITE_HALFHEIGHT; + + gEngfuncs.pTriAPI->Color4f( 1.0, 1.0, 1.0, Drip->alpha ); + gEngfuncs.pTriAPI->Begin( TRI_TRIANGLES ); + + gEngfuncs.pTriAPI->TexCoord2f( 0, 0 ); + gEngfuncs.pTriAPI->Vertex3f( Drip->origin[0]-toPlayer.y - shiftX, Drip->origin[1]+toPlayer.x - shiftY,Drip->origin[2] + DRIP_SPRITE_HALFHEIGHT ); + + gEngfuncs.pTriAPI->TexCoord2f( 0.5, 1 ); + gEngfuncs.pTriAPI->Vertex3f( Drip->origin[0] + shiftX, Drip->origin[1] + shiftY, Drip->origin[2]-DRIP_SPRITE_HALFHEIGHT ); + + gEngfuncs.pTriAPI->TexCoord2f( 1, 0 ); + gEngfuncs.pTriAPI->Vertex3f( Drip->origin[0]+toPlayer.y - shiftX, Drip->origin[1]-toPlayer.x - shiftY, Drip->origin[2]+DRIP_SPRITE_HALFHEIGHT); + + gEngfuncs.pTriAPI->End(); + Drip = nextdDrip; + } + } + + else // draw snow + { + vec3_t normal; + gEngfuncs.GetViewAngles((float*)normal); + + float matrix[3][4]; + AngleMatrix (normal, matrix); // calc view matrix + + while (Drip != NULL) + { + cl_drip* nextdDrip = Drip->p_Next; + + matrix[0][3] = Drip->origin[0]; // write origin to matrix + matrix[1][3] = Drip->origin[1]; + matrix[2][3] = Drip->origin[2]; + + // apply start fading effect + float alpha = (Drip->origin[2] <= visibleHeight) ? Drip->alpha : ((Rain.globalHeight - Drip->origin[2]) / (float)SNOWFADEDIST) * Drip->alpha; + + gEngfuncs.pTriAPI->Color4f( 1.0, 1.0, 1.0, alpha ); + gEngfuncs.pTriAPI->Begin( TRI_QUADS ); + + gEngfuncs.pTriAPI->TexCoord2f( 0, 0 ); + SetPoint(0, SNOW_SPRITE_HALFSIZE ,SNOW_SPRITE_HALFSIZE, matrix); + + gEngfuncs.pTriAPI->TexCoord2f( 0, 1 ); + SetPoint(0, SNOW_SPRITE_HALFSIZE ,-SNOW_SPRITE_HALFSIZE, matrix); + + gEngfuncs.pTriAPI->TexCoord2f( 1, 1 ); + SetPoint(0, -SNOW_SPRITE_HALFSIZE ,-SNOW_SPRITE_HALFSIZE, matrix); + + gEngfuncs.pTriAPI->TexCoord2f( 1, 0 ); + SetPoint(0, -SNOW_SPRITE_HALFSIZE ,SNOW_SPRITE_HALFSIZE, matrix); + + gEngfuncs.pTriAPI->End(); + Drip = nextdDrip; + } + } +} + +/* +================================= +DrawFXObjects +================================= +*/ +void DrawFXObjects( void ) +{ + if (FirstChainFX.p_Next == NULL) + return; // no objects to draw + + float curtime = gEngfuncs.GetClientTime(); + + // usual triapi stuff + HSPRITE hsprTexture; + const model_s *pTexture; + hsprTexture = LoadSprite( "sprites/waterring.spr" ); // load water ring sprite + if(!hsprTexture) return; + pTexture = gEngfuncs.GetSpritePointer( hsprTexture ); + gEngfuncs.pTriAPI->SpriteTexture( (struct model_s *)pTexture, 0 ); + gEngfuncs.pTriAPI->RenderMode( kRenderTransAdd ); + gEngfuncs.pTriAPI->CullFace( TRI_NONE ); + + // go through objects list + cl_rainfx* curFX = FirstChainFX.p_Next; + while (curFX != NULL) + { + cl_rainfx* nextFX = curFX->p_Next; + + // fadeout + float alpha = ((curFX->birthTime + curFX->life - curtime) / curFX->life) * curFX->alpha; + float size = (curtime - curFX->birthTime) * MAXRINGHALFSIZE; + + gEngfuncs.pTriAPI->Color4f( 1.0, 1.0, 1.0, alpha ); + gEngfuncs.pTriAPI->Begin( TRI_QUADS ); + + gEngfuncs.pTriAPI->TexCoord2f( 0, 0 ); + gEngfuncs.pTriAPI->Vertex3f(curFX->origin[0] - size, curFX->origin[1] - size, curFX->origin[2]); + + gEngfuncs.pTriAPI->TexCoord2f( 0, 1 ); + gEngfuncs.pTriAPI->Vertex3f(curFX->origin[0] - size, curFX->origin[1] + size, curFX->origin[2]); + + gEngfuncs.pTriAPI->TexCoord2f( 1, 1 ); + gEngfuncs.pTriAPI->Vertex3f(curFX->origin[0] + size, curFX->origin[1] + size, curFX->origin[2]); + + gEngfuncs.pTriAPI->TexCoord2f( 1, 0 ); + gEngfuncs.pTriAPI->Vertex3f(curFX->origin[0] + size, curFX->origin[1] - size, curFX->origin[2]); + + gEngfuncs.pTriAPI->End(); + curFX = nextFX; + } +} + +/* +================================= +DrawAll +================================= +*/ + +void R_DrawWeather( void ) +{ + ProcessFXObjects(); + ProcessRain(); + DrawRain(); + DrawFXObjects(); +} \ No newline at end of file diff --git a/client/r_weather.h b/client/r_weather.h new file mode 100644 index 00000000..71267add --- /dev/null +++ b/client/r_weather.h @@ -0,0 +1,66 @@ +/*** +* +* Copyright (c) 1996-2004, Shambler Team. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Shambler Team. All other use, distribution, or modification is prohibited +* without written permission from Shambler Team. +* +****/ +/* +====== rain.h ======================================================== +*/ +#ifndef __RAIN_H__ +#define __RAIN_H__ + +#define DRIPSPEED 900 // speed of raindrips (pixel per secs) +#define SNOWSPEED 200 // speed of snowflakes +#define SNOWFADEDIST 80 + +#define MAXDRIPS 2000 // max raindrops +#define MAXFX 3000 // max effects + +#define DRIP_SPRITE_HALFHEIGHT 46 +#define DRIP_SPRITE_HALFWIDTH 8 +#define SNOW_SPRITE_HALFSIZE 3 + +// radius water rings +#define MAXRINGHALFSIZE 25 + +typedef struct cl_drip +{ + float birthTime; + float minHeight; // minimal height to kill raindrop + vec3_t origin; + float alpha; + + float xDelta; // side speed + float yDelta; + int landInWater; + + cl_drip* p_Next; // next drip in chain + cl_drip* p_Prev; // previous drip in chain +} cl_drip_t; + +typedef struct cl_rainfx +{ + float birthTime; + float life; + vec3_t origin; + float alpha; + + cl_rainfx* p_Next; // next fx in chain + cl_rainfx* p_Prev; // previous fx in chain +} cl_rainfx_t; + +void ProcessRain( void ); +void ProcessFXObjects( void ); +void ResetRain( void ); +void InitRain( void ); + +#endif \ No newline at end of file diff --git a/client/readme.txt b/client/readme.txt new file mode 100644 index 00000000..23d561be --- /dev/null +++ b/client/readme.txt @@ -0,0 +1,107 @@ + client dll readme.txt +------------------------- + +This file details the structure of the half-life client dll, and +how it communicates with the half-life game engine. + + +Engine callback functions: + +Drawing functions: + HSPRITE SPR_Load( char *picname ); + Loads a sprite into memory, and returns a handle to it. + + int SPR_Frames( HSPRITE sprite ); + Returns the number of frames stored in the specified sprite. + + int SPR_Height( HSPRITE x, int frame ) + Returns the height, in pixels, of a sprite at the specified frame. + Returns 0 is the frame number or the sprite handle is invalid. + + int SPR_Width( HSPRITE x, int f ) + Returns the width, in pixels, of a sprite at the specified frame. + Returns 0 is the frame number or the sprite handle is invalid. + + int SPR_Set( HSPRITE sprite, int r, int g, int b ); + Prepares a sprite about to be drawn. RBG color values are applied to the sprite at this time. + + + void SPR_Draw( int frame, int x, int y ); + Precondition: SPR_Set has already been called for a sprite. + Draws the currently active sprite to the screen, at position (x,y), where (0,0) is + the top left-hand corner of the screen. + + + void SPR_DrawHoles( int frame, int x, int y ); + Precondition: SPR_Set has already been called for a sprite. + Draws the currently active sprite to the screen. Color index #255 is treated as transparent. + + void SPR_DrawAdditive( int frame, int x, int y ); + Precondition: SPR_Set has already been called for a sprite. + Draws the currently active sprite to the screen, adding it's color values to the background. + + void SPR_EnableScissor( int x, int y, int width, int height ); + Creates a clipping rectangle. No pixels will be drawn outside the specified area. Will + stay in effect until either the next frame, or SPR_DisableScissor is called. + + void SPR_DisableScissor( void ); + Disables the effect of an SPR_EnableScissor call. + + int IsHighRes( void ); + returns 1 if the res mode is 640x480 or higher; 0 otherwise. + + int ScreenWidth( void ); + returns the screen width, in pixels. + + int ScreenHeight( void ); + returns the screen height, in pixels. + +// Sound functions + void PlaySound( char *szSound, int volume ) + plays the sound 'szSound' at the specified volume. Loads the sound if it hasn't been cached. + If it can't find the sound, it displays an error message and plays no sound. + + void PlaySound( int iSound, int volume ) + Precondition: iSound has been precached. + Plays the sound, from the precache list. + + +// Communication functions + void SendClientCmd( char *szCmdString ); + sends a command to the server, just as if the client had typed the szCmdString at the console. + + char *GetPlayerName( int entity_number ); + returns a pointer to a string, that contains the name of the specified client. + Returns NULL if the entity_number is not a client. + + + DECLARE_MESSAGE(), HOOK_MESSAGE() + These two macros bind the message sending between the entity DLL and the client DLL to + the CHud object. + + HOOK_MESSAGE( message_name ) + This is used inside CHud::Init(). It calls into the engine to hook that message + from the incoming message stream. + Precondition: There must be a function of name UserMsg_message_name declared + for CHud. Eg, CHud::UserMsg_Health() must be declared if you want to + use HOOK_MESSAGE( Health ); + + DECLARE_MESSAGE( message_name ) + For each HOOK_MESSAGE you must have an equivalent DECLARE_MESSAGE. This creates + a function which passes the hooked messages into the CHud object. + + + HOOK_COMMAND(), DECLARE_COMMAND() + These two functions declare and hook console commands into the client dll. + + HOOK_COMMAND( char *command, command_name ) + Whenever the user types the 'command' at the console, the function 'command_name' + will be called. + Precondition: There must be a function of the name UserCmd_command_name declared + for CHud. Eg, CHud::UserMsg_ShowScores() must be declared if you want to + use HOOK_COMMAND( "+showscores", ShowScores ); + + DECLARE_COMMAND( command_name ) + For each HOOK_COMMAND you must have an equivelant DECLARE_COMMAND. This creates + a function which passes the hooked commands into the CHud object. + diff --git a/common/common.dsp b/common/common.dsp index d2f83b69..f1f38253 100644 --- a/common/common.dsp +++ b/common/common.dsp @@ -91,7 +91,7 @@ BSC32=bscmake.exe # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept -# ADD LINK32 msvcrt.lib user32.lib /nologo /dll /debug /machine:I386 /nodefaultlib:"msvcrtd.lib" /pdbtype:sept +# ADD LINK32 msvcrtd.lib user32.lib /nologo /dll /debug /machine:I386 /nodefaultlib:"libc.lib" /pdbtype:sept # SUBTRACT LINK32 /incremental:no /nodefaultlib # Begin Custom Build TargetDir=\Xash3D\src_main\temp\common\!debug diff --git a/debug.bat b/debug.bat index 4349fe9f..332a7b48 100644 --- a/debug.bat +++ b/debug.bat @@ -11,6 +11,9 @@ call vcvars32 %MSDEV% baserc/baserc.dsp %CONFIG%"baserc - Win32 Debug" %build_target% if errorlevel 1 set BUILD_ERROR=1 +%MSDEV% client/client.dsp %CONFIG%"client - Win32 Debug" %build_target% +if errorlevel 1 set BUILD_ERROR=1 + %MSDEV% engine/engine.dsp %CONFIG%"engine - Win32 Debug" %build_target% if errorlevel 1 set BUILD_ERROR=1 @@ -26,7 +29,7 @@ if errorlevel 1 set BUILD_ERROR=1 %MSDEV% render/render.dsp %CONFIG%"render - Win32 Debug" %build_target% if errorlevel 1 set BUILD_ERROR=1 -%MSDEV% sv_dll/server.dsp %CONFIG%"server - Win32 Debug" %build_target% +%MSDEV% server/server.dsp %CONFIG%"server - Win32 Debug" %build_target% if errorlevel 1 set BUILD_ERROR=1 %MSDEV% vprogs/vprogs.dsp %CONFIG%"vprogs - Win32 Debug" %build_target% @@ -59,6 +62,7 @@ if exist engine\engine.plg del /f /q engine\engine.plg if exist launch\launch.plg del /f /q launch\launch.plg if exist common\common.plg del /f /q common\common.plg if exist physic\physic.plg del /f /q physic\physic.plg +if exist server\server.plg del /f /q server\server.plg if exist render\render.plg del /f /q render\render.plg if exist viewer\viewer.plg del /f /q viewer\viewer.plg if exist vprogs\vprogs.plg del /f /q vprogs\vprogs.plg @@ -67,5 +71,5 @@ if exist vsound\vsound.plg del /f /q vsound\vsound.plg echo Build succeeded! echo Please wait. Xash is now loading cd D:\Xash3D\ -quake.exe -game tmpQuArK -log -debug -dev 3 +map qctest +quake.exe -game tmpQuArK -log -debug -dev 5 +map qctest :done \ No newline at end of file diff --git a/engine/client/cl_demo.c b/engine/client/cl_demo.c index 8f88333c..fd2210ed 100644 --- a/engine/client/cl_demo.c +++ b/engine/client/cl_demo.c @@ -34,7 +34,7 @@ void CL_WriteDemoHeader( const char *name ) { char buf_data[MAX_MSGLEN]; entity_state_t *state, nullstate; - pr_edict_t *ent; + edict_t *ent; sizebuf_t buf; int i, len; @@ -69,7 +69,7 @@ void CL_WriteDemoHeader( const char *name ) if( buf.cursize + com.strlen(cl.configstrings[i]) + 32 > buf.maxsize ) { // write it out - len = LittleLong (buf.cursize); + len = LittleLong( buf.cursize ); FS_Write( cls.demofile, &len, 4 ); FS_Write( cls.demofile, buf.data, buf.cursize ); buf.cursize = 0; @@ -81,16 +81,14 @@ void CL_WriteDemoHeader( const char *name ) } - CL_VM_Begin(); - // baselines - memset(&nullstate, 0, sizeof(nullstate)); + Mem_Set( &nullstate, 0, sizeof( nullstate )); - for( i = 0; i < prog->num_edicts; i++ ) + for( i = 0; i < game.numEntities; i++ ) { - ent = PRVM_EDICT_NUM( i ); - state = &ent->priv.cl->baseline; - if(!state->model.index) continue; + ent = EDICT_NUM( i ); + state = &ent->pvEngineData->baseline; + if( !state->model.index ) continue; if( buf.cursize + 64 > buf.maxsize ) { @@ -101,7 +99,7 @@ void CL_WriteDemoHeader( const char *name ) buf.cursize = 0; } MSG_WriteByte( &buf, svc_spawnbaseline ); - MSG_WriteDeltaEntity (&nullstate, &ent->priv.cl->baseline, &buf, true, true ); + MSG_WriteDeltaEntity( &nullstate, &ent->pvEngineData->baseline, &buf, true, true ); } MSG_WriteByte( &buf, svc_stufftext ); @@ -111,8 +109,6 @@ void CL_WriteDemoHeader( const char *name ) len = LittleLong( buf.cursize ); FS_Write( cls.demofile, &len, 4 ); FS_Write( cls.demofile, buf.data, buf.cursize ); - - CL_VM_End(); } /* diff --git a/engine/client/cl_edict.h b/engine/client/cl_edict.h deleted file mode 100644 index 709eba31..00000000 --- a/engine/client/cl_edict.h +++ /dev/null @@ -1,79 +0,0 @@ -//======================================================================= -// Copyright XashXT Group 2008 © -// cl_edict.h - client prvm edict -//======================================================================= -#ifndef CL_EDICT_H -#define CL_EDICT_H - -struct cl_globalvars_s -{ - int pad[34]; - int pev; - int world; - string_t mapname; - float realtime; - float frametime; - vec3_t vieworg; - vec3_t viewangles; - vec3_t v_forward; - vec3_t v_right; - vec3_t v_up; - float onground; - float playernum; - float waterlevel; - float clientflags; - vec3_t cl_viewangles; - vec3_t simvel; - vec3_t simorg; - float idealpitch; - vec3_t viewheight; - float health; - float max_entities; - float maxclients; - float lerpfrac; - float intermission; - float demoplayback; - float paused; - vec3_t punchangle; - vec3_t crosshairangle; - int viewentity; - vec3_t blend_color; - float blend_alpha; - float screen_x; - float screen_y; - float screen_w; - float screen_h; - func_t HUD_Init; - func_t HUD_StudioEvent; - func_t HUD_ParseMessage; - func_t HUD_Render; - func_t HUD_UpdateEntities; - func_t HUD_Shutdown; - func_t V_CalcRefdef; -}; - -struct cl_entvars_s -{ - string_t classname; - string_t globalname; - float modelindex; - float soundindex; - int chain; - int owner; - string_t model; - vec3_t origin; - vec3_t angles; - vec3_t mins; - vec3_t maxs; - float solid; - float sequence; - float effects; - float frame; - float body; - float skin; - float flags; -}; - -#define PROG_CRC_CLIENT 3720 - -#endif//CL_EDICT_H \ No newline at end of file diff --git a/engine/client/cl_effects.c b/engine/client/cl_effects.c index 92919185..ae6e8ccc 100644 --- a/engine/client/cl_effects.c +++ b/engine/client/cl_effects.c @@ -587,8 +587,8 @@ void CL_AddParticles( void ) if( !cl_particles->integer ) return; - if( PRVM_EDICT_NUM( cl.frame.ps.number )->priv.cl->current.gravity != 0 ) - gravity = PRVM_EDICT_NUM( cl.frame.ps.number )->priv.cl->current.gravity / 800.0; + if( EDICT_NUM( cl.frame.ps.number )->pvEngineData->current.gravity != 0 ) + gravity = EDICT_NUM( cl.frame.ps.number )->pvEngineData->current.gravity / 800.0; // FIXME: register CS_GRAVITY else gravity = 1.0f; for( p = cl_active_particles; p; p = next ) @@ -682,7 +682,7 @@ void CL_AddParticles( void ) if( p->flags & PARTICLE_BOUNCE ) { - pr_edict_t *clent = PRVM_EDICT_NUM( cl.frame.ps.number ); + edict_t *clent = EDICT_NUM( cl.frame.ps.number ); // bouncy particle VectorSet(mins, -radius, -radius, -radius); diff --git a/engine/client/cl_frame.c b/engine/client/cl_frame.c index 251c6cbe..e93886ad 100644 --- a/engine/client/cl_frame.c +++ b/engine/client/cl_frame.c @@ -13,15 +13,15 @@ FRAME PARSING ========================================================================= */ -void CL_UpdateEntityFileds( pr_edict_t *ent ) +void CL_UpdateEntityFields( edict_t *ent ) { // copy state to progs - ent->progs.cl->classname = cl.edict_classnames[ent->priv.cl->current.classname]; - ent->progs.cl->modelindex = ent->priv.cl->current.model.index; - ent->progs.cl->soundindex = ent->priv.cl->current.soundindex; - ent->progs.cl->model = PRVM_SetEngineString( cl.configstrings[CS_MODELS+ent->priv.cl->current.model.index] ); - VectorCopy( ent->priv.cl->current.origin, ent->progs.cl->origin ); - VectorCopy( ent->priv.cl->current.angles, ent->progs.cl->angles ); + ent->v.classname = cl.edict_classnames[ent->pvEngineData->current.classname]; + ent->v.modelindex = ent->pvEngineData->current.model.index; + ent->v.ambient = ent->pvEngineData->current.soundindex; + ent->v.model = MAKE_STRING( cl.configstrings[CS_MODELS+ent->pvEngineData->current.model.index] ); + VectorCopy( ent->pvEngineData->current.origin, ent->v.origin ); + VectorCopy( ent->pvEngineData->current.angles, ent->v.angles ); } /* @@ -34,10 +34,10 @@ to the current frame */ void CL_DeltaEntity( sizebuf_t *msg, frame_t *frame, int newnum, entity_state_t *old, bool unchanged ) { - pr_edict_t *ent; + edict_t *ent; entity_state_t *state; - ent = PRVM_EDICT_NUM( newnum ); + ent = EDICT_NUM( newnum ); state = &cl_parse_entities[cl.parse_entities & (MAX_PARSE_ENTITIES-1)]; if( unchanged ) *state = *old; @@ -49,28 +49,28 @@ void CL_DeltaEntity( sizebuf_t *msg, frame_t *frame, int newnum, entity_state_t frame->num_entities++; // some data changes will force no lerping - if( state->model.index != ent->priv.cl->current.model.index || state->pmodel.index != ent->priv.cl->current.pmodel.index || state->model.body != ent->priv.cl->current.model.body - || state->model.sequence != ent->priv.cl->current.model.sequence || abs(state->origin[0] - ent->priv.cl->current.origin[0]) > 512 - || abs(state->origin[1] - ent->priv.cl->current.origin[1]) > 512 || abs(state->origin[2] - ent->priv.cl->current.origin[2]) > 512 ) + if( state->model.index != ent->pvEngineData->current.model.index || state->pmodel.index != ent->pvEngineData->current.pmodel.index || state->model.body != ent->pvEngineData->current.model.body + || state->model.sequence != ent->pvEngineData->current.model.sequence || abs(state->origin[0] - ent->pvEngineData->current.origin[0]) > 512 + || abs(state->origin[1] - ent->pvEngineData->current.origin[1]) > 512 || abs(state->origin[2] - ent->pvEngineData->current.origin[2]) > 512 ) { - ent->priv.cl->serverframe = -99; + ent->pvEngineData->serverframe = -99; } - if( ent->priv.cl->serverframe != cl.frame.serverframe - 1 ) + if( ent->pvEngineData->serverframe != cl.frame.serverframe - 1 ) { // duplicate the current state so lerping doesn't hurt anything - ent->priv.cl->prev = *state; + ent->pvEngineData->prev = *state; } else { // shuffle the last state to previous - ent->priv.cl->prev = ent->priv.cl->current; + ent->pvEngineData->prev = ent->pvEngineData->current; } - ent->priv.cl->serverframe = cl.frame.serverframe; - ent->priv.cl->current = *state; + ent->pvEngineData->serverframe = cl.frame.serverframe; + ent->pvEngineData->current = *state; // update prvm fields - CL_UpdateEntityFileds( ent ); + CL_UpdateEntityFields( ent ); } /* @@ -154,8 +154,8 @@ void CL_ParsePacketEntities( sizebuf_t *msg, frame_t *oldframe, frame_t *newfram if( oldnum > newnum ) { // delta from baseline - pr_edict_t *ent = PRVM_EDICT_NUM( newnum ); - CL_DeltaEntity( msg, newframe, newnum, &ent->priv.cl->baseline, false ); + edict_t *ent = EDICT_NUM( newnum ); + CL_DeltaEntity( msg, newframe, newnum, &ent->pvEngineData->baseline, false ); continue; } @@ -188,7 +188,7 @@ CL_ParseFrame void CL_ParseFrame( sizebuf_t *msg ) { int cmd, len, idx; - pr_edict_t *clent; + edict_t *clent; frame_t *old; memset( &cl.frame, 0, sizeof(cl.frame)); @@ -243,7 +243,7 @@ void CL_ParseFrame( sizebuf_t *msg ) cmd = MSG_ReadByte( msg ); if( cmd != svc_playerinfo ) Host_Error( "CL_ParseFrame: not clientindex\n" ); idx = MSG_ReadByte( msg ); - clent = PRVM_EDICT_NUM( idx ); // get client + clent = EDICT_NUM( idx ); // get client if((idx-1) != cl.playernum ) Host_Error("CL_ParseFrame: invalid playernum (%d should be %d)\n", idx-1, cl.playernum ); } else @@ -263,8 +263,8 @@ void CL_ParseFrame( sizebuf_t *msg ) if( sv_newprotocol->integer ) { // now we can reading delta player state - if( old ) cl.frame.ps = MSG_ParseDeltaPlayer( &old->ps, &clent->priv.cl->current ); - else cl.frame.ps = MSG_ParseDeltaPlayer( NULL, &clent->priv.cl->current ); + if( old ) cl.frame.ps = MSG_ParseDeltaPlayer( &old->ps, &clent->pvEngineData->current ); + else cl.frame.ps = MSG_ParseDeltaPlayer( NULL, &clent->pvEngineData->current ); } // FIXME @@ -304,19 +304,17 @@ CL_AddPacketEntities void CL_AddPacketEntities( frame_t *frame ) { entity_state_t *s1; - pr_edict_t *ent; + edict_t *ent; int pnum; for( pnum = 0; pnum < frame->num_entities; pnum++ ) { s1 = &cl_parse_entities[(frame->parse_entities + pnum)&(MAX_PARSE_ENTITIES-1)]; - ent = PRVM_EDICT_NUM( s1->number ); - re->AddRefEntity( &ent->priv.cl->current, &ent->priv.cl->prev, cl.lerpfrac ); + ent = EDICT_NUM( s1->number ); + re->AddRefEntity( &ent->pvEngineData->current, &ent->pvEngineData->prev, cl.lerpfrac ); } } - - /* ============== CL_AddViewWeapon @@ -324,7 +322,7 @@ CL_AddViewWeapon */ void CL_AddViewWeapon( entity_state_t *ps ) { - pr_edict_t *view; // view model + edict_t *view; // view model // allow the gun to be completely removed if( !cl_gun->value ) return; @@ -332,12 +330,12 @@ void CL_AddViewWeapon( entity_state_t *ps ) // don't draw gun if in wide angle view if( ps->fov > 135 ) return; - view = PRVM_EDICT_NUM( ps->aiment ); - VectorCopy( cl.refdef.vieworg, view->priv.cl->current.origin ); - VectorCopy( cl.refdef.viewangles, view->priv.cl->current.angles ); - VectorCopy( cl.refdef.vieworg, view->priv.cl->prev.origin ); - VectorCopy( cl.refdef.viewangles, view->priv.cl->prev.angles ); - re->AddRefEntity( &view->priv.cl->current, &view->priv.cl->prev, cl.lerpfrac ); + view = EDICT_NUM( ps->aiment ); + VectorCopy( cl.refdef.vieworg, view->pvEngineData->current.origin ); + VectorCopy( cl.refdef.viewangles, view->pvEngineData->current.angles ); + VectorCopy( cl.refdef.vieworg, view->pvEngineData->prev.origin ); + VectorCopy( cl.refdef.viewangles, view->pvEngineData->prev.angles ); + re->AddRefEntity( &view->pvEngineData->current, &view->pvEngineData->prev, cl.lerpfrac ); } @@ -459,7 +457,7 @@ void CL_AddEntities( void ) void CL_GetEntitySoundSpatialization( int entnum, vec3_t origin, vec3_t velocity ) { - pr_edict_t *ent; + edict_t *ent; cmodel_t *cmodel; vec3_t midPoint; @@ -471,21 +469,21 @@ void CL_GetEntitySoundSpatialization( int entnum, vec3_t origin, vec3_t velocity return; } - ent = PRVM_EDICT_NUM( entnum ); + ent = EDICT_NUM( entnum ); // calculate origin - origin[0] = ent->priv.cl->prev.origin[0] + (ent->priv.cl->current.origin[0] - ent->priv.cl->prev.origin[0]) * cl.lerpfrac; - origin[1] = ent->priv.cl->prev.origin[1] + (ent->priv.cl->current.origin[1] - ent->priv.cl->prev.origin[1]) * cl.lerpfrac; - origin[2] = ent->priv.cl->prev.origin[2] + (ent->priv.cl->current.origin[2] - ent->priv.cl->prev.origin[2]) * cl.lerpfrac; + origin[0] = ent->pvEngineData->prev.origin[0] + (ent->pvEngineData->current.origin[0] - ent->pvEngineData->prev.origin[0]) * cl.lerpfrac; + origin[1] = ent->pvEngineData->prev.origin[1] + (ent->pvEngineData->current.origin[1] - ent->pvEngineData->prev.origin[1]) * cl.lerpfrac; + origin[2] = ent->pvEngineData->prev.origin[2] + (ent->pvEngineData->current.origin[2] - ent->pvEngineData->prev.origin[2]) * cl.lerpfrac; // calculate velocity - VectorSubtract( ent->priv.cl->current.origin, ent->priv.cl->prev.origin, velocity); + VectorSubtract( ent->pvEngineData->current.origin, ent->pvEngineData->prev.origin, velocity); VectorScale(velocity, 10, velocity); // if a brush model, offset the origin if( VectorIsNull( origin )) { - cmodel = cl.models[ent->priv.cl->current.model.index]; + cmodel = cl.models[ent->pvEngineData->current.model.index]; if( !cmodel ) return; VectorAverage( cmodel->mins, cmodel->maxs, midPoint ); VectorAdd( origin, midPoint, origin ); diff --git a/engine/client/cl_game.c b/engine/client/cl_game.c new file mode 100644 index 00000000..e4f184e6 --- /dev/null +++ b/engine/client/cl_game.c @@ -0,0 +1,924 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// cl_game.c - client dlls interaction +//======================================================================= + +#include "common.h" +#include "client.h" +#include "byteorder.h" +#include "matrix_lib.h" +#include "com_library.h" +#include "const.h" + +/* +==================== +CL_GetClientEntity + +Render callback for studio models +==================== +*/ +entity_state_t *CL_GetEdictByIndex( int index ) +{ + return &EDICT_NUM( index )->pvEngineData->current; +} + +/* +==================== +CL_GetLocalPlayer + +Render callback for studio models +==================== +*/ +entity_state_t *CL_GetLocalPlayer( void ) +{ + return &EDICT_NUM( cl.playernum + 1 )->pvEngineData->current; +} + +/* +==================== +CL_GetMaxlients + +Render callback for studio models +==================== +*/ +int CL_GetMaxClients( void ) +{ + return com.atoi( cl.configstrings[CS_MAXCLIENTS] ); +} + +/* +================ +CL_FadeColor +================ +*/ +float *CL_FadeColor( float starttime, float endtime ) +{ + static vec4_t color; + float time, fade_time; + + if( starttime == 0 ) return NULL; + time = (cls.realtime * 0.001f) - starttime; + if( time >= endtime ) return NULL; + + // fade time is 1/4 of endtime + fade_time = endtime / 4; + fade_time = bound( 0.3f, fade_time, 10.0f ); + + // fade out + if((endtime - time) < fade_time) + color[3] = (endtime - time) * 1.0f / fade_time; + else color[3] = 1.0; + color[0] = color[1] = color[2] = 1.0f; + + return color; +} + +void CL_DrawHUD( void ) +{ +} + +void CL_CopyTraceResult( TraceResult *out, trace_t trace ) +{ + if( !out ) return; + + out->fAllSolid = trace.allsolid; + out->fStartSolid = trace.startsolid; + out->fStartStuck = trace.startstuck; + out->flFraction = trace.fraction; + out->iStartContents = trace.startcontents; + out->iContents = trace.contents; + out->iHitgroup = trace.hitgroup; + out->flPlaneDist = trace.plane.dist; + VectorCopy( trace.endpos, out->vecEndPos ); + VectorCopy( trace.plane.normal, out->vecPlaneNormal ); + + if( trace.surface ) + out->pTexName = trace.surface->name; + else out->pTexName = NULL; + out->pHit = trace.ent; +} + +void CL_PrepUserMessage( char *pszName, const int svc_num ) +{ + user_message_t *msg; + char *end; + + if( !pszName || !*pszName ) return; // ignore blank names + + if( game.numMessages == MAX_USER_MESSAGES ) + { + MsgDev( D_ERROR, "CL_PrepUserMessage: user messages limit is out\n" ); + return; + } + + // clear existing or allocate new one + msg = game.msg[game.numMessages]; + if( msg ) Mem_Set( msg, 0, sizeof( *msg )); + else msg = Mem_Alloc( cls.mempool, sizeof( *msg )); + + end = com.strchr( pszName, '@' ); + if( !end ) + { + MsgDev( D_ERROR, "CL_PrepUserMessage: can't register message %s\n", pszName ); + return; + } + + msg->size = com.atoi( end + 1 ); + pszName[end-pszName] = '\0'; // remove size description from MsgName + msg->name = pszName; + msg->number = svc_num; + game.numMessages++; + + // debug + Msg("name %s [%i][svc_%i]\n", msg->name, msg->size, msg->number ); +} + +void CL_ParseUserMessage( sizebuf_t *net_buffer, int svc_num ) +{ + user_message_t *msg; + int iSize; + byte *pbuf; + + // NOTE: any user message parse on engine, not in client.dll + if( svc_num >= game.numMessages || !game.msg[svc_num] ) + { + // unregister message can't be parsed + Host_Error( "CL_ParseUserMessage: illegible server message %d\n", svc_num ); + return; + } + + if( svc_num != game.msg[svc_num]->number ) + { + int i; + + // search for right number + for( i = 0; i < game.numMessages; i++ ) + { + msg = game.msg[i]; + if( !msg || msg->number == svc_num ) + break; + } + + // throw warn + MsgDev( D_WARN, "CL_ParseUserMessage: wrong message num %i\n", svc_num ); + + if( i == game.numMessages || !msg ) + { + // this never happens + Host_Error( "CL_ParseUserMessage: illegible server message %d\n", svc_num ); + return; + } + } + else msg = game.msg[svc_num]; + + iSize = msg->size; + pbuf = NULL; + + // message with variable sizes receive actual size as first byte + // FIXME: replace with short for support messages more than 255 bytes ? + if( iSize == -1 ) iSize = MSG_ReadByte( net_buffer ); + if( iSize > 0 ) pbuf = Mem_Alloc( cls.private, iSize ); + + // parse user message into buffer + MSG_ReadData( net_buffer, pbuf, iSize ); + + if( msg->func ) msg->func( msg->name, iSize, pbuf ); + else MsgDev( D_WARN, "CL_ParseUserMessage: message %s doesn't have execute function\n", msg->name ); + if( pbuf ) Mem_Free( pbuf ); +} + +void CL_InitEdict( edict_t *pEdict ) +{ + Com_Assert( pEdict == NULL ); + + pEdict->v.pContainingEntity = pEdict; // make cross-links for consistency + pEdict->pvEngineData = (ed_priv_t *)Mem_Alloc( cls.mempool, sizeof( ed_priv_t )); + pEdict->serialnumber = NUM_FOR_EDICT( pEdict ); // merged on first update + pEdict->free = false; +} + +void CL_FreeEdict( edict_t *pEdict ) +{ + Com_Assert( pEdict == NULL ); + Com_Assert( pEdict->free ); + + // unlink from world + // CL_UnlinkEdict( pEdict ); + + if( pEdict->pvEngineData ) Mem_Free( pEdict->pvEngineData ); + Mem_Set( &pEdict->v, 0, sizeof( entvars_t )); + + pEdict->pvEngineData = NULL; + + // mark edict as freed + pEdict->freetime = cl.time * 0.001f; + pEdict->serialnumber = 0; + pEdict->free = true; +} + +edict_t *CL_AllocEdict( void ) +{ + edict_t *pEdict; + int i; + + for( i = 0; i < game.numEntities; i++ ) + { + pEdict = EDICT_NUM( i ); + // the first couple seconds of server time can involve a lot of + // freeing and allocating, so relax the replacement policy + if( pEdict->free && ( pEdict->freetime < 2.0f || ((cl.time * 0.001f) - pEdict->freetime) > 0.5f )) + { + CL_InitEdict( pEdict ); + return pEdict; + } + } + + if( i == game.maxEntities ) + Host_Error( "CL_AllocEdict: no free edicts\n" ); + + game.numEntities++; + pEdict = EDICT_NUM( i ); + CL_InitEdict( pEdict ); + + return pEdict; +} + +void CL_FreeEdicts( void ) +{ + int i; + edict_t *ent; + + for( i = 0; game.numEntities; i++ ) + { + ent = EDICT_NUM( i ); + if( ent->free ) continue; + CL_FreeEdict( ent ); + } +} + +/* +=============================================================================== + CGame Builtin Functions + +=============================================================================== +*/ + +/* +========= +pfnMemAlloc + +========= +*/ +static void *pfnMemAlloc( size_t cb, const char *filename, const int fileline ) +{ + return com.malloc( cls.private, cb, filename, fileline ); +} + +/* +========= +pfnMemFree + +========= +*/ +static void pfnMemFree( void *mem, const char *filename, const int fileline ) +{ + com.free( mem, filename, fileline ); +} +/* +============= +pfnLoadShader + +============= +*/ +shader_t pfnLoadShader( const char *szShaderName ) +{ + if( !re ) return 0; // render not initialized + if( !szShaderName || !*szShaderName ) + { + MsgDev( D_ERROR, "CL_LoadShader: invalid shadername\n" ); + return -1; + } + + return re->RegisterShader( szShaderName, SHADER_NOMIP ); +} + +/* +============= +pfnFillRGBA + +============= +*/ +void pfnFillRGBA( int x, int y, int width, int height, const float *color, float alpha ) +{ + SCR_FillRect( x, y, width, height, GetRGBA( color[0], color[1], color[2], alpha )); + if( re ) re->SetColor( NULL ); +} + +/* +============= +pfnDrawImage + +============= +*/ +void pfnDrawImage( shader_t shader, int x, int y, int width, int height, int frame ) +{ + if( shader == -1 ) + { + MsgDev( D_ERROR, "CL_DrawImage: invalid shader handle\n" ); + return; + } + SCR_DrawPic( x, y, width, height, shader ); + if( re ) re->SetColor( NULL ); +} + +/* +============= +pfnSetColor + +============= +*/ +void pfnSetColor( float r, float g, float b, float a ) +{ + if( !re ) return; // render not initialized + re->SetColor( GetRGBA( r, g, b, a )); +} + +/* +============= +pfnRegisterVariable + +============= +*/ +void pfnRegisterVariable( const char *szName, const char *szValue, int flags, const char *szDesc ) +{ + // FIXME: translate client.dll flags to real cvar flags + Cvar_Get( szName, szValue, flags, szDesc ); +} + +/* +============= +pfnCvarSetValue + +============= +*/ +void pfnCvarSetValue( const char *cvar, float value ) +{ + Cvar_SetValue( cvar, value ); +} + +/* +============= +pfnGetCvarFloat + +============= +*/ +float pfnGetCvarFloat( const char *szName ) +{ + return Cvar_VariableValue( szName ); +} + +/* +============= +pfnGetCvarString + +============= +*/ +char* pfnGetCvarString( const char *szName ) +{ + return Cvar_VariableString( szName ); +} + +/* +============= +pfnGetCvarString + +============= +*/ +void pfnAddCommand( const char *cmd_name, xcommand_t func, const char *cmd_desc ) +{ + // NOTE: if( func == NULL ) cmd will be forwarded to a server + Cmd_AddCommand( cmd_name, func, cmd_desc ); +} + +/* +============= +pfnHookUserMsg + +============= +*/ +void pfnHookUserMsg( char *szMsgName, pfnUserMsgHook pfn ) +{ + user_message_t *msg; + int i; + + // ignore blank names + if( !szMsgName || !*szMsgName ) return; + for( i = 0; game.numMessages; i++ ) + { + msg = game.msg[i]; + if( !msg ) break; + + if( !com.strcmp( szMsgName, msg->name )) + { + // msg registration is complete + msg->func = pfn; + return; + } + } + MsgDev( D_ERROR, "CL_HookMessage: can't hook message %s\n", szMsgName ); + +} + +/* +============= +pfnServerCmd + +============= +*/ +void pfnServerCmd( const char *szCmdString ) +{ + // server command adding in cmds queue + Cbuf_AddText( va( "cmd %s", szCmdString )); +} + +/* +============= +pfnClientCmd + +============= +*/ +void pfnClientCmd( const char *szCmdString ) +{ + // client command executes immediately + Cmd_ExecuteString( szCmdString ); +} + +/* +============= +pfnTextMessageGet + +============= +*/ +void pfnGetPlayerInfo( int player_num, hud_player_info_t *pinfo ) +{ + // FIXME: implement + static hud_player_info_t null_info; + + Mem_Copy( pinfo, &null_info, sizeof( null_info )); +} + +/* +============= +pfnGetPlayerInfo + +============= +*/ +client_textmessage_t *pfnTextMessageGet( const char *pName ) +{ + // FIXME: implement + static client_textmessage_t null_msg; + + return &null_msg; +} + +/* +============= +pfnCmdArgc + +============= +*/ +int pfnCmdArgc( void ) +{ + return Cmd_Argc(); +} + +/* +============= +pfnCmdArgv + +============= +*/ +char *pfnCmdArgv( int argc ) +{ + if( argc >= 0 && argc < Cmd_Argc()) + return Cmd_Argv( argc ); + return ""; +} + +/* +============= +pfnAlertMessage + +============= +*/ +static void pfnAlertMessage( ALERT_TYPE type, char *szFmt, ... ) +{ + char buffer[2048]; // must support > 1k messages + va_list args; + + va_start( args, szFmt ); + com.vsnprintf( buffer, 2048, szFmt, args ); + va_end( args ); + + // FIXME: implement message filter + com.print( buffer ); +} + +/* +============= +pfnPlaySoundByName + +============= +*/ +void pfnPlaySoundByName( const char *szSound, float volume, const float *org ) +{ + S_StartLocalSound( szSound, volume, org ); +} + +/* +============= +pfnPlaySoundByIndex + +============= +*/ +void pfnPlaySoundByIndex( int iSound, float volume, const float *org ) +{ + // make sure what we in-bounds + iSound = bound( 0, iSound, MAX_SOUNDS ); + + if( cl.sound_precache[iSound] == 0 ) + { + MsgDev( D_ERROR, "CL_PlaySoundByIndex: invalid sound handle %i\n", iSound ); + return; + } + S_StartSound( org, cl.playernum + 1, CHAN_AUTO, cl.sound_precache[iSound], volume, ATTN_NORM, PITCH_NORM ); +} + +/* +============= +pfnDrawCenterPrint + +called each frame +============= +*/ +void pfnDrawCenterPrint( void ) +{ + char *start; + int l, x, y, w; + float *color; + + if( !cl.centerPrintTime ) return; + color = CL_FadeColor( cl.centerPrintTime * 0.001f, scr_centertime->value ); + if( !color ) + { + cl.centerPrintTime = 0; + return; + } + + re->SetColor( color ); + start = cl.centerPrint; + y = cl.centerPrintY - cl.centerPrintLines * BIGCHAR_HEIGHT / 2; + + while( 1 ) + { + char linebuffer[1024]; + + for ( l = 0; l < 50; l++ ) + { + if ( !start[l] || start[l] == '\n' ) + break; + linebuffer[l] = start[l]; + } + linebuffer[l] = 0; + + w = cl.centerPrintCharWidth * com.cstrlen( linebuffer ); + x = ( SCREEN_WIDTH - w )>>1; + + SCR_DrawStringExt( x, y, cl.centerPrintCharWidth, BIGCHAR_HEIGHT, linebuffer, color, false ); + + y += cl.centerPrintCharWidth * 1.5; + while( *start && ( *start != '\n' )) start++; + if( !*start ) break; + start++; + } + if( re ) re->SetColor( NULL ); +} + +/* +============= +pfnCenterPrint + +called once from message +============= +*/ +void pfnCenterPrint( const char *text, int y, int charWidth ) +{ + char *s; + + com.strncpy( cl.centerPrint, text, sizeof( cl.centerPrint )); + cl.centerPrintTime = cls.realtime; + cl.centerPrintY = y; + cl.centerPrintCharWidth = charWidth; + + // count the number of lines for centering + cl.centerPrintLines = 1; + s = cl.centerPrint; + while( *s ) + { + if( *s == '\n' ) + cl.centerPrintLines++; + s++; + } +} + +/* +============= +pfnDrawCharacter + +============= +*/ +int pfnDrawCharacter( int x, int y, int width, int height, int number ) +{ + if( number < 32 || number > 255 ) + { + MsgDev( D_WARN, "SCR_DrawChar: passed non-printable character %c\n", (char )number ); + return false; + } + + SCR_DrawChar( x, y, width, height, number ); + if( re ) re->SetColor( NULL ); + return true; +} + +/* +============= +pfnDrawString + +============= +*/ +void pfnDrawString( int x, int y, int width, int height, const char *text ) +{ + if( !text || !*text ) + { + MsgDev( D_ERROR, "SCR_DrawStringExt: passed null string!\n" ); + return; + } + + SCR_DrawStringExt( x, y, width, height, text, g_color_table[7], false ); + if( re ) re->SetColor( NULL ); +} + +void pfnGetImageSize( int *w, int *h, shader_t shader ) +{ + if( re ) re->DrawGetPicSize( w, h, shader ); + else + { + if( w ) *w = -1; + if( h ) *h = -1; + } +} + +/* +============= +pfnGetViewAngles + +return interpolated angles from previous frame +============= +*/ +void pfnGetViewAngles( float *angles ) +{ + if( angles == NULL ) return; + VectorCopy( cl.refdef.viewangles, angles ); +} + +/* +============= +pfnGetEntityByIndex + +============= +*/ +edict_t* pfnGetEntityByIndex( int idx ) +{ + if( idx < 0 || idx > game.numEntities ) + { + MsgDev( D_ERROR, "CL_GetEntityByIndex: invalid entindex %i\n", idx ); + return EDICT_NUM( 0 ); + } + return EDICT_NUM( idx ); +} + +/* +============= +pfnGetLocalPlayer + +============= +*/ +edict_t* pfnGetLocalPlayer( void ) +{ + return EDICT_NUM( cl.playernum + 1 ); +} + +/* +============= +pfnIsSpectateOnly + +============= +*/ +int pfnIsSpectateOnly( void ) +{ + // FIXME: implement + return 0; +} + +/* +============= +pfnGetClientTime + +============= +*/ +float pfnGetClientTime( void ) +{ + return cl.time * 0.001f; +} + +/* +============= +pfnGetMaxClients + +============= +*/ +int pfnGetMaxClients( void ) +{ + return com.atoi( cl.configstrings[CS_MAXCLIENTS] ); +} + +/* +============= +pfnGetViewModel + +can return NULL +============= +*/ +edict_t* pfnGetViewModel( void ) +{ + return EDICT_NUM( cl.playernum + 1 )->v.aiment; +} + +/* +============= +pfnPointContents + +============= +*/ +static int pfnPointContents( const float *rgflVector ) +{ + return CL_PointContents( rgflVector ); +} + +/* +============= +pfnTraceLine + +============= +*/ +static void pfnTraceLine( const float *v1, const float *v2, int fNoMonsters, edict_t *pentToSkip, TraceResult *ptr ) +{ + trace_t trace; + int move; + + move = (fNoMonsters) ? MOVE_NOMONSTERS : MOVE_NORMAL; + + if( IS_NAN(v1[0]) || IS_NAN(v1[1]) || IS_NAN(v1[2]) || IS_NAN(v2[0]) || IS_NAN(v1[2]) || IS_NAN(v2[2] )) + Host_Error( "CL_Trace: NAN errors detected ('%f %f %f', '%f %f %f'\n", v1[0], v1[1], v1[2], v2[0], v2[1], v2[2] ); + + trace = CL_Trace( v1, vec3_origin, vec3_origin, v2, move, pentToSkip, CL_ContentsMask( pentToSkip )); + CL_CopyTraceResult( ptr, trace ); +} + +static triapi_t gTriApi = +{ + sizeof( triapi_t ), +}; + +// engine callbacks +static cl_enginefuncs_t gEngfuncs = +{ + sizeof( cl_enginefuncs_t ), + pfnMemAlloc, + pfnMemFree, + pfnLoadShader, + pfnFillRGBA, + pfnDrawImage, + pfnSetColor, + pfnRegisterVariable, + pfnCvarSetValue, + pfnGetCvarFloat, + pfnGetCvarString, + pfnAddCommand, + pfnHookUserMsg, + pfnServerCmd, + pfnClientCmd, + pfnGetPlayerInfo, + pfnTextMessageGet, + pfnCmdArgc, + pfnCmdArgv, + pfnAlertMessage, + pfnPlaySoundByName, + pfnPlaySoundByIndex, + AngleVectors, + pfnDrawCenterPrint, + pfnCenterPrint, + pfnDrawCharacter, + pfnDrawString, + pfnGetImageSize, + pfnGetViewAngles, + pfnGetEntityByIndex, + pfnGetLocalPlayer, + pfnIsSpectateOnly, + pfnGetClientTime, + pfnGetMaxClients, + pfnGetViewModel, + pfnPointContents, + pfnTraceLine, + pfnRandomLong, + pfnRandomFloat, + pfnLoadFile, + pfnFreeFile, + pfnGetGameDir, + Host_Error, + &gTriApi +}; + +/* +==================== +StudioEvent + +Event callback for studio models +==================== +*/ +void CL_StudioEvent( dstudioevent_t *event, entity_state_t *ent ) +{ + // do upcast + edict_t *pEdict = EDICT_NUM( ent->number ); + + cls.dllFuncs.pfnStudioEvent( event, pEdict ); +} + +void CL_UnloadProgs( void ) +{ + // initialize game + cls.dllFuncs.pfnShutdown(); + + StringTable_Delete( game.hStringTable ); + Com_FreeLibrary( cls.game ); + Mem_FreePool( &cls.mempool ); + Mem_FreePool( &cls.private ); +} + +bool CL_LoadProgs( const char *name ) +{ + static CLIENTAPI GetClientAPI; + string libname; + edict_t *e; + int i; + + if( cls.game ) CL_UnloadProgs(); + + // fill it in + com.snprintf( libname, MAX_STRING, "bin/%s.dll", name ); + cls.mempool = Mem_AllocPool( "Client Edicts Zone" ); + cls.private = Mem_AllocPool( "Client Private Zone" ); + + cls.game = Com_LoadLibrary( libname ); + if( !cls.game ) return false; + + GetClientAPI = (CLIENTAPI)Com_GetProcAddress( cls.game, "CreateAPI" ); + + if( !GetClientAPI ) + { + MsgDev( D_ERROR, "CL_LoadProgs: failed to get address of CreateAPI proc\n" ); + return false; + } + + if( !GetClientAPI( &cls.dllFuncs, &gEngfuncs, INTERFACE_VERSION )) + { + MsgDev( D_ERROR, "CL_LoadProgs: can't init client API\n" ); + return false; + } + + // 65535 unique strings should be enough ... + game.hStringTable = StringTable_Create( "Client Strings", 0x10000 ); + StringTable_SetString( game.hStringTable, "" ); // make NULL string + game.maxEntities = host.max_edicts; // FIXME: must come from CS_MAXENTITIES + game.maxClients = Host_MaxClients(); + game.edicts = Mem_Alloc( cls.mempool, sizeof( edict_t ) * game.maxEntities ); + game.numMessages = 1; // message with index 0 it's svc_bad + + for( i = 0, e = game.edicts; i < game.maxEntities; i++, e++ ) + e->free = true; // mark all edicts as freed + + // initialize game + cls.dllFuncs.pfnInit(); + + return true; +} \ No newline at end of file diff --git a/engine/client/cl_main.c b/engine/client/cl_main.c index 91d0266c..cb6f891c 100644 --- a/engine/client/cl_main.c +++ b/engine/client/cl_main.c @@ -869,8 +869,6 @@ void CL_PacketEvent( netadr_t from, sizebuf_t *msg ) if( host.type == HOST_DEDICATED || cls.demoplayback ) return; - CL_VM_Begin(); - if( msg->cursize >= 4 && *(int *)msg->data == -1 ) { cls.netchan.last_received = cls.realtime; @@ -906,7 +904,6 @@ void CL_PacketEvent( netadr_t from, sizebuf_t *msg ) if( cls.demorecording && !cls.demowaiting ) CL_WriteDemoMessage( msg, headerBytes ); } - CL_VM_End(); } void CL_ReadPackets( void ) @@ -1203,9 +1200,6 @@ void CL_Frame( dword time ) // if in the debugger last frame, don't timeout if( time > 5000 ) cls.netchan.last_received = Sys_Milliseconds(); - // setup the VM frame - CL_VM_Begin(); - // fetch results from server CL_ReadPackets(); @@ -1235,9 +1229,6 @@ void CL_Frame( dword time ) SCR_RunCinematic(); Con_RunConsole(); - // end the client VM frame - CL_VM_End(); - cls.framecount++; } @@ -1259,7 +1250,10 @@ void CL_Init( void ) Con_Init(); VID_Init(); - CL_InitClientProgs(); + if( !CL_LoadProgs( "client" )) + { + Host_Error( "CL_InitGame: can't initialize client.dll\n" ); + } UI_Init(); SCR_Init(); CL_InitLocal(); @@ -1283,7 +1277,7 @@ void CL_Shutdown( void ) if( !cls.initialized ) return; CL_WriteConfiguration(); - CL_FreeClientProgs(); + CL_UnloadProgs(); UI_Shutdown(); S_Shutdown(); SCR_Shutdown(); diff --git a/engine/client/cl_parse.c b/engine/client/cl_parse.c index 55bfe46b..c48a9f21 100644 --- a/engine/client/cl_parse.c +++ b/engine/client/cl_parse.c @@ -275,17 +275,16 @@ void CL_ParseBaseline( sizebuf_t *msg ) { int newnum; entity_state_t nullstate; - pr_edict_t *ent; + edict_t *ent; - CL_VM_Begin(); - memset( &nullstate, 0, sizeof(nullstate)); + Mem_Set( &nullstate, 0, sizeof( nullstate )); newnum = MSG_ReadBits( msg, NET_WORD ); // increase edicts - while( newnum >= prog->num_edicts ) PRVM_ED_Alloc(); - ent = PRVM_EDICT_NUM( newnum ); + while( newnum >= game.numEntities ) CL_AllocEdict(); + ent = EDICT_NUM( newnum ); - MSG_ReadDeltaEntity( msg, &nullstate, &ent->priv.cl->baseline, newnum ); + MSG_ReadDeltaEntity( msg, &nullstate, &ent->pvEngineData->baseline, newnum ); } /* @@ -301,7 +300,6 @@ void CL_ParseConfigString( sizebuf_t *msg ) if( i < 0 || i >= MAX_CONFIGSTRINGS ) Host_Error("configstring > MAX_CONFIGSTRINGS\n"); com.strcpy( cl.configstrings[i], MSG_ReadString( msg )); - CL_VM_Begin(); // do something apropriate if( i == CS_SKYNAME && cl.video_prepped ) @@ -327,13 +325,12 @@ void CL_ParseConfigString( sizebuf_t *msg ) } else if( i >= CS_USER_MESSAGES && i < CS_USER_MESSAGES+MAX_USER_MESSAGES ) { - // FIXME: register user message here - // Msg("PrepUserMessage: %s[svc_%i]\n", cl.configstrings[i], i - CS_USER_MESSAGES ); + CL_PrepUserMessage( cl.configstrings[i], i - CS_USER_MESSAGES ); } else if( i >= CS_CLASSNAMES && i < CS_CLASSNAMES+MAX_CLASSNAMES ) { - // prvm classnames for search by classname on client vm - cl.edict_classnames[i-CS_CLASSNAMES] = PRVM_SetEngineString( cl.configstrings[i] ); + // edicts classnames for search by classname on client + cl.edict_classnames[i-CS_CLASSNAMES] = MAKE_STRING( cl.configstrings[i] ); } else if( i >= CS_LIGHTSTYLES && i < CS_LIGHTSTYLES+MAX_LIGHTSTYLES ) { @@ -372,9 +369,6 @@ void CL_ParseServerMessage( sizebuf_t *msg ) char *s; int cmd; - // client progs can recivied messages too - cls.multicast = msg; - // parse the message while( 1 ) { @@ -442,9 +436,7 @@ void CL_ParseServerMessage( sizebuf_t *msg ) Host_Error( "CL_ParseServerMessage: svc_bad\n" ); break; default: - // parse user messages - if(!CL_ParseUserMessage( cmd )) - Host_Error("CL_ParseServerMessage: illegible server message %d\n", cmd ); + CL_ParseUserMessage( msg, cmd ); break; } } diff --git a/engine/client/cl_pred.c b/engine/client/cl_phys.c similarity index 70% rename from engine/client/cl_pred.c rename to engine/client/cl_phys.c index 6e99ef7f..1018c397 100644 --- a/engine/client/cl_pred.c +++ b/engine/client/cl_phys.c @@ -1,22 +1,7 @@ -/* -Copyright (C) 1997-2001 Id Software, Inc. - -This program is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -*/ +//======================================================================= +// Copyright XashXT Group 2008 © +// cl_physics.c - client physic and prediction +//======================================================================= #include "common.h" #include "client.h" @@ -62,18 +47,39 @@ void CL_CheckPredictionError (void) } } +/* +=============================================================================== + +LINE TESTING IN HULLS + +=============================================================================== +*/ +int CL_ContentsMask( const edict_t *passedict ) +{ + if( passedict ) + { + if( passedict->v.flags & FL_MONSTER ) + return MASK_MONSTERSOLID; + else if( passedict->v.flags & FL_CLIENT ) + return MASK_PLAYERSOLID; + else if( passedict->v.solid == SOLID_TRIGGER ) + return CONTENTS_SOLID|CONTENTS_BODY; + return MASK_SOLID; + } + return MASK_SOLID; +} + /* ================== CL_Trace ================== */ -trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, pr_edict_t *passedict, int contentsmask ) +trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int type, edict_t *passedict, int contentsmask ) { vec3_t hullmins, hullmaxs; int i, bodycontents; - int passedictprog; bool pointtrace; - pr_edict_t *traceowner, *touch; + edict_t *traceowner, *touch; trace_t trace; vec3_t clipboxmins, clipboxmaxs; // bounding box of entire move area vec3_t clipmins, clipmaxs; // size of the moving object @@ -83,7 +89,7 @@ trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, cons matrix4x4 matrix, imatrix; // matrices to transform into/out of other entity's space cmodel_t *model; // model of other entity int numtouchedicts = 0; // list of entities to test for collisions - pr_edict_t *touchedicts[MAX_EDICTS]; + edict_t *touchedicts[MAX_EDICTS]; VectorCopy( start, clipstart ); VectorCopy( end, clipend ); @@ -96,7 +102,7 @@ trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, cons pe->ClipToWorld( &cliptrace, cl.worldmodel, clipstart, clipmins, clipmaxs, clipend, contentsmask ); cliptrace.startstuck = cliptrace.startsolid; if( cliptrace.startsolid || cliptrace.fraction < 1 ) - cliptrace.ent = prog ? (edict_t *)prog->edicts : NULL; + cliptrace.ent = (edict_t *)EDICT_NUM( 0 ); if( type == MOVE_WORLDONLY ) return cliptrace; if( type == MOVE_MISSILE ) @@ -120,21 +126,18 @@ trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, cons } // if the passedict is world, make it NULL (to avoid two checks each time) - // this checks prog because this function is often called without a CSQC - // VM context - if( prog == NULL || passedict == prog->edicts ) passedict = NULL; - // precalculate prog value for passedict for comparisons - passedictprog = prog != NULL ? PRVM_EDICT_TO_PROG(passedict) : 0; + if( passedict == EDICT_NUM( 0 )) passedict = NULL; // figure out whether this is a point trace for comparisons pointtrace = VectorCompare( clipmins, clipmaxs ); // precalculate passedict's owner edict pointer for comparisons - traceowner = passedict ? PRVM_PROG_TO_EDICT( passedict->progs.cl->owner ) : NULL; + if( passedict && passedict->v.owner ) + traceowner = passedict->v.owner; + else traceowner = NULL; // clip to entities // because this uses World_EntitiestoBox, we know all entity boxes overlap // the clip region, so we can skip culling checks in the loop below - // note: if prog is NULL then there won't be any linked entities - numtouchedicts = 0;//CL_AreaEdicts( clipboxmins, clipboxmaxs, touchedicts, host.max_edicts ); + numtouchedicts = 0;// FIXME: CL_AreaEdicts( clipboxmins, clipboxmaxs, touchedicts, host.max_edicts ); if( numtouchedicts > host.max_edicts ) { // this never happens @@ -144,9 +147,8 @@ trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, cons for( i = 0; i < numtouchedicts; i++ ) { touch = touchedicts[i]; - - if( touch->progs.cl->solid < SOLID_BBOX ) continue; - if( type == MOVE_NOMONSTERS && touch->progs.cl->solid != SOLID_BSP ) + if( touch->v.solid < SOLID_BBOX ) continue; + if( type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP ) continue; if( passedict ) @@ -156,9 +158,9 @@ trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, cons // don't clip owned entities against owner if( traceowner == touch ) continue; // don't clip owner against owned entities - if( passedictprog == touch->progs.cl->owner ) continue; + if( passedict == touch->v.owner ) continue; // don't clip points against points (they can't collide) - if( pointtrace && VectorCompare( touch->progs.cl->mins, touch->progs.cl->maxs) && (type != MOVE_MISSILE || !((int)touch->progs.cl->flags & FL_MONSTER))) + if( pointtrace && VectorCompare( touch->v.mins, touch->v.maxs ) && (type != MOVE_MISSILE || !(touch->v.flags & FL_MONSTER))) continue; } @@ -166,20 +168,20 @@ trace_t CL_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, cons // might interact, so do an exact clip model = NULL; - if((int)touch->progs.cl->solid == SOLID_BSP || type == MOVE_HITMODEL ) + if( touch->v.solid == SOLID_BSP || type == MOVE_HITMODEL ) { - uint modelindex = (uint)touch->progs.cl->modelindex; + uint modelindex = (uint)touch->v.modelindex; // if the modelindex is 0, it shouldn't be SOLID_BSP! if( modelindex > 0 && modelindex < MAX_MODELS ) - model = cl.models[(int)touch->progs.cl->modelindex]; + model = cl.models[touch->v.modelindex]; } - if( model ) Matrix4x4_CreateFromEntity( matrix, touch->progs.cl->origin[0], touch->progs.cl->origin[1], touch->progs.cl->origin[2], touch->progs.cl->angles[0], touch->progs.cl->angles[1], touch->progs.cl->angles[2], 1 ); - else Matrix4x4_CreateTranslate( matrix, touch->progs.cl->origin[0], touch->progs.cl->origin[1], touch->progs.cl->origin[2] ); + if( model ) Matrix4x4_CreateFromEntity( matrix, touch->v.origin[0], touch->v.origin[1], touch->v.origin[2], touch->v.angles[0], touch->v.angles[1], touch->v.angles[2], 1 ); + else Matrix4x4_CreateTranslate( matrix, touch->v.origin[0], touch->v.origin[1], touch->v.origin[2] ); Matrix4x4_Invert_Simple( imatrix, matrix ); - if((int)touch->progs.cl->flags & FL_MONSTER) - pe->ClipToGenericEntity(&trace, model, touch->progs.cl->mins, touch->progs.cl->maxs, bodycontents, matrix, imatrix, clipstart, clipmins2, clipmaxs2, clipend, contentsmask ); - else pe->ClipToGenericEntity(&trace, model, touch->progs.cl->mins, touch->progs.cl->maxs, bodycontents, matrix, imatrix, clipstart, clipmins, clipmaxs, clipend, contentsmask ); - pe->CombineTraces( &cliptrace, &trace, (edict_t *)touch, touch->progs.cl->solid == SOLID_BSP ); + if( touch->v.flags & FL_MONSTER ) + pe->ClipToGenericEntity(&trace, model, touch->v.mins, touch->v.maxs, bodycontents, matrix, imatrix, clipstart, clipmins2, clipmaxs2, clipend, contentsmask ); + else pe->ClipToGenericEntity(&trace, model, touch->v.mins, touch->v.maxs, bodycontents, matrix, imatrix, clipstart, clipmins, clipmaxs, clipend, contentsmask ); + pe->CombineTraces( &cliptrace, &trace, (edict_t *)touch, touch->v.solid == SOLID_BSP ); } return cliptrace; } diff --git a/engine/client/cl_progs.c b/engine/client/cl_progs.c index edacf392..7dfb5866 100644 --- a/engine/client/cl_progs.c +++ b/engine/client/cl_progs.c @@ -6,222 +6,6 @@ #include "common.h" #include "client.h" -/* -================ -CL_FadeColor -================ -*/ -float *CL_FadeColor( float starttime, float endtime ) -{ - static vec4_t color; - float time, fade_time; - - if( starttime == 0 ) return NULL; - time = (cls.realtime * 0.001f) - starttime; - if( time >= endtime ) return NULL; - - // fade time is 1/4 of endtime - fade_time = endtime / 4; - fade_time = bound( 0.3f, fade_time, 10.0f ); - - // fade out - if((endtime - time) < fade_time) - color[3] = (endtime - time) * 1.0f / fade_time; - else color[3] = 1.0; - color[0] = color[1] = color[2] = 1.0f; - - return color; -} - -void CL_DrawHUD( void ) -{ - CL_VM_Begin(); - - // setup pparms - prog->globals.cl->health = cl.frame.ps.health; - prog->globals.cl->maxclients = com.atoi(cl.configstrings[CS_MAXCLIENTS]); - prog->globals.cl->max_entities = host.max_edicts; - prog->globals.cl->realtime = cls.realtime * 0.001f; - prog->globals.cl->paused = cl_paused->integer; - - // setup args - PRVM_G_FLOAT(OFS_PARM0) = (float)cls.state; - PRVM_ExecuteProgram (prog->globals.cl->HUD_Render, "HUD_Render"); -} - -bool CL_ParseUserMessage( int svc_number ) -{ - bool msg_parsed = false; - - // setup pparms - prog->globals.cl->health = cl.frame.ps.health; - prog->globals.cl->maxclients = com.atoi(cl.configstrings[CS_MAXCLIENTS]); - prog->globals.cl->realtime = cls.realtime * 0.001f; - prog->globals.cl->paused = cl_paused->integer; - - // setup args - PRVM_G_FLOAT(OFS_PARM0) = (float)svc_number; - PRVM_ExecuteProgram (prog->globals.cl->HUD_ParseMessage, "HUD_ParseMessage"); - msg_parsed = (bool)PRVM_G_FLOAT(OFS_RETURN); - - return msg_parsed; -} - -/* -==================== -StudioEvent - -Event callback for studio models -==================== -*/ -void CL_StudioEvent ( dstudioevent_t *event, entity_state_t *ent ) -{ - // setup args - PRVM_G_FLOAT(OFS_PARM0) = (float)event->event; - PRVM_G_INT(OFS_PARM1) = PRVM_SetEngineString( event->options ); - VectorCopy( ent->origin, PRVM_G_VECTOR(OFS_PARM2)); - VectorCopy( ent->angles, PRVM_G_VECTOR(OFS_PARM3)); - PRVM_ExecuteProgram( prog->globals.cl->HUD_StudioEvent, "HUD_StudioEvent"); -} - -/* -==================== -GetClientEntity - -Render callback for studio models -==================== -*/ -entity_state_t *CL_GetEdictByIndex( int index ) -{ - return &PRVM_EDICT_NUM( index )->priv.cl->current; -} - -entity_state_t *CL_GetLocalPlayer( void ) -{ - return &PRVM_EDICT_NUM( cl.playernum + 1 )->priv.cl->current; -} - -int CL_GetMaxClients( void ) -{ - return com.atoi(cl.configstrings[CS_MAXCLIENTS]); -} - -/* -=============================================================================== -Client Builtin Functions - -mathlib, debugger, and various misc helpers -=============================================================================== -*/ -void CL_BeginIncreaseEdicts( void ) -{ - int i; - pr_edict_t *ent; - - // links don't survive the transition, so unlink everything - for (i = 0, ent = prog->edicts; i < prog->max_edicts; i++, ent++) - { - } -} - -void CL_EndIncreaseEdicts( void ) -{ - int i; - pr_edict_t *ent; - - for (i = 0, ent = prog->edicts; i < prog->max_edicts; i++, ent++) - { - } -} - -void CL_InitEdict( pr_edict_t *e ) -{ - e->priv.cl->serialnumber = PRVM_NUM_FOR_EDICT(e); - e->priv.cl->free = false; -} - -void CL_FreeEdict( pr_edict_t *ed ) -{ - ed->priv.cl->freetime = cl.time * 0.001f; - ed->priv.cl->free = true; - - ed->progs.cl->model = 0; - ed->progs.cl->modelindex = 0; - ed->progs.cl->soundindex = 0; - ed->progs.cl->skin = 0; - ed->progs.cl->frame = 0; - VectorClear(ed->progs.cl->origin); - VectorClear(ed->progs.cl->angles); -} - -void CL_FreeEdicts( void ) -{ - int i; - pr_edict_t *ent; - - CL_VM_Begin(); - for( i = 1; prog && i < prog->num_edicts; i++ ) - { - ent = PRVM_EDICT_NUM(i); - CL_FreeEdict( ent ); - } - CL_VM_End(); -} - -void CL_CountEdicts( void ) -{ - pr_edict_t *ent; - int i, active = 0, models = 0; - - for (i = 0; i < prog->num_edicts; i++) - { - ent = PRVM_EDICT_NUM(i); - if (ent->priv.cl->free) continue; - active++; - if (ent->progs.cl->model) models++; - } - - Msg("num_edicts:%3i\n", prog->num_edicts); - Msg("active :%3i\n", active); - Msg("view :%3i\n", models); -} - -void CL_VM_Begin( void ) -{ - PRVM_Begin; - PRVM_SetProg( PRVM_CLIENTPROG ); - - if( prog ) *prog->time = cl.time * 0.001f; -} - -void CL_VM_End( void ) -{ - PRVM_End; -} - -bool CL_LoadEdict( pr_edict_t *ent ) -{ - return true; -} - -static void PF_BeginRead( void ) -{ -} - -static void PF_EndRead( void ) -{ -} - -static void PF_ReadChar (void){ PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadChar( cls.multicast ); } -static void PF_ReadByte (void){ PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadByte( cls.multicast ); } -static void PF_ReadShort (void){ PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadShort( cls.multicast ); } -static void PF_ReadLong (void){ PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadLong( cls.multicast ); } -static void PF_ReadFloat (void){ PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadFloat( cls.multicast ); } -static void PF_ReadAngle (void){ PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadBits( cls.multicast, NET_FLOAT ); } -static void PF_ReadCoord (void){ PRVM_G_FLOAT(OFS_RETURN) = MSG_ReadBits( cls.multicast, NET_FLOAT ); } -static void PF_ReadString (void){ PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString( MSG_ReadString( cls.multicast) ); } -static void PF_ReadEntity (void){ VM_RETURN_EDICT( PRVM_PROG_TO_EDICT( MSG_ReadShort( cls.multicast ))); } // entindex - /* ========= PF_drawnet @@ -294,96 +78,6 @@ static void PF_drawfps( void ) SCR_DrawBigStringColor(pos[0], pos[1], fpsstring, color ); } -/* -========= -PF_drawcenterprint - -void DrawCenterPrint( void ) -========= -*/ -static void PF_drawcenterprint( void ) -{ - char *start; - int l, x, y, w; - float *color; - - if(!cl.centerPrintTime ) return; - if(!VM_ValidateArgs( "DrawCenterPrint", 0 )) - return; - - color = CL_FadeColor( cl.centerPrintTime * 0.001f, scr_centertime->value ); - if( !color ) - { - cl.centerPrintTime = 0; - return; - } - - re->SetColor( color ); - start = cl.centerPrint; - y = cl.centerPrintY - cl.centerPrintLines * BIGCHAR_HEIGHT/2; - - while( 1 ) - { - char linebuffer[1024]; - - for ( l = 0; l < 50; l++ ) - { - if ( !start[l] || start[l] == '\n' ) - break; - linebuffer[l] = start[l]; - } - linebuffer[l] = 0; - - w = cl.centerPrintCharWidth * com.cstrlen( linebuffer ); - x = ( SCREEN_WIDTH - w )>>1; - - SCR_DrawStringExt( x, y, cl.centerPrintCharWidth, BIGCHAR_HEIGHT, linebuffer, color, false ); - - y += cl.centerPrintCharWidth * 1.5; - while ( *start && ( *start != '\n' )) start++; - if( !*start ) break; - start++; - } - re->SetColor( NULL ); -} - -/* -========= -PF_centerprint - -void HUD_CenterPrint( string text, float y, float charwidth ) -========= -*/ -static void PF_centerprint( void ) -{ - float y, width; - const char *text; - char *s; - - if(!VM_ValidateArgs( "HUD_CenterPrint", 3 )) - return; - - text = PRVM_G_STRING(OFS_PARM0); - y = PRVM_G_FLOAT(OFS_PARM1); - width = PRVM_G_FLOAT(OFS_PARM2); - VM_ValidateString( text ); - - com.strncpy( cl.centerPrint, text, sizeof(cl.centerPrint)); - - cl.centerPrintTime = cls.realtime; - cl.centerPrintY = y; - cl.centerPrintCharWidth = width; - - // count the number of lines for centering - cl.centerPrintLines = 1; - s = cl.centerPrint; - while( *s ) - { - if (*s == '\n') cl.centerPrintLines++; - s++; - } -} - /* ========= PF_levelshot @@ -408,96 +102,6 @@ static void PF_levelshot( void ) } } -/* -========= -PF_setcolor - -void HUD_SetColor( vector rgb, float alpha ) -========= -*/ -static void PF_setcolor( void ) -{ - float *rgb, alpha; - - if(!VM_ValidateArgs( "HUD_SetColor", 2 )) - return; - - rgb = PRVM_G_VECTOR(OFS_PARM0); - alpha = PRVM_G_FLOAT(OFS_PARM1); - re->SetColor( GetRGBA( rgb[0], rgb[1], rgb[2], alpha )); -} - -/* -========= -PF_startsound - -void CL_StartSound( vector pos, entity e, float chan, float sfx, float vol, float attn, float pitch, float localsound ) -========= -*/ -static void PF_startsound( void ) -{ - float volume; - int channel; - sound_t sound_num; - int attenuation; - float *pos = NULL; - bool client_sound; - int ent = 0; - float pitch; - - if( !VM_ValidateArgs( "CL_StartSound", 8 )) - return; - - pos = PRVM_G_VECTOR(OFS_PARM0); - ent = PRVM_G_EDICTNUM(OFS_PARM1); - channel = (int)PRVM_G_FLOAT(OFS_PARM2); - sound_num = (sound_t)PRVM_G_FLOAT(OFS_PARM3); - volume = PRVM_G_FLOAT(OFS_PARM4); - attenuation = (int)PRVM_G_FLOAT(OFS_PARM5); - pitch = (int)PRVM_G_FLOAT(OFS_PARM6); - client_sound = (bool)PRVM_G_FLOAT(OFS_PARM7); - - if( client_sound ) - { - S_StartSound( pos, ent, channel, sound_num, volume, attenuation, pitch ); - } - else if( cl.sound_precache[sound_num] ) - { - S_StartSound( pos, ent, channel, cl.sound_precache[sound_num], volume, attenuation, pitch ); - } - else VM_Warning( "CL_StartSound: can't play sound with index %i\n", sound_num ); -} - -/* -========= -PF_pointcontents - -float CL_PointContents( vector point ) -========= -*/ -static void PF_pointcontents( void ) -{ - if( !VM_ValidateArgs( "CL_PointContents", 1 )) - return; - PRVM_G_FLOAT(OFS_RETURN) = CL_PointContents(PRVM_G_VECTOR(OFS_PARM0)); -} - -/* -========= -PF_startsound - -sound_t CL_PrecacheSound( string samp ) -========= -*/ -static void PF_precachesound( void ) -{ - if( !VM_ValidateArgs( "CL_PrecacheSound", 1 )) - return; - - VM_ValidateString(PRVM_G_STRING(OFS_PARM0)); - PRVM_G_FLOAT(OFS_RETURN) = S_RegisterSound(PRVM_G_STRING(OFS_PARM0)); -} - /* ================= PF_findexplosionplane @@ -536,198 +140,4 @@ static void PF_findexplosionplane( void ) } } VectorCopy( dir, PRVM_G_VECTOR( OFS_RETURN )); -} - -// NOTE: intervals between various "interfaces" was leave for future expansions -prvm_builtin_t vm_cl_builtins[] = -{ -NULL, // #0 (leave blank as default, but can include easter egg ) - -// system events -VM_ConPrintf, // #1 void Con_Printf( ... ) -VM_ConDPrintf, // #2 void Con_DPrintf( float level, ... ) -VM_HostError, // #3 void Com_Error( ... ) -VM_SysExit, // #4 void Sys_Exit( void ) -VM_CmdArgv, // #5 string Cmd_Argv( float arg ) -VM_CmdArgc, // #6 float Cmd_Argc( void ) -NULL, // #7 -- reserved -- -NULL, // #8 -- reserved -- -NULL, // #9 -- reserved -- -NULL, // #10 -- reserved -- - -// common tools -VM_ComTrace, // #11 void Com_Trace( float enable ) -VM_ComFileExists, // #12 float Com_FileExists( string filename ) -VM_ComFileSize, // #13 float Com_FileSize( string filename ) -VM_ComFileTime, // #14 float Com_FileTime( string filename ) -VM_ComLoadScript, // #15 float Com_LoadScript( string filename ) -VM_ComResetScript, // #16 void Com_ResetScript( void ) -VM_ComReadToken, // #17 string Com_ReadToken( float newline ) -VM_Random, // #18 float Random( void ) -VM_ComSearchFiles, // #19 float Com_Search( string mask, float casecmp ) -VM_ComSearchNames, // #20 string Com_SearchFilename( float num ) -VM_RandomLong, // #21 float RandomLong( float min, float max ) -VM_RandomFloat, // #22 float RandomFloat( float min, float max ) -VM_RandomVector, // #23 vector RandomVector( vector min, vector max ) -VM_CvarRegister, // #24 void Cvar_Register( string name, string value, float flags ) -VM_CvarSetValue, // #25 void Cvar_SetValue( string name, float value ) -VM_CvarGetValue, // #26 float Cvar_GetValue( string name ) -VM_CvarSetString, // #27 void Cvar_SetString( string name, string value ) -VM_CvarGetString, // #28 void VM_CvarGetString( void ) -VM_ComVA, // #29 string va( ... ) -VM_ComStrlen, // #30 float strlen( string text ) -VM_TimeStamp, // #31 string Com_TimeStamp( float format ) -VM_LocalCmd, // #32 void LocalCmd( ... ) -VM_SubString, // #33 string substring( string s, float start, float length ) -VM_AddCommand, // #34 void Add_Command( string s ) -VM_atof, // #35 float atof( string s ) -VM_atoi, // #36 float atoi( string s ) -VM_atov, // #37 vector atov( string s ) -NULL, // #38 -- reserved -- -NULL, // #39 -- reserved -- -NULL, // #40 -- reserved -- - -// quakec intrinsics ( compiler will be lookup this functions, don't remove or rename ) -VM_SpawnEdict, // #41 entity spawn( void ) -VM_RemoveEdict, // #42 void remove( entity ent ) -VM_NextEdict, // #43 entity nextent( entity ent ) -VM_CopyEdict, // #44 void copyentity( entity src, entity dst ) -NULL, // #45 -- reserved -- -NULL, // #46 -- reserved -- -NULL, // #47 -- reserved -- -NULL, // #48 -- reserved -- -NULL, // #49 -- reserved -- -NULL, // #50 -- reserved -- - -// filesystem -VM_FS_Open, // #51 float fopen( string filename, float mode ) -VM_FS_Close, // #52 void fclose( float handle ) -VM_FS_Gets, // #53 string fgets( float handle ) -VM_FS_Puts, // #54 void fputs( float handle, string s ) -NULL, // #55 -- reserved -- -NULL, // #56 -- reserved -- -NULL, // #57 -- reserved -- -NULL, // #58 -- reserved -- -NULL, // #59 -- reserved -- -NULL, // #60 -- reserved -- - -// mathlib -VM_min, // #61 float min(float a, float b ) -VM_max, // #62 float max(float a, float b ) -VM_bound, // #63 float bound(float min, float val, float max) -VM_pow, // #64 float pow(float x, float y) -VM_sin, // #65 float sin(float f) -VM_cos, // #66 float cos(float f) -VM_tan, // #67 float tan(float f) -VM_asin, // #68 float asin(float f) -VM_acos, // #69 float acos(float f) -VM_atan, // #70 float atan(float f) -VM_sqrt, // #71 float sqrt(float f) -VM_rint, // #72 float rint (float v) -VM_floor, // #73 float floor(float v) -VM_ceil, // #74 float ceil (float v) -VM_fabs, // #75 float fabs (float f) -VM_abs, // #76 float abs (float f) -NULL, // #77 -- reserved -- -NULL, // #78 -- reserved -- -VM_VectorNormalize, // #79 vector VectorNormalize( vector v ) -VM_VectorLength, // #80 float VectorLength( vector v ) -e10, e10, // #81 - #100 are reserved for future expansions - -// network messaging -PF_BeginRead, // #101 void MsgBegin( void ) -PF_ReadByte, // #102 float ReadByte( void ) -PF_ReadChar, // #103 float ReadChar( void ) -PF_ReadShort, // #104 float ReadShort( void ) -PF_ReadLong, // #105 float ReadLong( void ) -PF_ReadFloat, // #106 float ReadFloat( void ) -PF_ReadAngle, // #107 float ReadAngle( void ) -PF_ReadCoord, // #108 float ReadCoord( void ) -PF_ReadString, // #109 string ReadString( void ) -PF_ReadEntity, // #110 entity ReadEntity( void ) -PF_EndRead, // #111 void MsgEnd( void ) - -// clientfuncs_t -VM_precache_pic, // #112 float precache_pic( string pic ) -VM_drawcharacter, // #113 float DrawChar( vector pos, float char, vector scale, vector rgb, float alpha ) -VM_drawstring, // #114 float DrawString( vector pos, string text, vector scale, vector rgb, float alpha ) -VM_drawpic, // #115 float DrawPic( vector pos, string pic, vector size, vector rgb, float alpha ) -VM_drawfill, // #116 void DrawFill( vector pos, vector size, vector rgb, float alpha ) -VM_drawmodel, // #117 void DrawModel( vector pos, vector size, string mod, vector org, vector ang, float seq ) -NULL, // #118 -- reserved -- -VM_getimagesize, // #119 vector getimagesize( string pic ) -PF_drawnet, // #120 void DrawNet( vector pos, string image ) -PF_drawfps, // #121 void DrawFPS( vector pos ) -PF_drawcenterprint, // #122 void DrawCenterPrint( void ) -PF_centerprint, // #123 void HUD_CenterPrint( string text, float y, float charwidth ) -PF_levelshot, // #124 float HUD_MakeLevelShot( void ) -PF_setcolor, // #125 void HUD_SetColor( vector rgb, float alpha ) -VM_localsound, // #126 void HUD_PlaySound( string sample ) -PF_startsound, // #127 void CL_StartSound( vector, entity, float, float, float, float, float, float ) -PF_addparticle, // #128 float AddParticle(vector, vector, vector, vector, vector, vector, vector, string, float) -PF_pointcontents, // #129 float CL_PointContents( vector point ) -PF_precachesound, // #130 sound_t CL_PrecacheSound( string samp ) -PF_adddecal, // #131 void AddDecal( vector, vector, vector, float, float, float, float, string, float ) -PF_addlight, // #132 void AddLight( vector pos, vector col, float rad, float decay, float time, float key ) -NULL, // #133 -NULL, // #134 -NULL, // #135 -NULL, // #136 -NULL, // #137 -PF_findexplosionplane, // #138 vector CL_FindExplosionPlane( vector org, float radius ) -}; - -const int vm_cl_numbuiltins = sizeof(vm_cl_builtins) / sizeof(prvm_builtin_t); //num of builtins - -void CL_InitClientProgs( void ) -{ - Msg("\n"); - PRVM_Begin; - - PRVM_InitProg( PRVM_CLIENTPROG ); - if( !prog->loaded ) - { - prog->progs_mempool = Mem_AllocPool( "Client Progs" ); - prog->name = "client"; - prog->builtins = vm_cl_builtins; - prog->numbuiltins = vm_cl_numbuiltins; - prog->edictprivate_size = sizeof(cl_edict_t); - prog->max_edicts = host.max_edicts<<2; - prog->limit_edicts = host.max_edicts; - prog->begin_increase_edicts = CL_BeginIncreaseEdicts; - prog->end_increase_edicts = CL_EndIncreaseEdicts; - prog->init_edict = CL_InitEdict; - prog->free_edict = CL_FreeEdict; - prog->count_edicts = CL_CountEdicts; - prog->load_edict = CL_LoadEdict; - prog->filecrc = PROG_CRC_CLIENT; - - // using default builtins - prog->init_cmd = VM_Cmd_Init; - prog->reset_cmd = VM_Cmd_Reset; - prog->error_cmd = VM_Error; - PRVM_LoadProgs( va("%s/client.dat", GI->vprogs_dir )); - } - - // init some globals - prog->globals.cl->realtime = cls.realtime * 0.001f; - prog->globals.cl->pev = 0; - prog->globals.cl->mapname = PRVM_SetEngineString( cls.servername ); - prog->globals.cl->playernum = cl.playernum; - - // call the prog init - PRVM_ExecuteProgram( prog->globals.cl->HUD_Init, "HUD_Init" ); - PRVM_End; -} - -void CL_FreeClientProgs( void ) -{ - CL_VM_Begin(); - - prog->globals.cl->realtime = cls.realtime * 0.001f; - prog->globals.cl->pev = 0; - PRVM_ExecuteProgram(prog->globals.cl->HUD_Shutdown, "HUD_Shutdown"); - PRVM_ResetProg(); - - CL_VM_End(); } \ No newline at end of file diff --git a/engine/client/cl_scrn.c b/engine/client/cl_scrn.c index 19c80c93..4648df96 100644 --- a/engine/client/cl_scrn.c +++ b/engine/client/cl_scrn.c @@ -177,9 +177,9 @@ void SCR_DrawStringExt( int x, int y, float w, float h, const char *string, floa re->SetColor( setColor ); while ( *s ) { - if(IsColorString( s )) + if( IsColorString( s )) { - if ( !forceColor ) + if( !forceColor ) { Mem_Copy( color, g_color_table[ColorIndex(*(s+1))], sizeof( color )); color[3] = setColor[3]; @@ -343,8 +343,6 @@ SCR_Init */ void SCR_Init( void ) { - cls.mempool = Mem_AllocPool( "Client Static" ); - scr_showpause = Cvar_Get("scr_showpause", "1", 0, "show pause picture" ); scr_centertime = Cvar_Get("scr_centertime", "2.5", 0, "centerprint hold time" ); scr_printspeed = Cvar_Get("scr_printspeed", "8", 0, "centerprint speed of print" ); @@ -368,5 +366,4 @@ void SCR_Init( void ) void SCR_Shutdown( void ) { - Mem_FreePool( &cls.mempool ); } \ No newline at end of file diff --git a/engine/client/cl_view.c b/engine/client/cl_view.c index f98f1399..669c4462 100644 --- a/engine/client/cl_view.c +++ b/engine/client/cl_view.c @@ -157,7 +157,6 @@ void V_RenderView( void ) // build a refresh entity list and calc cl.sim* // this also calls CL_CalcViewValues which loads // v_forward, etc. - CL_VM_Begin(); CL_AddEntities (); if( cl_testentities->value ) V_TestEntities(); diff --git a/engine/client/client.h b/engine/client/client.h index 7e1b0552..485b7c5b 100644 --- a/engine/client/client.h +++ b/engine/client/client.h @@ -23,7 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define CLIENT_H #include "mathlib.h" -#include "cl_edict.h" +#include "entity_def.h" +#include "clgame_api.h" #include "render_api.h" #define MAX_EDIT_LINE 256 @@ -31,6 +32,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define MAX_SERVERS 64 #define ColorIndex(c) (((c) - '0') & 7) +#define NUM_FOR_EDICT(e) ((int)((edict_t *)(e) - game.edicts)) +#define EDICT_NUM( num ) _EDICT_NUM( num, __FILE__, __LINE__ ) + //============================================================================= typedef struct frame_s { @@ -163,15 +167,11 @@ typedef enum dl_generic, } dltype_t; // download type -struct cl_edict_s +// cl_private_edict_t +struct ed_priv_s { - // generic_edict_t (don't move these fields!) - bool free; - float freetime; // cl.time when the object was freed int serverframe; // if not current, this ent isn't in the frame - int serialnumber; // client serialnumber - // cl_private_edict_t entity_state_t baseline; // delta from this if not from a previous frame entity_state_t current; entity_state_t prev; // will always be valid, but might just be a copy of current @@ -195,7 +195,32 @@ typedef struct serverinfo_s } serverinfo_t; -typedef enum {key_game, key_console, key_message, key_menu} keydest_t; +typedef enum { key_game, key_console, key_message, key_menu } keydest_t; + +typedef struct +{ + const char *name; + int number; // svc_ number + int size; // if size == -1, size come from first byte after svcnum + pfnUserMsgHook func; // user-defined function +} user_message_t; + +typedef struct +{ + int maxClients; + int numEntities; + int maxEntities; + user_message_t *msg[MAX_USER_MESSAGES]; + int numMessages; // actual count of user messages + + union + { + edict_t *edicts; // acess by edict number + void *vp; // acess by offset in bytes + }; + + int hStringTable; // stringtable handle +} clgame_static_t; typedef struct { @@ -203,7 +228,11 @@ typedef struct bool initialized; keydest_t key_dest; - byte *mempool; + + void *game; // pointer to client.dll + HUD_FUNCTIONS dllFuncs; // dll exported funcs + byte *mempool; // edicts pool + byte *private; // client.dll private pool int framecount; dword realtime; // always increasing, no clamping, etc @@ -214,7 +243,6 @@ typedef struct float connect_time; // for connection retransmits netchan_t netchan; - sizebuf_t *multicast; // ptr for current message buffer (net or demo flow) int serverProtocol; // in case we are doing some kind of version hack int challenge; // from the server to use for connecting @@ -245,6 +273,7 @@ typedef struct } client_static_t; extern client_static_t cls; +extern clgame_static_t game; /* ============================================================== @@ -253,20 +282,6 @@ SCREEN CONSTS ============================================================== */ -// all drawing is done to a 640*480 virtual screen size -// and will be automatically scaled to the real resolution -#define SCREEN_WIDTH 640 -#define SCREEN_HEIGHT 480 - -#define TINYCHAR_WIDTH (SMALLCHAR_WIDTH) -#define TINYCHAR_HEIGHT (SMALLCHAR_HEIGHT/2) -#define SMALLCHAR_WIDTH 8 -#define SMALLCHAR_HEIGHT 16 -#define BIGCHAR_WIDTH 16 -#define BIGCHAR_HEIGHT 24 -#define GIANTCHAR_WIDTH 32 -#define GIANTCHAR_HEIGHT 48 - extern vrect_t scr_vrect; // position of render window extern vec4_t g_color_table[8]; @@ -446,12 +461,34 @@ void CL_InitClientProgs( void ); void CL_FreeClientProgs( void ); int CL_GetMaxClients( void ); void CL_DrawHUD( void ); -pr_edict_t *CL_GetEdict( int entnum ); +edict_t *CL_GetEdict( int entnum ); float *CL_FadeColor( float starttime, float endtime ); -bool CL_ParseUserMessage( int svc_number ); void CL_FreeEdicts( void ); -void CL_VM_Begin( void ); -void CL_VM_End( void ); + +// +// cl_game.c +// +void CL_UnloadProgs( void ); +bool CL_LoadProgs( const char *name ); +void CL_ParseUserMessage( sizebuf_t *msg, int svc_num ); +void CL_PrepUserMessage( char *pszName, const int svc_num ); +edict_t *CL_AllocEdict( void ); +void CL_FreeEdict( edict_t *pEdict ); +string_t pfnAllocString( const char *szValue ); +const char *pfnGetString( string_t iString ); +void pfnGetGameDir( char *szGetGameDir ); +long pfnRandomLong( long lLow, long lHigh ); +float pfnRandomFloat( float flLow, float flHigh ); +byte* pfnLoadFile( const char *filename, int *pLength ); +void pfnFreeFile( void *buffer ); + +_inline edict_t *_EDICT_NUM( int n, const char *file, const int line ) +{ + if((n >= 0) && (n < game.maxEntities)) + return game.edicts + n; + Host_Error( "EDICT_NUM: bad number %i (called at %s:%i)\n", n, file, line ); + return NULL; +} // // cl_sound.c @@ -461,7 +498,7 @@ void CL_VM_End( void ); // if origin is NULL, the sound will be dynamically sourced from the entity #define S_StartStreaming if( se ) se->StartStreaming #define S_StartSound( a,b,c,d,e,f,g ) if( se ) se->StartSound( a, b, c, d, e, f, g, true ); -#define S_StartLocalSound( name ) ( se && se->StartLocalSound( name )) +#define S_StartLocalSound( a, b, c ) if( se ) se->StartLocalSound( a, b, c ) #define S_StartBackgroundTrack if( se ) se->StartBackgroundTrack #define S_StopBackgroundTrack if( se ) se->StopBackgroundTrack #define S_RawSamples if( se ) se->StreamRawSamples @@ -525,14 +562,15 @@ void V_RenderSplash( void ); float V_CalcFov( float fov_x, float width, float height ); // -// cl_pred.c +// cl_phys.c // void CL_InitPrediction (void); void CL_PredictMove (void); void CL_CheckPredictionError (void); int CL_PointContents( const vec3_t point ); +int CL_ContentsMask( const edict_t *passedict ); bool CL_AmbientLevel( const vec3_t point, float *volumes ); -trace_t CL_Trace(const vec3_t s1, const vec3_t m1, const vec3_t m2, const vec3_t s2, int type, pr_edict_t *e, int mask); +trace_t CL_Trace( const vec3_t s1, const vec3_t m1, const vec3_t m2, const vec3_t s2, int type, edict_t *e, int mask ); // // cl_ents.c @@ -590,8 +628,6 @@ extern field_t chatField; // cl_menu.c // extern bool ui_active; -extern const int vm_ui_numbuiltins; -extern prvm_builtin_t vm_ui_builtins[]; void UI_Init( void ); void UI_DrawCredits( void ); diff --git a/engine/common.h b/engine/common.h index 979a26c9..ba57cd31 100644 --- a/engine/common.h +++ b/engine/common.h @@ -145,6 +145,13 @@ PRVM INTERACTIONS */ char *ED_NewString( const char *string, byte *mempool ); +#define NUM_FOR_EDICT(e) ((int)((edict_t *)(e) - game.edicts)) +#define EDICT_NUM( num ) _EDICT_NUM( num, __FILE__, __LINE__ ) + +// for constant strings +#define STRING( offset ) pfnGetString( offset ) +#define MAKE_STRING(str) pfnAllocString( str ) + #define prog vm->prog // global callback to vprogs.dll #define PRVM_EDICT_NUM( num ) _PRVM_EDICT_NUM( num, __FILE__, __LINE__ ) @@ -317,13 +324,6 @@ enum e_trace MOVE_HITMODEL, }; -// client printf level -enum e_clprint -{ - PRINT_CONSOLE = 0, // normal messages - PRINT_CENTER, // centerprint - PRINT_CHAT, // chat messages (with sound) -}; extern byte *zonepool; #define Z_Malloc(size) Mem_Alloc( zonepool, size ) diff --git a/engine/common/con_main.c b/engine/common/con_main.c index 501040a4..2b2b7f2e 100644 --- a/engine/common/con_main.c +++ b/engine/common/con_main.c @@ -171,7 +171,7 @@ Con_CheckResize If the line width has changed, reformat the buffer. ================ */ -void Con_CheckResize (void) +void Con_CheckResize( void ) { int i, j, width, oldwidth, oldtotallines, numlines, numchars; short tbuf[CON_TEXTSIZE]; diff --git a/engine/common/engfuncs.c b/engine/common/engfuncs.c index 7a406358..1a460d90 100644 --- a/engine/common/engfuncs.c +++ b/engine/common/engfuncs.c @@ -9,6 +9,84 @@ #include "const.h" #include "client.h" +/* +============= +pfnAllocString + +============= +*/ +string_t pfnAllocString( const char *szValue ) +{ + return StringTable_SetString( game.hStringTable, szValue ); +} + +/* +============= +pfnGetString + +============= +*/ +const char *pfnGetString( string_t iString ) +{ + return StringTable_GetString( game.hStringTable, iString ); +} + +/* +============= +pfnLoadFile + +============= +*/ +byte* pfnLoadFile( const char *filename, int *pLength ) +{ + return FS_LoadFile( filename, pLength ); +} + +/* +============= +pfnFreeFile + +============= +*/ +void pfnFreeFile( void *buffer ) +{ + if( buffer ) Mem_Free( buffer ); +} + +/* +============= +pfnRandomLong + +============= +*/ +long pfnRandomLong( long lLow, long lHigh ) +{ + return Com_RandomLong( lLow, lHigh ); +} + +/* +============= +pfnRandomFloat + +============= +*/ +float pfnRandomFloat( float flLow, float flHigh ) +{ + return Com_RandomFloat( flLow, flHigh ); +} + +/* +============= +pfnGetGameDir + +============= +*/ +void pfnGetGameDir( char *szGetGameDir ) +{ + // FIXME: potentially crashpoint + com.strcpy( szGetGameDir, FS_Gamedir ); +} + /* ======================================================================= @@ -804,7 +882,8 @@ void VM_localsound( void ) return; s = PRVM_G_STRING( OFS_PARM0 ); - if(!S_StartLocalSound( s )) + S_StartLocalSound( s, 1.0f, NULL ); + else { VM_Warning( "localsound: can't play %s!\n", s ); PRVM_G_FLOAT(OFS_RETURN) = 0; diff --git a/engine/common/menu.c b/engine/common/menu.c index 64da59e8..a3d3cc92 100644 --- a/engine/common/menu.c +++ b/engine/common/menu.c @@ -102,8 +102,6 @@ void UI_KeyEvent( int key ) prog->globals.ui->time = cls.realtime * 0.001f; PRVM_G_INT(OFS_PARM1) = PRVM_SetEngineString(ascii); PRVM_ExecuteProgram (prog->globals.ui->m_keydown, "m_keydown"); - - CL_VM_Begin(); // restore clvm state } void UI_Draw( void ) @@ -115,8 +113,6 @@ void UI_Draw( void ) prog->globals.ui->time = cls.realtime * 0.001f; PRVM_ExecuteProgram (prog->globals.ui->m_draw, "m_draw"); UI_DrawCredits(); // display game credits - - CL_VM_Begin(); // restore clvm state } void UI_DrawCredits( void ) @@ -160,7 +156,6 @@ void UI_ShowMenu( void ) ui_active = true; prog->globals.ui->time = cls.realtime * 0.001f; PRVM_ExecuteProgram (prog->globals.ui->m_show, "m_show"); - CL_VM_Begin(); // restore clvm state } void UI_HideMenu( void ) @@ -173,7 +168,6 @@ void UI_HideMenu( void ) ui_active = false; prog->globals.ui->time = cls.realtime * 0.001f; PRVM_ExecuteProgram (prog->globals.ui->m_hide, "m_hide"); - CL_VM_Begin(); // restore clvm state } void UI_Shutdown( void ) diff --git a/engine/engine.dsp b/engine/engine.dsp index caf2f1da..369dc23d 100644 --- a/engine/engine.dsp +++ b/engine/engine.dsp @@ -134,6 +134,10 @@ SOURCE=.\client\cl_frame.c # End Source File # Begin Source File +SOURCE=.\client\cl_game.c +# End Source File +# Begin Source File + SOURCE=.\client\cl_input.c # End Source File # Begin Source File @@ -146,7 +150,7 @@ SOURCE=.\client\cl_parse.c # End Source File # Begin Source File -SOURCE=.\client\cl_pred.c +SOURCE=.\client\cl_phys.c # End Source File # Begin Source File @@ -262,10 +266,6 @@ SOURCE=.\server\sv_world.c # PROP Default_Filter "h;hpp;hxx;hm;inl;fi;fd" # Begin Source File -SOURCE=.\client\cl_edict.h -# End Source File -# Begin Source File - SOURCE=.\client\client.h # End Source File # Begin Source File diff --git a/engine/host.c b/engine/host.c index 33c0feee..4921ee03 100644 --- a/engine/host.c +++ b/engine/host.c @@ -529,7 +529,7 @@ void Host_Error( const char *error, ... ) if( host.framecount < 3 || host.state == HOST_SHUTDOWN ) { - Msg( "Host_InitError:" ); + Msg( "Host_InitError: " ); com.error( hosterror1 ); } else if( host.framecount == host.errorframe ) diff --git a/engine/server/server.h b/engine/server/server.h index 5c127306..92932d78 100644 --- a/engine/server/server.h +++ b/engine/server/server.h @@ -22,13 +22,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define SERVER_H #include "mathlib.h" -#include "svprog_def.h" +#include "entity_def.h" #include "svgame_api.h" //============================================================================= -#define NUM_FOR_EDICT(e) ((int)((edict_t *)(e) - game.edicts)) -#define EDICT_NUM( num ) _EDICT_NUM( num, __FILE__, __LINE__ ) - #define AREA_SOLID 1 #define AREA_TRIGGERS 2 @@ -215,7 +212,7 @@ typedef struct dword funcBase; // base offset int hStringTable; // stringtable handle -} game_static_t; +} svgame_static_t; typedef struct { @@ -254,7 +251,7 @@ typedef struct extern netadr_t master_adr[MAX_MASTERS]; // address of the master server extern const char *ed_name[]; extern server_static_t svs; // persistant server info -extern game_static_t game; // persistant game info +extern svgame_static_t game; // persistant game info extern server_t sv; // local server extern cvar_t *sv_paused; @@ -384,6 +381,11 @@ float SV_AngleMod( float ideal, float current, float speed ); void SV_SpawnEntities( const char *mapname, script_t *entities ); string_t pfnAllocString( const char *szValue ); const char *pfnGetString( string_t iString ); +void pfnGetGameDir( char *szGetGameDir ); +long pfnRandomLong( long lLow, long lHigh ); +float pfnRandomFloat( float flLow, float flHigh ); +byte* pfnLoadFile( const char *filename, int *pLength ); +void pfnFreeFile( void *buffer ); _inline edict_t *_EDICT_NUM( int n, const char * file, const int line ) { @@ -393,10 +395,6 @@ _inline edict_t *_EDICT_NUM( int n, const char * file, const int line ) return NULL; } -// for constant strings -#define STRING( offset ) pfnGetString( offset ) -#define MAKE_STRING(str) pfnAllocString( str ) - // // sv_studio.c // diff --git a/engine/server/sv_client.c b/engine/server/sv_client.c index ccbd18d7..0161ab81 100644 --- a/engine/server/sv_client.c +++ b/engine/server/sv_client.c @@ -302,7 +302,7 @@ void SV_FlushRedirect( netadr_t adr, int dest, char *buf ) case RD_CLIENT: if( !sv_client ) return; // client not set MSG_WriteByte( &sv_client->netchan.message, svc_print ); - MSG_WriteByte( &sv_client->netchan.message, PRINT_CONSOLE ); + MSG_WriteByte( &sv_client->netchan.message, HUD_PRINTCONSOLE ); MSG_WriteString( &sv_client->netchan.message, buf ); break; case RD_NONE: diff --git a/engine/server/sv_cmds.c b/engine/server/sv_cmds.c index 0278ddc2..de02f584 100644 --- a/engine/server/sv_cmds.c +++ b/engine/server/sv_cmds.c @@ -370,9 +370,9 @@ void SV_Kick_f( void ) } if(!SV_SetPlayer()) return; - SV_BroadcastPrintf( PRINT_CONSOLE, "%s was kicked\n", sv_client->name ); - SV_ClientPrintf(sv_client, PRINT_CONSOLE, "You were kicked from the game\n"); - SV_DropClient(sv_client); + SV_BroadcastPrintf( HUD_PRINTCONSOLE, "%s was kicked\n", sv_client->name ); + SV_ClientPrintf( sv_client, HUD_PRINTCONSOLE, "You were kicked from the game\n" ); + SV_DropClient( sv_client ); sv_client->lastmessage = svs.realtime; // min case there is a funny zombie } @@ -454,7 +454,7 @@ void SV_ConSay_f( void ) for (i = 0, client = svs.clients; i < Host_MaxClients(); i++, client++) { if( client->state != cs_spawned ) continue; - SV_ClientPrintf( client, PRINT_CHAT, "%s\n", text ); + SV_ClientPrintf( client, HUD_PRINTTALK, "%s\n", text ); } } diff --git a/engine/server/sv_frame.c b/engine/server/sv_frame.c index 38b6a7f1..ca7f576d 100644 --- a/engine/server/sv_frame.c +++ b/engine/server/sv_frame.c @@ -626,7 +626,7 @@ void SV_SendClientMessages( void ) { MSG_Clear( &cl->netchan.message ); MSG_Clear( &cl->datagram ); - SV_BroadcastPrintf( PRINT_CONSOLE, "%s overflowed\n", cl->name ); + SV_BroadcastPrintf( HUD_PRINTCONSOLE, "%s overflowed\n", cl->name ); SV_DropClient( cl ); } diff --git a/engine/server/sv_game.c b/engine/server/sv_game.c index ba0376a3..22c66f00 100644 --- a/engine/server/sv_game.c +++ b/engine/server/sv_game.c @@ -994,12 +994,24 @@ bool SV_ReadComment( char *comment, int savenum ) =============================================================================== */ -void *pfnMemAlloc( size_t cb, const char *filename, const int fileline ) +/* +========= +pfnMemAlloc + +========= +*/ +static void *pfnMemAlloc( size_t cb, const char *filename, const int fileline ) { - return com.malloc( svs.mempool, cb, filename, fileline ); + return com.malloc( svs.private, cb, filename, fileline ); } -void pfnMemFree( void *mem, const char *filename, const int fileline ) +/* +========= +pfnMemFree + +========= +*/ +static void pfnMemFree( void *mem, const char *filename, const int fileline ) { com.free( mem, filename, fileline ); } @@ -1482,18 +1494,6 @@ void pfnMakeVectors( const float *rgflVector ) AngleVectors( rgflVector, svs.globals->v_forward, svs.globals->v_right, svs.globals->v_up ); } -/* -============== -pfnAngleVectors - -can receive NULL output args -============== -*/ -void pfnAngleVectors( const float *rgflVector, float *forward, float *right, float *up ) -{ - AngleVectors( rgflVector, forward, right, up ); -} - /* ============== pfnCreateEntity @@ -1835,7 +1835,7 @@ pfnTraceLine ================= */ -void pfnTraceLine( const float *v1, const float *v2, int fNoMonsters, edict_t *pentToSkip, TraceResult *ptr ) +static void pfnTraceLine( const float *v1, const float *v2, int fNoMonsters, edict_t *pentToSkip, TraceResult *ptr ) { trace_t trace; int move; @@ -2100,7 +2100,7 @@ pfnPointContents ============= */ -int pfnPointContents( const float *rgflVector ) +static int pfnPointContents( const float *rgflVector ) { return SV_PointContents( rgflVector ); } @@ -2225,6 +2225,18 @@ void pfnWriteCoord( float flValue ) if( game.msg_leftsize != 0xFFFF ) game.msg_leftsize -= 4; } +/* +============= +pfnWriteFloat + +============= +*/ +void pfnWriteFloat( float flValue ) +{ + MSG_WriteFloat( &sv.multicast, flValue ); + if( game.msg_leftsize != 0xFFFF ) game.msg_leftsize -= 4; +} + /* ============= pfnWriteString @@ -2320,7 +2332,7 @@ pfnAlertMessage ============= */ -void pfnAlertMessage( ALERT_TYPE level, char *szFmt, ... ) +static void pfnAlertMessage( ALERT_TYPE level, char *szFmt, ... ) { char buffer[2048]; // must support > 1k messages va_list args; @@ -2362,28 +2374,6 @@ void pfnFreeEntPrivateData( edict_t *pEdict ) pEdict->pvServerData = NULL; // freed } -/* -============= -pfnAllocString - -============= -*/ -string_t pfnAllocString( const char *szValue ) -{ - return StringTable_SetString( game.hStringTable, szValue ); -} - -/* -============= -pfnGetString - -============= -*/ -const char *pfnGetString( string_t iString ) -{ - return StringTable_GetString( game.hStringTable, iString ); -} - /* ============= pfnPEntityOfEntOffset @@ -2481,8 +2471,18 @@ int pfnRegUserMsg( const char *pszName, int iSize ) { // register message first int msg_index; + string msg_name; - msg_index = SV_UserMessageIndex( pszName ); + // scan name for reserved symbol + if( com.strchr( pszName, '@' )) + { + MsgDev( D_ERROR, "SV_RegisterUserMessage: invalid name %s\n", pszName ); + return svc_bad; // force error + } + + // build message name, fmt: MsgName@size + com.snprintf( msg_name, MAX_STRING, "%s@%i", pszName, iSize ); + msg_index = SV_UserMessageIndex( msg_name ); if( iSize == -1 ) game.msg_sizes[msg_index] = 0xFFFF; else game.msg_sizes[msg_index] = iSize; @@ -2578,17 +2578,16 @@ void pfnClientPrintf( edict_t* pEdict, int ptype, const char *szMsg ) switch( ptype ) { - case PRINT_CONSOLE: - SV_ClientPrintf (client, PRINT_CONSOLE, (char *)szMsg ); + case HUD_PRINTTALK: + case HUD_PRINTNOTIFY: // don't leave message in console history + case HUD_PRINTCONSOLE: + SV_ClientPrintf( client, ptype, (char *)szMsg ); break; - case PRINT_CENTER: + case HUD_PRINTCENTER: MSG_Begin( svc_centerprint ); MSG_WriteString( &sv.multicast, szMsg ); MSG_Send( MSG_ONE_R, NULL, pEdict ); break; - case PRINT_CHAT: - SV_ClientPrintf( client, PRINT_CHAT, (char *)szMsg ); - break; default: MsgDev( D_ERROR, "SV_ClientPrintf: invalid destination\n" ); break; @@ -2608,7 +2607,7 @@ void pfnServerPrint( const char *szMsg ) // while loading in-progress we can sending message com.print( szMsg ); // only for local client } - else SV_BroadcastPrintf( PRINT_CONSOLE, (char *)szMsg ); + else SV_BroadcastPrintf( HUD_PRINTCONSOLE, (char *)szMsg ); } /* @@ -2710,28 +2709,6 @@ word pfnCRC_Final( word pulCRC ) return pulCRC ^ 0x0000; } -/* -============= -pfnRandomLong - -============= -*/ -long pfnRandomLong( long lLow, long lHigh ) -{ - return Com_RandomLong( lLow, lHigh ); -} - -/* -============= -pfnRandomFloat - -============= -*/ -float pfnRandomFloat( float flLow, float flHigh ) -{ - return Com_RandomFloat( flLow, flHigh ); -} - /* ============= pfnSetView @@ -2754,28 +2731,6 @@ void pfnCrosshairAngle( const edict_t *pClient, float pitch, float yaw ) // FIXME: implement } -/* -============= -pfnLoadFile - -============= -*/ -byte* pfnLoadFile( const char *filename, int *pLength ) -{ - return FS_LoadFile( filename, pLength ); -} - -/* -============= -pfnFreeFile - -============= -*/ -void pfnFreeFile( void *buffer ) -{ - if( buffer ) Mem_Free( buffer ); -} - /* ============= pfnCompareFileTime @@ -2800,19 +2755,7 @@ int pfnCompareFileTime( const char *filename1, const char *filename2, int *iComp /* ============= -pfnGetGameDir - -============= -*/ -void pfnGetGameDir( char *szGetGameDir ) -{ - // FIXME: potentially crashpoint - com.strcpy( szGetGameDir, FS_Gamedir ); -} - -/* -============= -pfnGetGameDir +pfnStaticDecal ============= */ @@ -3001,7 +2944,7 @@ static enginefuncs_t gEngfuncs = pfnEntitiesInPVS, pfnEntitiesInPHS, pfnMakeVectors, - pfnAngleVectors, + AngleVectors, pfnCreateEntity, pfnRemoveEntity, pfnCreateNamedEntity, @@ -3034,6 +2977,7 @@ static enginefuncs_t gEngfuncs = pfnWriteLong, pfnWriteAngle, pfnWriteCoord, + pfnWriteFloat, pfnWriteString, pfnWriteEntity, pfnCVarRegister, diff --git a/engine/server/sv_init.c b/engine/server/sv_init.c index cf5b5988..53feeeb1 100644 --- a/engine/server/sv_init.c +++ b/engine/server/sv_init.c @@ -22,7 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "server.h" server_static_t svs; // persistant server info -game_static_t game; // persistant game info +svgame_static_t game; // persistant game info server_t sv; // local server /* diff --git a/engine/server/sv_main.c b/engine/server/sv_main.c index e10f5da8..463a88c3 100644 --- a/engine/server/sv_main.c +++ b/engine/server/sv_main.c @@ -164,7 +164,7 @@ void SV_CheckTimeouts( void ) } if(( cl->state == cs_connected || cl->state == cs_spawned) && cl->lastmessage < droppoint ) { - SV_BroadcastPrintf( PRINT_CONSOLE, "%s timed out\n", cl->name ); + SV_BroadcastPrintf( HUD_PRINTCONSOLE, "%s timed out\n", cl->name ); SV_DropClient( cl ); cl->state = cs_free; // don't bother with zombie state } @@ -394,9 +394,9 @@ void SV_FinalMessage( char *message, bool reconnect ) sizebuf_t msg; int i; - MSG_Init( &msg, msg_buf, sizeof(msg_buf)); + MSG_Init( &msg, msg_buf, sizeof( msg_buf )); MSG_WriteByte( &msg, svc_print ); - MSG_WriteByte( &msg, PRINT_CONSOLE ); + MSG_WriteByte( &msg, HUD_PRINTCONSOLE ); MSG_WriteString( &msg, message ); if( reconnect ) diff --git a/engine/server/sv_phys.c b/engine/server/sv_phys.c index 741f304d..ef343832 100644 --- a/engine/server/sv_phys.c +++ b/engine/server/sv_phys.c @@ -154,7 +154,7 @@ trace_t SV_Trace( const vec3_t start, const vec3_t mins, const vec3_t maxs, cons uint modelindex = (uint)touch->v.modelindex; // if the modelindex is 0, it shouldn't be SOLID_BSP! if( modelindex > 0 && modelindex < MAX_MODELS ) - model = sv.models[(int)touch->v.modelindex]; + model = sv.models[touch->v.modelindex]; } if( model ) Matrix4x4_CreateFromEntity( matrix, touch->v.origin[0], touch->v.origin[1], touch->v.origin[2], touch->v.angles[0], touch->v.angles[1], touch->v.angles[2], 1 ); else Matrix4x4_CreateTranslate( matrix, touch->v.origin[0], touch->v.origin[1], touch->v.origin[2] ); diff --git a/launch/cvar.c b/launch/cvar.c index c44037df..8f36d76e 100644 --- a/launch/cvar.c +++ b/launch/cvar.c @@ -40,13 +40,13 @@ static long Cvar_GetHashValue( const char *fname ) Cvar_InfoValidate ============ */ -static bool Cvar_ValidateString(const char *s, bool isvalue ) +static bool Cvar_ValidateString( const char *s, bool isvalue ) { - if ( !s ) return false; - if (strstr(s, "\\") && !isvalue) + if( !s ) return false; + if( com.strstr( s, "\\" ) && !isvalue ) return false; - if (strstr(s, "\"")) return false; - if (strstr(s, ";")) return false; + if( com.strstr( s, "\"" )) return false; + if( com.strstr( s, ";" )) return false; return true; } diff --git a/public/clgame_api.h b/public/clgame_api.h new file mode 100644 index 00000000..af0213e2 --- /dev/null +++ b/public/clgame_api.h @@ -0,0 +1,235 @@ +//======================================================================= +// Copyright XashXT Group 2008 © +// clgame_api.h - entity interface between engine and clgame +//======================================================================= +#ifndef CLGAME_API_H +#define CLGAME_API_H + +typedef int HSPRITE; // handle to a graphic +typedef int (*pfnUserMsgHook)( const char *pszName, int iSize, void *pbuf ); // user message handle + +typedef enum +{ + TRI_FRONT = 0, + TRI_BACK, + TRI_NONE, +} TRI_CULL; + +typedef enum +{ + TRI_TRIANGLES = 0, + TRI_TRIANGLE_FAN, + TRI_TRIANGLE_STRIP, + TRI_POLYGON, + TRI_QUADS, + TRI_LINES, +} TRI_DRAW; + +typedef struct triapi_s +{ + size_t api_size; // must match with sizeof( triapi_t ); + + void (*Bind)( HSPRITE shader ); // use handle that return pfnLoadShader + void (*Begin)( TRI_DRAW mode ); + void (*End)( void ); + + void (*Vertex2f)( float x, float y ); + void (*Vertex3f)( float x, float y, float z ); + void (*Vertex2fv)( const float *v ); + void (*Vertex3fv)( const float *v ); + void (*Color3f)( float r, float g, float b ); + void (*Color4f)( float r, float g, float b, float a ); + void (*Color4ub)( byte r, byte g, byte b, byte a ); + void (*TexCoord2f)( float u, float v ); + void (*TexCoord2fv)( const float *v ); + void (*CullFace)( TRI_CULL mode ); +} triapi_t; + +// FIXME: get rid of this +typedef struct +{ + char *name; + short ping; + byte thisplayer; // TRUE if this is the calling player + + // stuff that's unused at the moment, but should be done + byte spectator; + byte packetloss; + + char *model; + short topcolor; + short bottomcolor; + +} hud_player_info_t; + +// FIXME: get rid of this +typedef struct client_textmessage_s +{ + int effect; + byte r1, g1, b1, a1; // 2 colors for effects + byte r2, g2, b2, a2; + float x; + float y; + float fadein; + float fadeout; + float holdtime; + float fxtime; + const char *pName; + const char *pMessage; +} client_textmessage_t; + +// NOTE: engine trace struct not matched with clgame trace +typedef struct +{ + BOOL fAllSolid; // if true, plane is not valid + BOOL fStartSolid; // if true, the initial point was in a solid area + BOOL fStartStuck; // if true, trace started from solid entity + float flFraction; // time completed, 1.0 = didn't hit anything + vec3_t vecEndPos; // final position + int iStartContents; // start pos conetnts + int iContents; // final pos contents + int iHitgroup; // 0 == generic, non zero is specific body part + float flPlaneDist; // planes distance + vec3_t vecPlaneNormal; // surface normal at impact + const char *pTexName; // texture name that we hitting (brushes and studiomodels) + edict_t *pHit; // entity the surface is on +} TraceResult; + +typedef struct ref_params_s +{ + // output + vec3_t vieworg; + vec3_t viewangles; + float fov; + + vec3_t forward; + vec3_t right; + vec3_t up; + + float frametime; // client frametime + float lerpfrac; // interp value + float time; // client time + + // misc + BOOL intermission; + BOOL demoplayback; + BOOL demorecord; + BOOL spectator; + BOOL paused; + qword iWeaponBits; // pev->weapon + dword iKeyBits; // pev->button + edict_t *onground; // pointer to onground entity + int waterlevel; + + // input + vec3_t velocity; + vec3_t angles; // input viewangles + vec3_t origin; // origin + viewheight = vieworg + + vec3_t viewheight; + float idealpitch; + float v_idlescale; // used for concussion effect + float mouse_sensitivity; + + int health; + vec3_t crosshairangle; // pfnCrosshairAngle values from server + vec3_t punchangle; // recivied from server + edict_t *viewentity; + int clientnum; + int num_entities; + int max_entities; + int max_clients; +} ref_params_t; + +typedef struct cl_enginefuncs_s +{ + // interface validator + size_t api_size; // must matched with sizeof(cl_enginefuncs_t) + + // engine memory manager + void* (*pfnMemAlloc)( size_t cb, const char *filename, const int fileline ); + void (*pfnMemFree)( void *mem, const char *filename, const int fileline ); + + // screen handlers + HSPRITE (*pfnLoadShader)( const char *szShaderName ); + void (*pfnFillRGBA)( int x, int y, int width, int height, const float *color, float alpha ); + void (*pfnDrawImage)( HSPRITE shader, int x, int y, int width, int height, int frame ); + void (*pfnSetColor)( float r, float g, float b, float a ); + + // cvar handlers + void (*pfnRegisterVariable)( const char *szName, const char *szValue, int flags, const char *szDesc ); + void (*pfnCvarSetValue)( const char *cvar, float value ); + float (*pfnGetCvarFloat)( const char *szName ); + char* (*pfnGetCvarString)( const char *szName ); + + // command handlers + void (*pfnAddCommand)( const char *cmd_name, void (*function)(void), const char *cmd_desc ); + void (*pfnHookUserMsg)( char *szMsgName, pfnUserMsgHook pfn ); + void (*pfnServerCmd)( const char *szCmdString ); + void (*pfnClientCmd)( const char *szCmdString ); + void (*pfnGetPlayerInfo)( int player_num, hud_player_info_t *pinfo ); + client_textmessage_t *(*pfnTextMessageGet)( const char *pName ); + + int (*pfnCmdArgc)( void ); + char *(*pfnCmdArgv)( int argc ); + void (*pfnAlertMessage)( ALERT_TYPE, char *szFmt, ... ); + + // sound handlers (NULL origin == play at current client origin) + void (*pfnPlaySoundByName)( const char *szSound, float volume, const float *org ); + void (*pfnPlaySoundByIndex)( int iSound, float volume, const float *org ); + + // vector helpers + void (*pfnAngleVectors)( const float *rgflVector, float *forward, float *right, float *up ); + + void (*pfnDrawCenterPrint)( void ); + void (*pfnCenterPrint)( const char *text, int y, int charWidth ); + int (*pfnDrawCharacter)( int x, int y, int width, int height, int number ); + void (*pfnDrawString)( int x, int y, int width, int height, const char *text ); + void (*pfnGetImageSize)( int *w, int *h, shader_t shader ); + + // local client handlers + void (*pfnGetViewAngles)( float *angles ); + edict_t* (*pfnGetEntityByIndex)( int idx ); // matched with entity serialnumber + edict_t* (*pfnGetLocalPlayer)( void ); + int (*pfnIsSpectateOnly)( void ); // returns 1 if the client is a spectator only + float (*pfnGetClientTime)( void ); + int (*pfnGetMaxClients)( void ); + edict_t* (*pfnGetViewModel)( void ); + + int (*pfnPointContents)( const float *rgflVector ); + void (*pfnTraceLine)( const float *v1, const float *v2, int fNoMonsters, edict_t *pentToSkip, TraceResult *ptr ); + + long (*pfnRandomLong)( long lLow, long lHigh ); + float (*pfnRandomFloat)( float flLow, float flHigh ); + byte* (*pfnLoadFile)( const char *filename, int *pLength ); + void (*pfnFreeFile)( void *buffer ); + void (*pfnGetGameDir)( char *szGetGameDir ); + void (*pfnHostError)( const char *szFmt, ... ); // invoke host error + + triapi_t *pTriAPI; + +} cl_enginefuncs_t; + +typedef struct +{ + // interface validator + size_t api_size; // must matched with sizeof(HUD_FUNCTIONS) + + int (*pfnVidInit)( void ); + void (*pfnInit)( void ); + int (*pfnRedraw)( float flTime, int intermission ); + int (*pfnUpdateClientData)( ref_params_t *parms, float flTime ); + void (*pfnReset)( void ); + void (*pfnFrame)( double time ); + void (*pfnShutdown)( void ); + void (*pfnDrawNormalTriangles)( void ); + void (*pfnDrawTransparentTriangles)( void ); + void (*pfnCreateEntities)( void ); + void (*pfnStudioEvent)( const dstudioevent_t *event, edict_t *entity ); + void (*pfnCalcRefdef)( ref_params_t *parms ); + +} HUD_FUNCTIONS; + +typedef int (*CLIENTAPI)( HUD_FUNCTIONS *pFunctionTable, cl_enginefuncs_t* pEngfuncsFromEngine, int interfaceVersion ); + +#endif//CLGAME_API_H \ No newline at end of file diff --git a/public/const.h b/public/const.h index b7d2710b..bcb1ccc3 100644 --- a/public/const.h +++ b/public/const.h @@ -5,6 +5,14 @@ #ifndef CONST_H #define CONST_H +// shared typedefs +typedef unsigned __int64 qword; +typedef unsigned long dword; +typedef unsigned int uint; +typedef unsigned short word; +typedef unsigned char byte; +typedef int shader_t; + // euler angle order #define PITCH 0 #define YAW 1 @@ -102,6 +110,16 @@ #define DAMAGE_YES 1 #define DAMAGE_AIM 2 +typedef enum +{ + at_console = 1, // format: [msg] + at_warning, // format: Warning: [msg] + at_error, // format: Error: [msg] + at_loading, // print messages during loading + at_aiconsole, // same as at_console, but only shown if developer level is 5! + at_logged // server print to console ( only in multiplayer games ). +} ALERT_TYPE; + // engine edict types typedef enum { @@ -210,4 +228,53 @@ typedef enum kRenderFxNoReflect, // don't reflecting in mirrors } kRenderFx_t; +// all drawing is done to a 640*480 virtual screen size +// and will be automatically scaled to the real resolution +#define SCREEN_WIDTH 640 +#define SCREEN_HEIGHT 480 + +#define TINYCHAR_WIDTH (SMALLCHAR_WIDTH) +#define TINYCHAR_HEIGHT (SMALLCHAR_HEIGHT/2) +#define SMALLCHAR_WIDTH 8 +#define SMALLCHAR_HEIGHT 16 +#define BIGCHAR_WIDTH 16 +#define BIGCHAR_HEIGHT 24 +#define GIANTCHAR_WIDTH 32 +#define GIANTCHAR_HEIGHT 48 + +#define HUD_PRINTNOTIFY 1 +#define HUD_PRINTCONSOLE 2 +#define HUD_PRINTTALK 3 +#define HUD_PRINTCENTER 4 + +#define INTERFACE_VERSION 1 // both the client and server iface version + +//======================================================================= +// +// server.dll - client.dll definitions only +// +//======================================================================= +#define MAX_WEAPONS 32 +#define MAX_AMMO_SLOTS 32 + +#define HIDEHUD_WEAPONS BIT( 0 ) +#define HIDEHUD_FLASHLIGHT BIT( 1 ) +#define HIDEHUD_ALL BIT( 2 ) +#define HIDEHUD_HEALTH BIT( 3 ) +#define ITEM_SUIT BIT( 4 ) + +enum ShakeCommand_t +{ + SHAKE_START = 0, // Starts the screen shake for all players within the radius. + SHAKE_STOP, // Stops the screen shake for all players within the radius. + SHAKE_AMPLITUDE, // Modifies the amplitude of an active screen shake for all players within the radius. + SHAKE_FREQUENCY, // Modifies the frequency of an active screen shake for all players within the radius. +}; + +#define FFADE_IN 0x0000 // Just here so we don't pass 0 into the function +#define FFADE_OUT 0x0001 // Fade out (not in) +#define FFADE_MODULATE 0x0002 // Modulate (don't blend) +#define FFADE_STAYOUT 0x0004 // ignores the duration, stays faded out until new ScreenFade message received +#define FFADE_CUSTOMVIEW 0x0008 // fading only at custom viewing (don't sending this to engine ) + #endif//CONST_H \ No newline at end of file diff --git a/public/svprog_def.h b/public/entity_def.h similarity index 75% rename from public/svprog_def.h rename to public/entity_def.h index 6474a53b..f477cf12 100644 --- a/public/svprog_def.h +++ b/public/entity_def.h @@ -1,57 +1,15 @@ //======================================================================= // Copyright XashXT Group 2008 © -// svprog_def.h - engine <-> svgame communications +// entity_def.h - generic engine edict //======================================================================= -#ifndef SVDEFS_API_H -#define SVDEFS_API_H +#ifndef ENTITY_DEF_H +#define ENTITY_DEF_H typedef struct ed_priv_s ed_priv_t; // engine private data typedef struct edict_s edict_t; -typedef struct globalvars_s -{ - float time; - float frametime; - string_t mapname; - string_t startspot; - vec3_t spotOffset; // landmark offset - - BOOL deathmatch; - BOOL coop; - BOOL teamplay; - - int serverflags; - int maxClients; - int numClients; // actual clients count - int maxEntities; - int numEntities; // actual ents count - - vec3_t v_forward; - vec3_t v_right; - vec3_t v_up; - - BOOL trace_allsolid; - BOOL trace_startsolid; - BOOL trace_startstuck; - float trace_fraction; - vec3_t trace_endpos; - vec3_t trace_plane_normal; - float trace_plane_dist; - int trace_start_contents; - int trace_contents; - int trace_hitgroup; - const char *trace_texture; // texture name that we hitting (brushes and studiomodels) - edict_t *trace_ent; - - int total_secrets; - int found_secrets; // number of secrets found - int total_monsters; - int killed_monsters; // number of monsters killed - - void *pSaveData; // savedata base offset -} globalvars_t; - -// NOT FINALIZED! +// TODO: move to CBaseEntity all fields which doesn't existing on client side +// TODO: generic edict must have all fields as valid on client side too typedef struct entvars_s { string_t classname; @@ -119,7 +77,7 @@ typedef struct entvars_s float health; float frags; - int weapons; // bit mask for available weapons + qword weapons; // bit mask for available weapons float takedamage; int deadflag; @@ -197,4 +155,4 @@ struct edict_s // other fields from progs come immediately after }; -#endif//SVDEFS_API_H \ No newline at end of file +#endif//ENTITY_DEF_H \ No newline at end of file diff --git a/public/svgame_api.h b/public/svgame_api.h index 87fcb9dd..5c6e35fb 100644 --- a/public/svgame_api.h +++ b/public/svgame_api.h @@ -5,26 +5,6 @@ #ifndef SVGAME_API_H #define SVGAME_API_H -#define INTERFACE_VERSION 138 - -typedef enum -{ - at_console = 1, // format: [msg] - at_warning, // format: Warning: [msg] - at_error, // format: Error: [msg] - at_loading, // print messages during loading - at_aiconsole, // same as at_console, but only shown if developer level is 5! - at_logged // server print to console ( only in multiplayer games ). -} ALERT_TYPE; - -// Client_Printf modes -enum -{ - print_console = 0, - print_center, - print_chat -}; - enum { WALKMOVE_NORMAL = 0, @@ -54,6 +34,49 @@ typedef struct edict_t *pHit; // entity the surface is on } TraceResult; +typedef struct globalvars_s +{ + float time; + float frametime; + string_t mapname; + string_t startspot; + vec3_t spotOffset; // landmark offset + + BOOL deathmatch; + BOOL coop; + BOOL teamplay; + + int serverflags; + int maxClients; + int numClients; // actual clients count + int maxEntities; + int numEntities; // actual ents count + + vec3_t v_forward; + vec3_t v_right; + vec3_t v_up; + + BOOL trace_allsolid; + BOOL trace_startsolid; + BOOL trace_startstuck; + float trace_fraction; + vec3_t trace_endpos; + vec3_t trace_plane_normal; + float trace_plane_dist; + int trace_start_contents; + int trace_contents; + int trace_hitgroup; + const char *trace_texture; // texture name that we hitting (brushes and studiomodels) + edict_t *trace_ent; + + int total_secrets; + int found_secrets; // number of secrets found + int total_monsters; + int killed_monsters; // number of monsters killed + + void *pSaveData; // savedata base offset +} globalvars_t; + // engine hands this to DLLs for functionality callbacks typedef struct enginefuncs_s { @@ -115,6 +138,7 @@ typedef struct enginefuncs_s void (*pfnWriteLong)( int iValue ); void (*pfnWriteAngle)( float flValue ); void (*pfnWriteCoord)( float flValue ); + void (*pfnWriteFloat)( float flValue ); void (*pfnWriteString)( const char *sz ); void (*pfnWriteEntity)( int iValue ); void (*pfnCVarRegister)( const char *name, const char *value, int flags, const char *desc ); @@ -148,7 +172,7 @@ typedef struct enginefuncs_s void (*pfnCRC_Init)( word *pulCRC ); void (*pfnCRC_ProcessBuffer)( word *pulCRC, void *p, int len ); word (*pfnCRC_Final)( word pulCRC ); - long (*pfnRandomLong)( long lLow, long lHigh ); + long (*pfnRandomLong)( long lLow, long lHigh ); float (*pfnRandomFloat)( float flLow, float flHigh ); void (*pfnSetView)( const edict_t *pClient, const edict_t *pViewent ); void (*pfnCrosshairAngle)( const edict_t *pClient, float pitch, float yaw ); @@ -317,8 +341,6 @@ typedef struct // returns string describing current .dll. E.g., TeamFotrress 2, Half-Life const char *(*pfnGetGameDescription)( void ); - // notify game .dll that engine is going to shut down. Allows mod authors to set a breakpoint. - void (*pfnHostError)( const char *error_string ); } DLL_FUNCTIONS; // TODO: create single func diff --git a/public/vsound_api.h b/public/vsound_api.h index 523bc0a0..b330f197 100644 --- a/public/vsound_api.h +++ b/public/vsound_api.h @@ -31,7 +31,7 @@ typedef struct vsound_exp_s void (*StartSound)( const vec3_t pos, int ent, int chan, sound_t sfx, float vol, float attn, float pitch, bool loop ); void (*StreamRawSamples)( int samples, int rate, int width, int channels, const byte *data ); bool (*AddLoopingSound)( int entnum, sound_t handle, float volume, float attn ); - bool (*StartLocalSound)( const char *name ); + bool (*StartLocalSound)( const char *name, float volume, const float *origin ); void (*StartBackgroundTrack)( const char *introTrack, const char *loopTrack ); void (*StopBackgroundTrack)( void ); diff --git a/release.bat b/release.bat index 9ee69be6..d2130fd3 100644 --- a/release.bat +++ b/release.bat @@ -11,6 +11,9 @@ call vcvars32 %MSDEV% baserc/baserc.dsp %CONFIG%"baserc - Win32 Release" %build_target% if errorlevel 1 set BUILD_ERROR=1 +%MSDEV% client/client.dsp %CONFIG%"client - Win32 Release" %build_target% +if errorlevel 1 set BUILD_ERROR=1 + %MSDEV% engine/engine.dsp %CONFIG%"engine - Win32 Release" %build_target% if errorlevel 1 set BUILD_ERROR=1 @@ -26,7 +29,7 @@ if errorlevel 1 set BUILD_ERROR=1 %MSDEV% render/render.dsp %CONFIG%"render - Win32 Release" %build_target% if errorlevel 1 set BUILD_ERROR=1 -%MSDEV% sv_dll/server.dsp %CONFIG%"server - Win32 Release" %build_target% +%MSDEV% server/server.dsp %CONFIG%"server - Win32 Release" %build_target% if errorlevel 1 set BUILD_ERROR=1 %MSDEV% vprogs/vprogs.dsp %CONFIG%"vprogs - Win32 Release" %build_target% @@ -60,7 +63,7 @@ if exist launch\launch.plg del /f /q launch\launch.plg if exist common\common.plg del /f /q common\common.plg if exist physic\physic.plg del /f /q physic\physic.plg if exist render\render.plg del /f /q render\render.plg -if exist sv_dll\server.plg del /f /q sv_dll\server.plg +if exist server\server.plg del /f /q server\server.plg if exist vprogs\vprogs.plg del /f /q vprogs\vprogs.plg if exist vsound\vsound.plg del /f /q vsound\vsound.plg diff --git a/sv_dll/cbase.h b/server/cbase.h similarity index 100% rename from sv_dll/cbase.h rename to server/cbase.h diff --git a/server/ents/basebeams.h b/server/ents/basebeams.h new file mode 100644 index 00000000..b64903fb --- /dev/null +++ b/server/ents/basebeams.h @@ -0,0 +1,106 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#ifndef BASEBEAMS_H +#define BASEBEAMS_H + +#define SF_BEAM_TRIPPED 0x80000 //bit who indicated "trip" action + +class CBeam : public CBaseLogic +{ +public: + void Spawn( void ); + void Precache( void ); + int ObjectCaps( void ) { return (CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); } + void Touch( CBaseEntity *pOther ); + + // These functions are here to show the way beams are encoded as entities. + // Encoding beams as entities simplifies their management in the client/server architecture + inline void SetType( int type ) { pev->rendermode = (pev->rendermode & 0xF0) | (type&0x0F); } + inline void SetFlags( int flags ) { pev->rendermode = (pev->rendermode & 0x0F) | (flags&0xF0); } + inline void SetStartPos( const Vector& pos ) { pev->origin = pos; } + inline void SetEndPos( const Vector& pos ) { pev->angles = pos; } + void SetStartEntity( int entityIndex ); + void SetEndEntity( int entityIndex ); + + inline void SetStartAttachment( int attachment ) { pev->sequence = (pev->sequence & 0x0FFF) | ((attachment&0xF)<<12); } + inline void SetEndAttachment( int attachment ) { pev->skin = (pev->skin & 0x0FFF) | ((attachment&0xF)<<12); } + + inline void SetTexture( int spriteIndex ) { pev->modelindex = spriteIndex; } + inline void SetWidth( int width ) { pev->scale = width; } + inline void SetNoise( int amplitude ) { pev->body = amplitude; } + inline void SetColor( int r, int g, int b ) { pev->rendercolor.x = r; pev->rendercolor.y = g; pev->rendercolor.z = b; } + inline void SetBrightness( int brightness ) { pev->renderamt = brightness; } + inline void SetFrame( float frame ) { pev->frame = frame; } + inline void SetScrollRate( int speed ) { pev->animtime = speed; } + + inline int GetType( void ) { return pev->rendermode & 0x0F; } + inline int GetFlags( void ) { return pev->rendermode & 0xF0; } + inline int GetStartEntity( void ) { return pev->sequence & 0xFFF; } + inline int GetEndEntity( void ) { return pev->skin & 0xFFF; } + + const Vector &GetStartPos( void ); + const Vector &GetEndPos( void ); + + Vector Center( void ) { return (GetStartPos() + GetEndPos()) * 0.5; }; // center point of beam + + inline int GetTexture( void ) { return pev->modelindex; } + inline int GetWidth( void ) { return pev->scale; } + inline int GetNoise( void ) { return pev->body; } + inline Vector GetColor( void ) { return Vector(pev->rendercolor.x, pev->rendercolor.y, pev->rendercolor.z ); } + inline int GetBrightness( void ) { return pev->renderamt; } + inline int GetFrame( void ) { return pev->frame; } + inline int GetScrollRate( void ) { return pev->animtime; } + + CBaseEntity* GetTripEntity( TraceResult *ptr ); + + // Call after you change start/end positions + void RelinkBeam( void ); + void SetObjectCollisionBox( void ); + + void DoSparks( const Vector &start, const Vector &end ); + CBaseEntity *RandomTargetname( const char *szName ); + void BeamDamage( TraceResult *ptr ); + // Init after BeamCreate() + void BeamInit( const char *pSpriteName, int width ); + void PointsInit( const Vector &start, const Vector &end ); + void PointEntInit( const Vector &start, int endIndex ); + void EntsInit( int startIndex, int endIndex ); + void HoseInit( const Vector &start, const Vector &direction ); + + static CBeam *BeamCreate( const char *pSpriteName, int width ); + + inline void LiveForTime( float time ) { SetThink( Remove ); SetNextThink( time ); } + inline void BeamDamageInstant( TraceResult *ptr, float damage ) + { + pev->dmg = damage; + pev->dmgtime = gpGlobals->time - 1; + BeamDamage(ptr); + } +}; + +class CLaser : public CBeam +{ +public: + void Spawn( void ); + void PostSpawn( void ); + void Precache( void ); + void KeyValue( KeyValueData *pkvd ); + void Activate( void ); + void SetObjectCollisionBox( void ); + void TurnOn( void ); + void TurnOff( void ); + void FireAtPoint( Vector startpos, TraceResult &point ); + + void Think( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + CSprite *m_pStartSprite; + CSprite *m_pEndSprite; +}; + +#endif //BASEBEAMS_H \ No newline at end of file diff --git a/server/ents/basebrush.cpp b/server/ents/basebrush.cpp new file mode 100644 index 00000000..81a564ed --- /dev/null +++ b/server/ents/basebrush.cpp @@ -0,0 +1,846 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// basebreak.cpp - base for all brush +// entities. +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "sfx.h" +#include "cbase.h" +#include "decals.h" +#include "client.h" +#include "soundent.h" +#include "saverestore.h" +#include "gamerules.h" +#include "basebrush.h" +#include "defaults.h" +#include "player.h" + +//======================================================================= +// STATIC BREACABLE BRUSHES +//======================================================================= +#define DAMAGE_SOUND(x, y, z)EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, x[RANDOM_LONG(0, ARRAYSIZE(x)-1)], y, ATTN_NORM, 0, z) + +//spawn objects +const char *CBaseBrush::pSpawnObjects[] = +{ + NULL, // 0 + "item_battery", // 1 + "item_healthkit", // 2 + "weapon_9mmhandgun", // 3 + "ammo_9mmclip", // 4 + "weapon_9mmAR", // 5 + "ammo_9mmAR", // 6 + "ammo_m203", // 7 + "weapon_shotgun", // 8 + "ammo_buckshot", // 9 + "weapon_crossbow", // 10 + "ammo_crossbow", // 11 + "weapon_357", // 12 + "ammo_357", // 13 + "weapon_rpg", // 14 + "ammo_rpgclip", // 15 + "ammo_gaussclip", // 16 + "weapon_handgrenade", // 17 + "weapon_gauss", // 18 +}; + +//material sounds +const char *CBaseBrush::pSoundsWood[] = +{ + "materials/wood/wood1.wav", + "materials/wood/wood2.wav", + "materials/wood/wood3.wav", + "materials/wood/wood4.wav", +}; + +const char *CBaseBrush::pSoundsFlesh[] = +{ + "materials/flesh/flesh1.wav", + "materials/flesh/flesh2.wav", + "materials/flesh/flesh3.wav", + "materials/flesh/flesh4.wav", + "materials/flesh/flesh5.wav", + "materials/flesh/flesh6.wav", + "materials/flesh/flesh7.wav", + "materials/flesh/flesh8.wav", +}; + +const char *CBaseBrush::pSoundsMetal[] = +{ + "materials/metal/metal1.wav", + "materials/metal/metal2.wav", + "materials/metal/metal3.wav", + "materials/metal/metal4.wav", + "materials/metal/metal5.wav", +}; + +const char *CBaseBrush::pSoundsCrete[] = +{ + "materials/crete/concrete1.wav", + "materials/crete/concrete2.wav", + "materials/crete/concrete3.wav", + "materials/crete/concrete4.wav", + "materials/crete/concrete5.wav", + "materials/crete/concrete6.wav", + "materials/crete/concrete7.wav", + "materials/crete/concrete8.wav", +}; + +const char *CBaseBrush::pSoundsGlass[] = +{ + "materials/glass/glass1.wav", + "materials/glass/glass2.wav", + "materials/glass/glass3.wav", + "materials/glass/glass4.wav", + "materials/glass/glass5.wav", + "materials/glass/glass6.wav", + "materials/glass/glass7.wav", + "materials/glass/glass8.wav", +}; + +const char *CBaseBrush::pSoundsCeil[] = +{ + "materials/ceil/ceiling1.wav", + "materials/ceil/ceiling2.wav", + "materials/ceil/ceiling3.wav", + "materials/ceil/ceiling4.wav", +}; + +const char **CBaseBrush::MaterialSoundList( Materials precacheMaterial, int &soundCount ) +{ + const char **pSoundList = NULL; + + switch ( precacheMaterial ) + { + case None: + soundCount = 0; + break; + case Bones: + case Flesh: + pSoundList = pSoundsFlesh; + soundCount = ARRAYSIZE(pSoundsFlesh); + break; + case CinderBlock: + case Concrete: + case Rocks: + pSoundList = pSoundsCrete; + soundCount = ARRAYSIZE(pSoundsCrete); + break; + case Glass: + case Computer: + case UnbreakableGlass: + pSoundList = pSoundsGlass; + soundCount = ARRAYSIZE(pSoundsGlass); + break; + case MetalPlate: + case AirDuct: + case Metal: + pSoundList = pSoundsMetal; + soundCount = ARRAYSIZE(pSoundsMetal); + break; + case CeilingTile: + pSoundList = pSoundsCeil; + soundCount = ARRAYSIZE(pSoundsCeil); + break; + case Wood: + pSoundList = pSoundsWood; + soundCount = ARRAYSIZE(pSoundsWood); + break; + default: + soundCount = 0; + break; + } + return pSoundList; +} + +void CBaseBrush::MaterialSoundPrecache( Materials precacheMaterial ) +{ + const char **pSoundList; + int i, soundCount = 0; + + pSoundList = MaterialSoundList( precacheMaterial, soundCount ); + for ( i = 0; i < soundCount; i++ ) UTIL_PrecacheSound( (char *)pSoundList[i] ); +} + +void CBaseBrush::PlayRandomSound( edict_t *pEdict, Materials soundMaterial, float volume ) +{ + const char **pSoundList; + int soundCount = 0; + + pSoundList = MaterialSoundList( soundMaterial, soundCount ); + if ( soundCount ) EMIT_SOUND( pEdict, CHAN_BODY, pSoundList[ RANDOM_LONG(0, soundCount-1) ], volume, 1.0 ); +} + +void CBaseBrush :: AxisDir( void ) +{ + //make backward compatibility + if ( pev->movedir != g_vecZero) return; + + //Don't change this! + if ( FBitSet(pev->spawnflags, SF_BRUSH_ROTATE_Z_AXIS)) + pev->movedir = Vector ( 0, 0, 1 ); // around z-axis + else if ( FBitSet(pev->spawnflags, SF_BRUSH_ROTATE_X_AXIS)) + pev->movedir = Vector ( 1, 0, 0 ); // around x-axis + else pev->movedir = Vector ( 0, 1, 0 ); // around y-axis +} + +void CBaseBrush::KeyValue( KeyValueData* pkvd ) +{ + //as default all brushes can select material + //and set strength (0 = unbreakable) + if (FStrEq(pkvd->szKeyName, "material")) + { + int i = atoi( pkvd->szValue); + + if ((i < 0) || (i >= LastMaterial)) + m_Material = Wood; + else m_Material = (Materials)i; + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "strength") ) + { + pev->health = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "spawnobject") ) + { + int namelen = strlen(pkvd->szValue) - 1; + int obj = atoi( pkvd->szValue ); + + //custom spawn object + if(namelen > 2) m_iSpawnObject = ALLOC_STRING( pkvd->szValue ); + else if ( obj > 0 && obj < ARRAYSIZE(pSpawnObjects)) + m_iSpawnObject = MAKE_STRING( pSpawnObjects[obj] ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "gibmodel")) + { + m_iGibModel = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "volume")) + { + //0.0 - silence, 1.0 - loudest( obsolete ) + m_flVolume = atof(pkvd->szValue); + if (m_flVolume > 1.0) m_flVolume = 1.0; + if (m_flVolume < 0.0) m_flVolume = 0.0; + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "movesound") || FStrEq(pkvd->szKeyName, "movesnd")) + { + m_iMoveSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "stopsound") || FStrEq(pkvd->szKeyName, "stopsnd")) + { + m_iStopSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "contents")) + { + pev->skin = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +//Base functions +TYPEDESCRIPTION CBaseBrush::m_SaveData[] = +{ + DEFINE_FIELD( CBaseBrush, m_flVolume, FIELD_FLOAT ), //volume of sounds + DEFINE_FIELD( CBaseBrush, m_pitch, FIELD_FLOAT ), //pitch of sound + DEFINE_FIELD( CBaseBrush, m_Material, FIELD_INTEGER ), //brush material + DEFINE_FIELD( CBaseBrush, m_iMagnitude, FIELD_INTEGER ), //explosion magnitude + DEFINE_FIELD( CBaseBrush, m_iMoveSound, FIELD_STRING ), //sound scheme like Quake + DEFINE_FIELD( CBaseBrush, m_iStartSound, FIELD_STRING ), //sound scheme like Quake + DEFINE_FIELD( CBaseBrush, m_iStopSound, FIELD_STRING ), //sound scheme like Quake + DEFINE_FIELD( CBaseBrush, m_iSpawnObject, FIELD_STRING ), //spawnobject index + DEFINE_FIELD( CBaseBrush, m_iGibModel, FIELD_STRING ), //custom gibname + DEFINE_FIELD( CBaseBrush, m_vecPlayerPos, FIELD_VECTOR ), //for controllable entity like tank + DEFINE_FIELD( CBaseBrush, m_pController, FIELD_CLASSPTR ), //for controllable entity like tank +}; +IMPLEMENT_SAVERESTORE( CBaseBrush, CBaseLogic ); + +void CBaseBrush::Spawn( void ) +{ + Precache(); + + if (!m_flVolume)m_flVolume = 1.0;//just enable full volume + + //breacable brush (if mapmaker just set material - just play material sound) + if(IsBreakable()) + pev->takedamage = DAMAGE_YES; + else pev->takedamage = DAMAGE_NO; + + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + if (!m_pParent)pev->flags |= FL_WORLDBRUSH; +} + +void CBaseBrush::Precache( void ) +{ + const char *pGibName = ""; + + switch (m_Material) + { + case Bones: + pGibName = "models/gibs/bones.mdl"; + break; + case Flesh: + pGibName = "models/gibs/flesh.mdl"; + UTIL_PrecacheSound("materials/flesh/bustflesh1.wav"); + UTIL_PrecacheSound("materials/flesh/bustflesh2.wav"); + break; + case Concrete: + pGibName = "models/gibs/concrete.mdl"; + UTIL_PrecacheSound("materials/crete/bustcrete1.wav"); + UTIL_PrecacheSound("materials/crete/bustcrete2.wav"); + break; + case Rocks: + pGibName = "models/gibs/rock.mdl"; + UTIL_PrecacheSound("materials/crete/bustcrete1.wav"); + UTIL_PrecacheSound("materials/crete/bustcrete2.wav"); + break; + case CinderBlock: + pGibName = "models/gibs/cinder.mdl"; + UTIL_PrecacheSound("materials/crete/bustcrete1.wav"); + UTIL_PrecacheSound("materials/crete/bustcrete2.wav"); + break; + case Computer: + pGibName = "models/gibs/computer.mdl"; + UTIL_PrecacheSound("materials/metal/bustmetal1.wav"); + UTIL_PrecacheSound("materials/metal/bustmetal2.wav"); + break; + case Glass: + case UnbreakableGlass: + pGibName = "models/gibs/glass.mdl"; + UTIL_PrecacheSound("materials/glass/bustglass1.wav"); + UTIL_PrecacheSound("materials/glass/bustglass2.wav"); + break; + case MetalPlate: + pGibName = "models/gibs/metalplate.mdl"; + UTIL_PrecacheSound("materials/metal/bustmetal1.wav"); + UTIL_PrecacheSound("materials/metal/bustmetal2.wav"); + break; + case Metal: + pGibName = "models/gibs/metal.mdl"; + UTIL_PrecacheSound("materials/metal/bustmetal1.wav"); + UTIL_PrecacheSound("materials/metal/bustmetal2.wav"); + break; + case AirDuct: + pGibName = "models/gibs/vent.mdl"; + UTIL_PrecacheSound("materials/metal/bustmetal1.wav"); + UTIL_PrecacheSound("materials/metal/bustmetal2.wav"); + break; + case CeilingTile: + pGibName = "models/gibs/ceiling.mdl"; + UTIL_PrecacheSound("materials/ceil/bustceiling1.wav"); + UTIL_PrecacheSound("materials/ceil/bustceiling2.wav"); + break; + case Wood: + pGibName = "models/gibs/wood.mdl"; + UTIL_PrecacheSound("materials/wood/bustcrate1.wav"); + UTIL_PrecacheSound("materials/wood/bustcrate2.wav"); + break; + case None: + default: + if(pev->health > 0)//mapmaker forget set material ? + { + DevMsg("\n======/Xash SmartFiled System/======\n\n"); + DevMsg("Please set material for %s,\n", STRING(pev->classname)); + DevMsg("if we want make this breakable\n"); + } + break; + } + + MaterialSoundPrecache( m_Material ); + if(IsBreakable()) + { + m_idShard = UTIL_PrecacheModel( m_iGibModel, (char*)pGibName );//precache model + if(m_iSpawnObject)UTIL_PrecacheEntity( m_iSpawnObject ); + } + UTIL_PrecacheModel( pev->model );//can use *.mdl for any brush +} + +void CBaseBrush :: DamageSound( void ) +{ + float fvol; + int pitch; + + if (RANDOM_LONG(0,2)) pitch = PITCH_NORM; + else pitch = 95 + RANDOM_LONG(0,34); + + fvol = RANDOM_FLOAT(m_flVolume/1.5, m_flVolume); + + switch (m_Material) + { + case None: break; + case Bones: + case Flesh: + DAMAGE_SOUND(pSoundsFlesh, fvol, pitch ); + break; + case CinderBlock: + case Concrete: + case Rocks: + DAMAGE_SOUND(pSoundsCrete, fvol, pitch ); + break; + case Computer: + case Glass: + case UnbreakableGlass: + DAMAGE_SOUND(pSoundsGlass, fvol, pitch ); + break; + case MetalPlate: + case AirDuct: + case Metal: + DAMAGE_SOUND(pSoundsMetal, fvol, pitch ); + break; + case CeilingTile: + DAMAGE_SOUND(pSoundsCeil, fvol, pitch ); + break; + case Wood: + DAMAGE_SOUND(pSoundsWood, fvol, pitch ); + break; + default: break; + } +} + +void CBaseBrush::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType ) +{ + switch( m_Material )//apply effects for some materials (may be extended in future) + { + case Computer: + { + if(RANDOM_LONG(0,1))UTIL_Sparks( ptr->vecEndPos ); + + float flVolume = RANDOM_FLOAT ( 0.7 , 1.0 );//random volume range + switch ( RANDOM_LONG(0,1) ) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark5.wav", flVolume, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark6.wav", flVolume, ATTN_NORM); break; + } + } + break; + case Glass: + { + if(pev->health == 0)//unbreakable glass + { + if( RANDOM_LONG(0,1))UTIL_Ricochet( ptr->vecEndPos, RANDOM_FLOAT(0.5,1.5) ); + UTIL_DecalTrace(ptr, DECAL_BPROOF1); + } + else UTIL_DecalTrace(ptr, DECAL_GLASSBREAK1 + RANDOM_LONG(0, 2));//breakable glass + } + break; + case UnbreakableGlass: + UTIL_Ricochet( ptr->vecEndPos, RANDOM_FLOAT(0.5,1.5) ); + break; + } + CBaseLogic::TraceAttack( pevAttacker, flDamage, vecDir, ptr, bitsDamageType ); +} + +int CBaseBrush :: TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ) +{ + Vector vecTemp; + + if ( pevAttacker == pevInflictor ) + { + vecTemp = pevInflictor->origin - ( pev->absmin + ( pev->size * 0.5 ) ); + if((pevAttacker->flags & FL_CLIENT) && (pev->spawnflags & SF_BREAK_CROWBAR) && (bitsDamageType & DMG_CLUB)) + flDamage = pev->health; //old valve flag + } + else vecTemp = pevInflictor->origin - ( pev->absmin + ( pev->size * 0.5 ) ); + + if (!IsBreakable()) + { + DamageSound(); + return 0; + } + // Breakables take double damage from the crowbar + if ( bitsDamageType & DMG_CLUB ) flDamage *= 1.2; + else if( bitsDamageType & DMG_CRUSH ) flDamage *= 2; + else if( bitsDamageType & DMG_BLAST ) flDamage *= 3; + else if( bitsDamageType & DMG_BULLET && m_Material == Glass )flDamage *= 0.5; + else if( bitsDamageType & DMG_BULLET && m_Material == Wood )flDamage *= 0.7; + else flDamage = 0; //don't give any other damage + + DmgType = bitsDamageType;//member damage type + + g_vecAttackDir = vecTemp.Normalize(); + pev->health -= flDamage; + + if (pev->health <= 0) + { + Killed( pevAttacker, GIB_NORMAL ); + Die(); + return 0; + } + DamageSound(); + + return 1; +} + +void CBaseBrush :: Blocked( CBaseEntity *pOther ) +{ + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE)//tell parent + m_pParent->Blocked( pOther ); + + if(!pOther->IsPlayer() && !pOther->IsMonster())//crash breakable + { + if(IsBreakable())TakeDamage( pev, pev, 5, DMG_CRUSH ); + } +} + +BOOL CBaseBrush :: IsBreakable( void ) +{ + return (pev->health > 0 && m_Material != UnbreakableGlass); +} + +void CBaseBrush::Die( void ) +{ + Vector vecSpot, vecVelocity; + char cFlag = 0; + int pitch, soundbits = NULL; + float fvol; + + pitch = 95 + RANDOM_LONG(0, 29); + if(pitch > 97 && pitch < 103)pitch = 100; + + fvol = RANDOM_FLOAT(0.85, 1.0) + (abs(pev->health) / 100.0); + if(fvol > 1.0)fvol = 1.0; + + switch (m_Material) + { + case None: break; //just in case + case Bones: + case Flesh: + if(RANDOM_LONG(0,1) ) + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/flesh/bustflesh1.wav", fvol, ATTN_NORM, 0, pitch); + else EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/flesh/bustflesh2.wav", fvol, ATTN_NORM, 0, pitch); + cFlag = BREAK_FLESH; + soundbits = bits_SOUND_MEAT; + break; + case CinderBlock: + case Concrete: + case Rocks: + if(RANDOM_LONG(0,1) ) + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/crete/bustcrete1.wav", fvol, ATTN_NORM, 0, pitch); + else EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/crete/bustcrete2.wav", fvol, ATTN_NORM, 0, pitch); + cFlag = BREAK_CONCRETE; + soundbits = bits_SOUND_WORLD; + break; + case Computer: + case Metal: + case AirDuct: + case MetalPlate: + if(RANDOM_LONG(0,1) ) + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/metal/bustmetal1.wav", fvol, ATTN_NORM, 0, pitch); + else EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/metal/bustmetal2.wav", fvol, ATTN_NORM, 0, pitch); + cFlag = BREAK_METAL; + soundbits = bits_SOUND_WORLD; + break; + case Glass: + if( RANDOM_LONG(0,1) ) + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/glass/bustglass1.wav", fvol, ATTN_NORM, 0, pitch); + else EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/glass/bustglass2.wav", fvol, ATTN_NORM, 0, pitch); + cFlag = BREAK_GLASS; + soundbits = bits_SOUND_WORLD; + break; + + case Wood: + if( RANDOM_LONG(0,1) ) + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/wood/bustcrate1.wav", fvol, ATTN_NORM, 0, pitch); + else EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/wood/bustcrate2.wav", fvol, ATTN_NORM, 0, pitch); + cFlag = BREAK_WOOD; + soundbits = bits_SOUND_WORLD; + break; + case CeilingTile: + if( RANDOM_LONG(0,1) ) + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/ceil/bustceiling1.wav",fvol, ATTN_NORM, 0, pitch); + else EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "materials/ceil/bustceiling1.wav",fvol, ATTN_NORM, 0, pitch); + cFlag = BREAK_CONCRETE; + soundbits = bits_SOUND_GARBAGE; + break; + } + + if (DmgType & DMG_CLUB)//direction from crowbar + vecVelocity = g_vecAttackDir * clamp(pev->dmg * -10, -1024, 1024); + else vecVelocity = UTIL_RandomVector() * clamp(pev->dmg * 10, 1, 240); + + vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + float time = RANDOM_FLOAT(25, 100); + + if(soundbits) CSoundEnt::InsertSound(soundbits, pev->origin, 128, 0.5);//make noise for ai + SFX_MakeGibs( m_idShard, vecSpot, pev->size, vecVelocity, time, cFlag); + + //what the hell does this ? + UTIL_FindBreakable( this ); + + // If I'm getting removed, don't fire something that could fire myself + pev->targetname = 0; + + pev->solid = SOLID_NOT; + UTIL_FireTargets( pev->target, NULL, NULL, USE_TOGGLE ); + pev->target = 0; + Msg("Fire!\n"); + SetThink( Remove ); + SetNextThink( 0.1 ); + + //pev->effects |= EF_NODRAW; + //pev->takedamage = DAMAGE_NO; + + //make explosion + if(m_iMagnitude) UTIL_Explode( Center(), edict(), m_iMagnitude ); + if(m_iSpawnObject) CBaseEntity::Create( (char *)STRING(m_iSpawnObject), Center(), pev->angles, edict() ); + + // Fire targets on break + + //UTIL_Remove( this ); +} + +int CBaseBrush :: DamageDecal( int bitsDamageType ) +{ + if ( m_Material == Glass ) + return DECAL_GLASSBREAK1 + RANDOM_LONG(0,2); + if ( m_Material == Glass && pev->health <= 0) + return DECAL_BPROOF1; + if ( m_Material == UnbreakableGlass ) + return DECAL_BPROOF1; + if ( m_Material == Wood ) + return DECAL_GUNSHOT1 + RANDOM_LONG(0,4); + if ( m_Material == Computer ) + return DECAL_BIGSHOT1 + RANDOM_LONG(0,4); + return CBaseEntity::DamageDecal( bitsDamageType ); +} + +//======================================================================= +// moving breakable brushes +//======================================================================= +//material moving sounds +const char *CPushable::pPushWood[] = +{ + "materials/wood/pushwood1.wav", + "materials/wood/pushwood2.wav", + "materials/wood/pushwood3.wav", +}; + +const char *CPushable::pPushFlesh[] = +{ + "materials/flesh/pushflesh1.wav", + "materials/flesh/pushflesh2.wav", + "materials/flesh/pushflesh3.wav", +}; +const char *CPushable::pPushMetal[] = +{ + "materials/metal/pushmetal1.wav", + "materials/metal/pushmetal2.wav", + "materials/metal/pushmetal3.wav", +}; +const char *CPushable::pPushCrete[] = +{ + "materials/crete/pushstone1.wav", + "materials/crete/pushstone2.wav", + "materials/crete/pushstone3.wav", +}; +const char *CPushable::pPushGlass[] = +{ + "materials/glass/pushglass1.wav", + "materials/glass/pushglass2.wav", + "materials/glass/pushglass3.wav", +}; +const char *CPushable::pPushCeil[] = +{ + "materials/ceil/ceiling1.wav", + "materials/ceil/ceiling2.wav", + "materials/ceil/ceiling3.wav", +}; + +void CPushable :: Spawn( void ) +{ + Precache(); + + pev->movetype = MOVETYPE_PUSHSTEP; + pev->solid = SOLID_BBOX; + UTIL_SetModel( ENT(pev), pev->model ); + UTIL_SetSize( pev, pev->absmin, pev->absmax ); + + if (!m_flVolume)m_flVolume = 1.0;//just enable full volume + + //breacable brush (if mapmaker just set material - just play material sound) + if(m_Material != None) + pev->takedamage = DAMAGE_YES; + else pev->takedamage = DAMAGE_NO; + + pev->speed = MatFrictionTable( m_Material ); + SetBits( pev->flags, FL_FLOAT ); + pev->origin.z += 1; // Pick up off of the floor + UTIL_SetOrigin( this, pev->origin ); + + // Multiply by area of the box's cross-section (assume 1000 units^3 standard volume) + pev->skin = MatByoancyTable( pev, m_Material ); + pev->frags = 0; +} + +void CPushable :: Precache( void ) +{ + CBaseBrush::Precache(); + PushSoundPrecache( m_Material ); +} + +const char **CPushable::PushSoundList( Materials precacheMaterial, int &soundCount ) +{ + const char **pSoundList = NULL; + + switch ( precacheMaterial ) + { + case None: + soundCount = 0; + break; + case Bones: + case Flesh: + pSoundList = pPushFlesh; + soundCount = ARRAYSIZE(pPushFlesh); + break; + case CinderBlock: + case Concrete: + case Rocks: + pSoundList = pPushCrete; + soundCount = ARRAYSIZE(pPushCrete); + break; + case Computer: + case Glass: + pSoundList = pPushGlass; + soundCount = ARRAYSIZE(pPushGlass); + break; + case MetalPlate: + case AirDuct: + case Metal: + pSoundList = pPushMetal; + soundCount = ARRAYSIZE(pPushMetal); + break; + case CeilingTile: + pSoundList = pPushCeil; + soundCount = ARRAYSIZE(pPushCeil); + break; + case Wood: + pSoundList = pPushWood; + soundCount = ARRAYSIZE(pPushWood); + break; + default: + soundCount = 0; + break; + } + return pSoundList; +} + +void CPushable::PushSoundPrecache( Materials precacheMaterial ) +{ + const char **pPushList; + int i, soundCount = 0; + + pPushList = PushSoundList( precacheMaterial, soundCount ); + for ( i = 0; i < soundCount; i++ ) UTIL_PrecacheSound( (char *)pPushList[i] ); +} + +int CPushable::PlayPushSound( edict_t *pEdict, Materials soundMaterial, float volume ) +{ + const char **pPushList; + int soundCount = 0; + int m_lastPushSnd = 0; + + pPushList = PushSoundList( soundMaterial, soundCount ); + if ( soundCount ) + { + m_lastPushSnd = RANDOM_LONG(0, soundCount-1); + EMIT_SOUND( pEdict, CHAN_WEAPON, pPushList[ m_lastPushSnd ], volume, 1.0 ); + } + return m_lastPushSnd; +} + +void CPushable::StopPushSound( edict_t *pEdict, Materials soundMaterial, int m_lastPushSnd ) +{ + const char **pPushList; + int soundCount = 0; + + pPushList = PushSoundList( soundMaterial, soundCount ); + if ( soundCount ) STOP_SOUND( pEdict, CHAN_WEAPON, pPushList[ m_lastPushSnd ] ); +} + +void CPushable :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(useType == USE_SHOWINFO) + { + DEBUGHEAD; + } + else if ( pActivator && pActivator->IsPlayer() && pActivator->pev->velocity != g_vecZero ) + Move( pActivator, 0 ); +} + +void CPushable :: Touch( CBaseEntity *pOther ) +{ + if ( pOther == g_pWorld ) return; + Move( pOther, 1 ); +} + +void CPushable :: Move( CBaseEntity *pOther, int push ) +{ + entvars_t* pevToucher = pOther->pev; + int playerTouch = 0; + + // Is entity standing on this pushable ? + if ( FBitSet(pevToucher->flags, FL_ONGROUND) && pevToucher->groundentity && VARS(pevToucher->groundentity) == pev ) + { + // Only push if floating + if ( pev->waterlevel > 0 && pev->watertype & MASK_WATER ) + pev->velocity.z += pevToucher->velocity.z * 0.1; + return; + } + + + if ( pOther->IsPlayer() ) + { + // Don't push unless the player is pushing forward and NOT use (pull) + if ( push && !(pevToucher->button & (IN_FORWARD|IN_USE)) ) return; + playerTouch = 1; + } + + float factor; + + if ( playerTouch ) + { + // Don't push away from jumping/falling players unless in water + if( !(pevToucher->flags & FL_ONGROUND) ) + { + if( pev->waterlevel < 1 || !(pev->watertype & MASK_WATER) ) + return; + else factor = 0.1; + } + else factor = 1; + } + else factor = 0.25; + + if (!push) factor = factor*0.5; + + pev->velocity.x += pevToucher->velocity.x * factor; + pev->velocity.y += pevToucher->velocity.y * factor; + + float length = sqrt( pev->velocity.x * pev->velocity.x + pev->velocity.y * pev->velocity.y ); + if ( push && (length > MaxSpeed()) ) + { + pev->velocity.x = (pev->velocity.x * MaxSpeed() / length ); + pev->velocity.y = (pev->velocity.y * MaxSpeed() / length ); + } + if ( playerTouch ) + { + pevToucher->velocity.x = pev->velocity.x; + pevToucher->velocity.y = pev->velocity.y; + if ( (gpGlobals->time - pev->frags) > 0.7 ) + { + pev->frags = gpGlobals->time; + if ( length > 0 && FBitSet(pev->flags, FL_ONGROUND) ) + m_lastSound = PlayPushSound( edict(), m_Material, m_flVolume ); + else StopPushSound( edict(), m_Material, m_lastSound ); + } + } +} +LINK_ENTITY_TO_CLASS( func_pushable, CPushable ); \ No newline at end of file diff --git a/server/ents/basebrush.h b/server/ents/basebrush.h new file mode 100644 index 00000000..7e4513a2 --- /dev/null +++ b/server/ents/basebrush.h @@ -0,0 +1,207 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// basebrush.h - base for all brush +// entities. +//======================================================================= +#ifndef BASEBRUSH_H +#define BASEBRUSH_H + +//ANY BRUSH MAY BE SET MATERIAL +typedef enum { + Glass = 0, //glass.mdl + Wood, //wood.mdl + Metal, //metal.mdl + Flesh, //flesh.mdl + CinderBlock, //cinder.mdl + CeilingTile, //ceiling.mdl + Computer, //computer.mdl + UnbreakableGlass, //galss.mdl + Rocks, //rock.mdl + Bones, //bones.mdl + Concrete, //concrete.mdl + MetalPlate, //metalplate.mdl + AirDuct, //vent.mdl + None, //very strange pos + LastMaterial, //just in case +}Materials; + +//gibs physics +typedef enum { + Bounce = 0, // bounce at collision + Noclip, // no collisions + Sticky, // sticky physic + Fly, // fly + Toss, // toss + WalkStep, // monsters walk +} Physics; + +static float MatFrictionTable( Materials mat) +{ + float friction = 0; + + switch(mat) + { + case CinderBlock: + case Concrete: + case Rocks: friction = 300; break; + case Glass: friction = 200; break; + case MetalPlate: + case Metal: friction = 60; break; + case Wood: friction = 250; break; + case None: + default: friction = 100; break; + } + return friction; +} + +static float MatByoancyTable( entvars_t *pev, Materials mat) +{ + float byoancy = 0; + + switch(mat) + { + case CinderBlock: + case Concrete: + case Rocks: byoancy = 0; break; + case Glass: byoancy = 5; break; + case MetalPlate: + case Metal: byoancy = 1; break; + case Wood: byoancy = 30; break; + case None: + default: byoancy = 100; break; + } + return ( byoancy * (pev->maxs.x - pev->mins.x) * (pev->maxs.y - pev->mins.y) ) * 0.0005; +} + +static float MatMassTable( entvars_t *pev, Materials mat) +{ + float mass = 0; + + switch(mat) + { + case CinderBlock: + case Concrete: + case Rocks: mass = 2; break; + case Glass: mass = 1.2; break; + case MetalPlate: + case Metal: mass = 1.5; break; + case Wood: mass = 0.8; break; + case None: + default: mass = 1; break; + } + return (pev->size.Length() * mass); +} + +static float MatVolume( Materials mat ) +{ + float volume = 0; + switch(mat) + { + case None: volume = 0.9; //unbreakable + case Bones: //bones.mdl + case Flesh: volume = 0.2; break; //flesh.mdl + case CinderBlock: //cinder.mdl + case Concrete: //concrete.mdl + case Rocks: volume = 0.25; break; //rock.mdl + case Computer: //computer.mdl + case Glass: volume = 0.3; break; //glass.mdl + case MetalPlate: //metalplate.mdl + case Metal: //metal.mdl + case AirDuct: volume = 0.1; break; //vent.mdl + case CeilingTile: //ceiling.mdl + case Wood: volume = 0.15; break; //wood.mdl + default: volume = 0.2; break; + } + + return volume; +} + +extern DLL_GLOBAL Vector g_vecAttackDir; +#define SetMoveDone( a ) m_pfnCallWhenMoveDone = static_cast (a) + +class CBaseBrush : public CBaseLogic +{ +public: + void Spawn( void ); + void Precache( void ); + void KeyValue( KeyValueData* pkvd); + + void Blocked( CBaseEntity *pOther ); + virtual void AxisDir( void ); + virtual int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ); + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType ); + int DamageDecal( int bitsDamageType ); + + BOOL IsBreakable( void ); + + void EXPORT Die( void ); + virtual CBaseBrush *MyBrushPointer( void ) { return this; } + virtual int ObjectCaps( void ) { return (CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); } + static TYPEDESCRIPTION m_SaveData[]; + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static void MaterialSoundPrecache( Materials precacheMaterial ); + static void PlayRandomSound( edict_t *pEdict, Materials soundMaterial, float volume ); + static const char **MaterialSoundList( Materials precacheMaterial, int &soundCount ); + + static const char *pSoundsWood[]; + static const char *pSoundsFlesh[]; + static const char *pSoundsMetal[]; + static const char *pSoundsCrete[]; + static const char *pSoundsGlass[]; + static const char *pSoundsCeil[]; + + //spawnobject name + static const char *pSpawnObjects[]; + + void DamageSound( void ); + + Materials m_Material; + CBasePlayer* m_pController; //player pointer + Vector m_vecPlayerPos; //player position + int m_iMoveSound; //move sound or preset + int m_iStartSound; //start sound or preset + int m_iStopSound; //stop sound or preset + int m_idShard; //index of gibs + int m_iMagnitude; //explosion magnitude + int m_iSpawnObject; //spawnobject name + int m_iGibModel; //custom gib model + int DmgType; //temp container for right calculate damage + float m_flVolume; //moving brushes has volume of move sound + float m_flBlockedTime; //don't save this + int m_pitch; //sound pitch +}; + +class CPushable : public CBaseBrush +{ +public: + void Spawn ( void ); + void Precache( void ); + void Touch ( CBaseEntity *pOther ); + void Move( CBaseEntity *pMover, int push ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + virtual int ObjectCaps( void ) { return (CBaseBrush :: ObjectCaps() | FCAP_CONTINUOUS_USE); } + virtual BOOL IsPushable( void ) { return TRUE; } + + inline float MaxSpeed( void ) { return pev->speed; } + + static const char *pPushWood[]; + static const char *pPushFlesh[]; + static const char *pPushMetal[]; + static const char *pPushCrete[]; + static const char *pPushGlass[]; + static const char *pPushCeil[]; //fixme + + static void PushSoundPrecache( Materials precacheMaterial ); + static int PlayPushSound( edict_t *pEdict, Materials soundMaterial, float volume ); + static void StopPushSound( edict_t *pEdict, Materials soundMaterial, int m_lastPushSnd ); + static const char **PushSoundList( Materials precacheMaterial, int &soundCount ); + + int m_lastSound; // no need to save/restore, just keeps the same sound from playing twice in a row +}; + +#include "basemover.h" + +#endif // BASEBRUSH_H diff --git a/server/ents/baseentity.cpp b/server/ents/baseentity.cpp new file mode 100644 index 00000000..a7f2742e --- /dev/null +++ b/server/ents/baseentity.cpp @@ -0,0 +1,959 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// baseentity.cpp - base class +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "saverestore.h" +#include "client.h" +#include "nodes.h" +#include "decals.h" +#include "gamerules.h" +#include "game.h" +#include "damage.h" +#include "defaults.h" +#include "bullets.h" + +extern Vector VecBModelOrigin( entvars_t* pevBModel ); +extern DLL_GLOBAL Vector g_vecAttackDir; +extern void SetObjectCollisionBox( entvars_t *pev ); +extern BOOL NewLevel; +extern CGraph WorldGraph; + +//======================================================================= +// decent mechanisms +//======================================================================= +void CBaseEntity::DontThink( void ) +{ + m_fNextThink = 0; + if (m_pParent == NULL && m_pChild == NULL) + { + pev->nextthink = 0; + m_fPevNextThink = 0; + } +} + +void CBaseEntity :: SetEternalThink( void ) +{ + if (pev->movetype == MOVETYPE_PUSH) + { + pev->nextthink = pev->ltime + 1E6; + m_fPevNextThink = pev->nextthink; + } + CBaseEntity *pChild; + for (pChild = m_pChild; pChild != NULL; pChild = pChild->m_pNextChild) + pChild->SetEternalThink( ); +} + +void CBaseEntity :: SetNextThink( float delay, BOOL correctSpeed ) +{ + if (m_pParent || m_pChild ) + { + if (pev->movetype == MOVETYPE_PUSH) + m_fNextThink = pev->ltime + delay; + else m_fNextThink = gpGlobals->time + delay; + + SetEternalThink( ); + UTIL_MarkChild( this, correctSpeed, FALSE ); + } + else + { + // set nextthink as normal. + if (pev->movetype == MOVETYPE_PUSH)pev->nextthink = pev->ltime + delay; + else pev->nextthink = gpGlobals->time + delay; + m_fPevNextThink = m_fNextThink = pev->nextthink; + } +} + +void CBaseEntity :: AbsoluteNextThink( float time, BOOL correctSpeed ) +{ + if (m_pParent || m_pChild) + { + m_fNextThink = time; + SetEternalThink( ); + UTIL_MarkChild( this, correctSpeed, FALSE ); + } + else + { + // set nextthink as normal. + pev->nextthink = time; + m_fPevNextThink = m_fNextThink = pev->nextthink; + } +} + +void CBaseEntity :: ThinkCorrection( void ) +{ + if (pev->nextthink != m_fPevNextThink) + { + m_fNextThink += pev->nextthink - m_fPevNextThink; + m_fPevNextThink = pev->nextthink; + } +} + +//======================================================================= +// set parent (void ) dinamically link parents +//======================================================================= +void CBaseEntity :: SetParent( int m_iNewParent, int m_iAttachment ) +{ + if(!m_iNewParent) //unlink entity from chain + { + ResetParent(); + return;//disable + } + + CBaseEntity* pParent; + + if(!m_iAttachment) //try to extract aiment from name + { + char *name = (char*)STRING(m_iNewParent); + for (char *c = name; *c; c++) + { + if (*c == '.') + { + m_iAttachment = atoi(c+1); + name[strlen(name)-2] = 0; + pParent = UTIL_FindEntityByTargetname( NULL, name); + SetParent( pParent, m_iAttachment); + return; + } + } + } + pParent = UTIL_FindEntityByTargetname( NULL, STRING(m_iNewParent)); + SetParent( pParent, m_iAttachment ); +} + +//======================================================================= +// set parent main function +//======================================================================= +void CBaseEntity :: SetParent( CBaseEntity* pParent, int m_iAttachment ) +{ + m_pParent = pParent; + + if(!m_pParent) + { + Msg("=========/Xash Parent System Info:/=========\n"); + if(pev->targetname) Msg("Warning! Not found parent for %s with name %s\n", STRING(pev->classname), STRING(pev->targetname) ); + else Msg("Warning! Not found parent for %s\n", STRING(pev->classname) ); + SHIFT; + ResetParent();//lose parent or not found parent + return; + } + + //check for himself parent + if(m_pParent == this) + { + ALERT(at_console, "=========/Xash Parent System Info:/=========\n"); + if(pev->targetname) Msg( "ERROR! %s with name %s has illegal parent\n", STRING(pev->classname), STRING(pev->targetname) ); + else Msg( "ERROR! %s has illegal parent\n", STRING(pev->classname) ); + SHIFT; + ResetParent();//clear parent + return; + } + + CBaseEntity *pCurChild = m_pParent->m_pChild; + while (pCurChild) //check that this entity isn't already in the list of children + { + if (pCurChild == this) break; + pCurChild = pCurChild->m_pNextChild; + } + if(!pCurChild) + { + m_pNextChild = m_pParent->m_pChild; // may be null: that's fine by me. + m_pParent->m_pChild = this; + + if(m_iAttachment) + { + if(pev->flags & FL_POINTENTITY || pev->flags & FL_MONSTER) + { + pev->skin = ENTINDEX(m_pParent->edict()); + pev->body = m_iAttachment; + pev->aiment = m_pParent->edict(); + pev->movetype = MOVETYPE_FOLLOW; + } + else //error + { + ALERT(at_console, "=========/Xash Parent System Info:/=========\n"); + if(pev->targetname) Msg("ERROR! %s with name %s not following with aiment %d!(yet)\n", STRING(pev->classname), STRING(pev->targetname), m_iAttachment ); + else Msg("ERROR! %s not following with aiment %d!(yet)\n", STRING(pev->classname), m_iAttachment ); + SHIFT; + } + return; + } + else//appllayed to origin + { + if (pev->movetype == MOVETYPE_NONE) + { + if (pev->solid == SOLID_BSP) + pev->movetype = MOVETYPE_PUSH; + else pev->movetype = MOVETYPE_NOCLIP; + SetBits (pFlags, PF_MOVENONE);//member movetype + } + if(m_pParent->pev->movetype == MOVETYPE_WALK)//parent is walking monster? + { + SetBits (pFlags, PF_POSTORG);//copy pos from parent every frame + pev->solid = SOLID_NOT;//set non solid + } + pParentOrigin = m_pParent->pev->origin; + pParentAngles = m_pParent->pev->angles; + } + + if (m_pParent->m_vecSpawnOffset != g_vecZero) + { + UTIL_AssignOrigin(this, pev->origin + m_pParent->m_vecSpawnOffset); + m_vecSpawnOffset = m_vecSpawnOffset + m_pParent->m_vecSpawnOffset; + } + OffsetOrigin = pev->origin - pParentOrigin; + OffsetAngles = pev->angles - pParentAngles; + + if((m_pParent->pFlags & PF_ANGULAR && OffsetOrigin != g_vecZero) || m_pParent->pev->flags & FL_POINTENTITY) + { + SetBits (pFlags, PF_POSTORG);//magic stuff + //GetPInfo( this ); + } + + if(g_serveractive)//maybe parent is moving ? + { + pev->velocity += m_pParent->pev->velocity; + pev->avelocity += m_pParent->pev->avelocity; + } + } +} + +//======================================================================= +// reset parent (void ) dinamically unlink parents +//======================================================================= +void CBaseEntity :: ResetParent( void ) +{ + if(pFlags & PF_MOVENONE)//this entity was static e.g. func_wall + { + ClearBits (pFlags, PF_MOVENONE); + pev->movetype = MOVETYPE_NONE; + } + + if ( !g_pWorld )return; //??? + + CBaseEntity* pTemp; + + for (pTemp = g_pWorld; pTemp->m_pLinkList != NULL; pTemp = pTemp->m_pLinkList) + { + if (this == pTemp->m_pLinkList) + { + pTemp->m_pLinkList = this->m_pLinkList;//save pointer + this->m_pLinkList = NULL; + break; + } + } + + if (m_pParent) + { + pTemp = m_pParent->m_pChild; + + if (pTemp == this)m_pParent->m_pChild = this->m_pNextChild; + else + { + while (pTemp->m_pNextChild) + { + if (pTemp->m_pNextChild == this) + { + pTemp->m_pNextChild = this->m_pNextChild; + break; + } + pTemp = pTemp->m_pNextChild; + } + } + } + + if (m_pNextChild) + { + CBaseEntity* pCur = m_pChild; + CBaseEntity* pNext; + while (pCur != NULL) + { + pNext = pCur->m_pNextChild; + //bring children to a stop + UTIL_SetChildVelocity (pCur, g_vecZero, MAX_CHILDS); + UTIL_SetChildAvelocity(pCur, g_vecZero, MAX_CHILDS); + pCur->m_pParent = NULL; + pCur->m_pNextChild = NULL; + pCur = pNext; + } + } +} + +//======================================================================= +// setup physics (execute once at spawn) +//======================================================================= +void CBaseEntity :: SetupPhysics( void ) +{ + //rebuild all parents + if(pFlags & PF_LINKCHILD) LinkChild( this ); + if(m_physinit) return; + SetParent(); //set all parents + m_physinit = true; + PostSpawn();//post spawn +} + +void CBaseEntity :: RestorePhysics( void ) +{ + if(m_iParent) SetParent(); +} + +void CBaseEntity :: ClearPointers( void ) +{ + m_pChild = NULL; + m_pNextChild = NULL; + m_pLinkList = NULL; +} + +//========================================================= +// FVisible - returns true if a line can be traced from +// the caller's eyes to the target vector +//========================================================= +BOOL CBaseEntity :: FVisible ( const Vector &vecOrigin ) +{ + TraceResult tr; + Vector vecLookerOrigin; + + vecLookerOrigin = EyePosition();//look through the caller's 'eyes' + + UTIL_TraceLine(vecLookerOrigin, vecOrigin, ignore_monsters, ignore_glass, ENT(pev)/*pentIgnore*/, &tr); + + if (tr.flFraction != 1.0) + return FALSE; + return TRUE;// line of sight is valid. +} + +//========================================================= +// FVisible - returns true if a line can be traced from +// the caller's eyes to the target +//========================================================= +BOOL CBaseEntity :: FVisible ( CBaseEntity *pEntity ) +{ + TraceResult tr; + Vector vecLookerOrigin; + Vector vecTargetOrigin; + + if( FBitSet( pEntity->pev->flags, FL_NOTARGET )) + return FALSE; + + // don't look through water + if ((pev->waterlevel != 3 && pEntity->pev->waterlevel == 3) || (pev->waterlevel == 3 && pEntity->pev->waterlevel == 0)) + return FALSE; + + vecLookerOrigin = pev->origin + pev->view_ofs;//look through the caller's 'eyes' + vecTargetOrigin = pEntity->EyePosition(); + + UTIL_TraceLine(vecLookerOrigin, vecTargetOrigin, ignore_monsters, ignore_glass, ENT(pev)/*pentIgnore*/, &tr); + + if (tr.flFraction != 1.0 && tr.pHit != ENT(pEntity->pev)) + return FALSE; + return TRUE; +} + +//======================================================================= +// fire bullets +//======================================================================= +void CBaseEntity::FireBullets(ULONG cShots, Vector vecSrc, Vector vecDirShooting, Vector vecSpread, float flDistance, int iBulletType, int iTracerFreq, int iDamage, entvars_t *pevAttacker ) +{ + static int tracerCount; + int tracer; + TraceResult tr; + Vector vecRight = gpGlobals->v_right; + Vector vecUp = gpGlobals->v_up; + + if ( pevAttacker == NULL ) + pevAttacker = pev; // the default attacker is ourselves + + ClearMultiDamage(); + gMultiDamage.type = DMG_BULLET | DMG_NEVERGIB; + + for (ULONG iShot = 1; iShot <= cShots; iShot++) + { + // get circular gaussian spread + float x, y, z; + do { + x = RANDOM_FLOAT(-0.5,0.5) + RANDOM_FLOAT(-0.5,0.5); + y = RANDOM_FLOAT(-0.5,0.5) + RANDOM_FLOAT(-0.5,0.5); + z = x*x+y*y; + } while (z > 1); + + Vector vecDir = vecDirShooting + + x * vecSpread.x * vecRight + + y * vecSpread.y * vecUp; + Vector vecEnd; + + vecEnd = vecSrc + vecDir * flDistance; + UTIL_TraceLine(vecSrc, vecEnd, dont_ignore_monsters, ENT(pev)/*pentIgnore*/, &tr); + + tracer = 0; + if (iTracerFreq != 0 && (tracerCount++ % iTracerFreq) == 0) + { + Vector vecTracerSrc; + + if ( IsPlayer() ) + {// adjust tracer position for player + vecTracerSrc = vecSrc + Vector ( 0 , 0 , -4 ) + gpGlobals->v_right * 2 + gpGlobals->v_forward * 16; + } + else + { + vecTracerSrc = vecSrc; + } + + if ( iTracerFreq != 1 ) // guns that always trace also always decal + tracer = 1; + switch( iBulletType ) + { + case BULLET_MP5: + case BULLET_9MM: + case BULLET_12MM: + case BULLET_357: + case BULLET_556: + case BULLET_762: + case BULLET_BUCKSHOT: + default: + MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, vecTracerSrc ); + WRITE_BYTE( TE_TRACER ); + WRITE_COORD( vecTracerSrc.x ); + WRITE_COORD( vecTracerSrc.y ); + WRITE_COORD( vecTracerSrc.z ); + WRITE_COORD( tr.vecEndPos.x ); + WRITE_COORD( tr.vecEndPos.y ); + WRITE_COORD( tr.vecEndPos.z ); + MESSAGE_END(); + break; + } + } + // do damage, paint decals + if (tr.flFraction != 1.0) + { + CBaseEntity *pEntity = CBaseEntity::Instance(tr.pHit); + + if ( iDamage ) + { + pEntity->TraceAttack(pevAttacker, iDamage, vecDir, &tr, DMG_BULLET | ((iDamage > 16) ? DMG_ALWAYSGIB : DMG_NEVERGIB) ); + + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + } + else switch(iBulletType) + { + default: + case BULLET_9MM: + pEntity->TraceAttack(pevAttacker, _9MM_DMG, vecDir, &tr, DMG_BULLET); + + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + + break; + + case BULLET_MP5: + pEntity->TraceAttack(pevAttacker, _MP5_DMG, vecDir, &tr, DMG_BULLET); + + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + + break; + + case BULLET_12MM: + pEntity->TraceAttack(pevAttacker, _12MM_DMG, vecDir, &tr, DMG_BULLET); + if ( !tracer ) + { + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + } + break; + + case BULLET_556: + pEntity->TraceAttack(pevAttacker, _556_DMG, vecDir, &tr, DMG_BULLET); + if ( !tracer ) + { + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + } + break; + + case BULLET_762: + pEntity->TraceAttack(pevAttacker, _762_DMG, vecDir, &tr, DMG_BULLET); + if ( !tracer ) + { + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + } + break; + + case BULLET_BUCKSHOT: + pEntity->TraceAttack(pevAttacker, BUCKSHOT_DMG, vecDir, &tr, DMG_BULLET); + if ( !tracer ) + { + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + } + break; + + case BULLET_357: + pEntity->TraceAttack(pevAttacker, _357_DMG, vecDir, &tr, DMG_BULLET); + if ( !tracer ) + { + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + DecalGunshot( &tr, iBulletType ); + } + break; + + case BULLET_NONE: // FIX + pEntity->TraceAttack(pevAttacker, 50, vecDir, &tr, DMG_CLUB); + TEXTURETYPE_PlaySound(&tr, vecSrc, vecEnd, iBulletType); + // only decal glass + if ( !FNullEnt(tr.pHit) && VARS(tr.pHit)->rendermode != 0) + { + UTIL_DecalTrace( &tr, DECAL_GLASSBREAK1 + RANDOM_LONG(0,2) ); + } + + break; + } + } + // make bullet trails + UTIL_BubbleTrail( vecSrc, tr.vecEndPos, (flDistance * tr.flFraction) / 64.0 ); + } + ApplyMultiDamage(pev, pevAttacker); +} + +//======================================================================= +// traceing operations +//======================================================================= +void CBaseEntity :: TraceBleed( float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType ) +{ + if (BloodColor() == DONT_BLEED) + return; + + if (flDamage == 0) + return; + + if (!(bitsDamageType & (DMG_CRUSH | DMG_BULLET | DMG_SLASH | DMG_BLAST | DMG_CLUB | DMG_MORTAR))) + return; + + // make blood decal on the wall! + TraceResult Bloodtr; + Vector vecTraceDir; + float flNoise; + int cCount; + int i; + + if (flDamage < 10) + { + flNoise = 0.1; + cCount = 1; + } + else if (flDamage < 25) + { + flNoise = 0.2; + cCount = 2; + } + else + { + flNoise = 0.3; + cCount = 4; + } + + for ( i = 0 ; i < cCount ; i++ ) + { + vecTraceDir = vecDir * -1;// trace in the opposite direction the shot came from (the direction the shot is going) + + vecTraceDir.x += RANDOM_FLOAT( -flNoise, flNoise ); + vecTraceDir.y += RANDOM_FLOAT( -flNoise, flNoise ); + vecTraceDir.z += RANDOM_FLOAT( -flNoise, flNoise ); + + UTIL_TraceLine( ptr->vecEndPos, ptr->vecEndPos + vecTraceDir * -172, ignore_monsters, ENT(pev), &Bloodtr); + + if ( Bloodtr.flFraction != 1.0 ) + { + UTIL_BloodDecalTrace( &Bloodtr, BloodColor() ); + } + } +} + +void CBaseEntity::TraceAttack(entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + Vector vecOrigin = ptr->vecEndPos - vecDir * 4; + + if ( pev->takedamage ) + { + AddMultiDamage( pevAttacker, this, flDamage, bitsDamageType ); + + int blood = BloodColor(); + + if ( blood != DONT_BLEED ) + { + SpawnBlood(vecOrigin, blood, flDamage);// a little surface blood. + TraceBleed( flDamage, vecDir, ptr, bitsDamageType ); + } + } +} + +//======================================================================= +// take damage\health +//======================================================================= +int CBaseEntity :: TakeHealth( float flHealth, int bitsDamageType ) +{ + if(!pev->takedamage) return 0; + if ( pev->health >= pev->max_health ) return 0; + + pev->health += flHealth; + pev->health = min(pev->health, pev->max_health); + + return 1; +} + +int CBaseEntity :: TakeArmor( float flArmor, int suit ) +{ + if(!pev->takedamage) return 0; + if (pev->armorvalue >= MAX_NORMAL_BATTERY) return 0; + + pev->armorvalue += flArmor; + pev->armorvalue = min(pev->armorvalue, MAX_NORMAL_BATTERY); + + return 1; +} + +int CBaseEntity :: TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ) +{ + Vector vecTemp; + + if (!pev->takedamage) return 0; + + // if Attacker == Inflictor, the attack was a melee or other instant-hit attack. + // (that is, no actual entity projectile was involved in the attack so use the shooter's origin). + if ( pevAttacker == pevInflictor ) + { + vecTemp = pevInflictor->origin - ( VecBModelOrigin(pev) ); + } + else // an actual missile was involved. + { + vecTemp = pevInflictor->origin - ( VecBModelOrigin(pev) ); + } + + // this global is still used for glass and other non-monster killables, along with decals. + g_vecAttackDir = vecTemp.Normalize(); + + // save damage based on the target's armor level + + // figure momentum add (don't let hurt brushes or other triggers move player) + if ((!FNullEnt(pevInflictor)) && (pev->movetype == MOVETYPE_WALK || pev->movetype == MOVETYPE_STEP) && (pevAttacker->solid != SOLID_TRIGGER) ) + { + Vector vecDir = pev->origin - (pevInflictor->absmin + pevInflictor->absmax) * 0.5; + vecDir = vecDir.Normalize(); + + float flForce = flDamage * ((32 * 32 * 72.0) / (pev->size.x * pev->size.y * pev->size.z)) * 5; + + if (flForce > 1000.0) flForce = 1000.0; + pev->velocity = pev->velocity + vecDir * flForce; + } + + // do the damage + pev->health -= flDamage; + if (pev->health <= 0) + { + Killed( pevAttacker, GIB_NORMAL ); + return 0; + } + return 1; +} + +int CBaseEntity :: DamageDecal( int bitsDamageType ) +{ + if ( pev->rendermode == kRenderTransAlpha ) + return -1; + + if ( pev->rendermode != kRenderNormal ) + return DECAL_BPROOF1; + + return DECAL_GUNSHOT1 + RANDOM_LONG(0, 4); +} + +//======================================================================= +// killed/create operations +//======================================================================= +CBaseEntity * CBaseEntity::Create( char *szName, const Vector &vecOrigin, const Vector &vecAngles, edict_t *pentOwner ) +{ + edict_t *pent; + int istr = ALLOC_STRING(szName); + CBaseEntity *pEntity; + + // check for virtual entities + if( FUNCTION_FROM_NAME( szName ) != 0 ) + { + pent = CREATE_NAMED_ENTITY( istr ); + if ( FNullEnt( pent )) return NULL; + } + else if(!strncmp( szName, "weapon_", 7)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("weapon_generic")); + if ( FNullEnt( pent )) return NULL; // this never gonna called anymore. just in case + pent->v.netname = istr; + } + else if(!strncmp( szName, "item_", 5)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("item_generic")); + if ( FNullEnt( pent )) return NULL; // this never gonna called anymore. just in case + pent->v.netname = istr; + } + else if(!strncmp( szName, "ammo_", 5)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("item_generic")); + if ( FNullEnt( pent )) return NULL; // this never gonna called anymore. just in case + pent->v.netname = istr; + } + else //unknown error + { + Msg("can't create %s\n", szName ); + return NULL; + } + + pEntity = Instance( pent ); + pEntity->pev->owner = pentOwner; + pEntity->pev->origin = vecOrigin; + pEntity->pev->angles = vecAngles; + DispatchSpawn( pEntity->edict() ); + return pEntity; +} + +CBaseEntity * CBaseEntity::CreateGib( char *szName, char *szModel ) +{ + edict_t *pent; + CBaseEntity *pEntity; + string_t model = MAKE_STRING( szModel ); + int istr = ALLOC_STRING(szName); + + //check for virtual entities + if(GetProcAddress( GetModuleHandle("server"), szName )) + { + pent = CREATE_NAMED_ENTITY(istr); + if ( FNullEnt( pent )) return NULL; + } + else if(!strncmp( szName, "weapon_", 7)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("weapon_generic")); + if ( FNullEnt( pent )) return NULL; //this never gonna called anymore. just in case + pent->v.netname = istr; + } + else if(!strncmp( szName, "item_", 5)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("item_generic")); + if ( FNullEnt( pent )) return NULL;//this never gonna called anymore. just in case + pent->v.netname = istr; + } + else if(!strncmp( szName, "ammo_", 5)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("item_generic")); + if ( FNullEnt( pent )) return NULL;//this never gonna called anymore. just in case + pent->v.netname = istr; + } + else //unknown error + { + Msg("can't create %s\n", szName ); + return NULL; + } + + pEntity = Instance( pent ); + DispatchSpawn( pEntity->edict() ); + + if(!FStringNull( model )) + { + UTIL_SetModel( pEntity->edict(), szModel ); + Msg("szModel %s\n", szModel ); + } + return pEntity; +} + +void CBaseEntity :: Killed( entvars_t *pevAttacker, int iGib ) +{ + pev->takedamage = DAMAGE_NO; + pev->deadflag = DEAD_DEAD; + UTIL_Remove( this ); +} + +void CBaseEntity::UpdateOnRemove( void ) +{ + ResetParent(); + + if ( FBitSet( pev->flags, FL_GRAPHED ) ) + { + for (int i = 0 ; i < WorldGraph.m_cLinks ; i++ ) + { + if ( WorldGraph.m_pLinkPool [ i ].m_pLinkEnt == pev ) + WorldGraph.m_pLinkPool [ i ].m_pLinkEnt = NULL; + } + } + if ( pev->globalname ) gGlobalState.EntitySetState( pev->globalname, GLOBAL_DEAD ); +} + +//======================================================================= +// three methods of remove entity +//======================================================================= +void CBaseEntity :: Remove( void ) +{ + UpdateOnRemove(); + if (pev->health > 0)pev->health = 0; + REMOVE_ENTITY(ENT(pev)); +} + +void CBaseEntity :: PVSRemove( void ) +{ + if ( FNullEnt( FIND_CLIENT_IN_PVS( edict()))) SetThink( Remove ); + SetNextThink( 0.1 ); +} + +void CBaseEntity :: Fadeout( void ) +{ + if (pev->rendermode == kRenderNormal) + { + pev->renderamt = 255; + pev->rendermode = kRenderTransTexture; + } + + pev->solid = SOLID_NOT; + pev->avelocity = g_vecZero; + + if ( pev->renderamt > 7 ) + { + pev->renderamt -= 7; + SetNextThink( 0.1 ); + } + else + { + pev->renderamt = 0; + SetNextThink( 0.2 ); + SetThink( Remove ); + } +} + +//======================================================================= +// global save\restore data +//======================================================================= +TYPEDESCRIPTION CBaseEntity::m_SaveData[] = +{ + DEFINE_FIELD( CBaseEntity, m_pGoalEnt, FIELD_CLASSPTR ), + + //parent system saves + DEFINE_FIELD( CBaseEntity, m_iParent, FIELD_STRING ), + DEFINE_FIELD( CBaseEntity, m_pParent, FIELD_CLASSPTR ), + DEFINE_FIELD( CBaseEntity, m_pChild, FIELD_CLASSPTR ), + DEFINE_FIELD( CBaseEntity, m_pNextChild, FIELD_CLASSPTR ), + DEFINE_FIELD( CBaseEntity, OffsetOrigin, FIELD_VECTOR ), + DEFINE_FIELD( CBaseEntity, OffsetAngles, FIELD_VECTOR ), + DEFINE_FIELD( CBaseEntity, pFlags, FIELD_INTEGER ), + DEFINE_FIELD( CBaseEntity, m_physinit, FIELD_BOOLEAN ), + + //local child coordinates + DEFINE_FIELD( CBaseEntity, PostOrigin, FIELD_VECTOR ), + DEFINE_FIELD( CBaseEntity, PostAngles, FIELD_VECTOR ), + DEFINE_FIELD( CBaseEntity, PostVelocity, FIELD_VECTOR ), + DEFINE_FIELD( CBaseEntity, PostAvelocity, FIELD_VECTOR ), + + //think time + DEFINE_FIELD( CBaseEntity, m_fNextThink, FIELD_TIME ), + DEFINE_FIELD( CBaseEntity, m_fPevNextThink, FIELD_TIME ), + + DEFINE_FIELD( CBaseEntity, m_iStyle, FIELD_INTEGER ), + DEFINE_FIELD( CBaseEntity, m_iAcessLevel, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseEntity, m_pfnThink, FIELD_FUNCTION ), + DEFINE_FIELD( CBaseEntity, m_pfnTouch, FIELD_FUNCTION ), + DEFINE_FIELD( CBaseEntity, m_pfnUse, FIELD_FUNCTION ), + DEFINE_FIELD( CBaseEntity, m_pfnBlocked, FIELD_FUNCTION ), +}; + +int CBaseEntity::Save( CSave &save ) +{ + ThinkCorrection(); + + if ( save.WriteEntVars( "ENTVARS", pev ) ) + { + if (pev->targetname) + return save.WriteFields( STRING(pev->targetname), "BASE", this, m_SaveData, ARRAYSIZE(m_SaveData) ); + else return save.WriteFields( STRING(pev->classname ), "BASE", this, m_SaveData, ARRAYSIZE(m_SaveData) ); + } + return 0; +} + +int CBaseEntity::Restore( CRestore &restore ) +{ + int status; + + status = restore.ReadEntVars( "ENTVARS", pev ); + if ( status ) status = restore.ReadFields( "BASE", this, m_SaveData, ARRAYSIZE(m_SaveData) ); + + if ( pev->modelindex != 0 && !FStringNull(pev->model) ) + { + Vector mins = pev->mins, maxs = pev->maxs; // Set model is about to destroy these + + UTIL_PrecacheModel( pev->model ); + UTIL_SetModel(ENT(pev), pev->model); + UTIL_SetSize(pev, mins, maxs); + } + return status; +} + +//======================================================================= +// collisoin boxes +//======================================================================= +void CBaseEntity::SetObjectCollisionBox( void ) +{ + ::SetObjectCollisionBox( pev ); +} + +int CBaseEntity :: Intersects( CBaseEntity *pOther ) +{ + if ( pOther->pev->absmin.x > pev->absmax.x || + pOther->pev->absmin.y > pev->absmax.y || + pOther->pev->absmin.z > pev->absmax.z || + pOther->pev->absmax.x < pev->absmin.x || + pOther->pev->absmax.y < pev->absmin.y || + pOther->pev->absmax.z < pev->absmin.z ) + return FALSE; + return TRUE; +} + +//======================================================================= +// Dormant operations +//======================================================================= +void CBaseEntity :: MakeDormant( void ) +{ + SetBits( pev->flags, FL_DORMANT ); + + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + SetBits( pev->effects, EF_NODRAW ); + DontThink(); + UTIL_SetOrigin( this, pev->origin ); +} + +int CBaseEntity :: IsDormant( void ) +{ + return FBitSet( pev->flags, FL_DORMANT ); +} + +BOOL CBaseEntity :: IsInWorld( void ) +{ + if (pev->origin.x >= MAP_HALFSIZE) return FALSE; + if (pev->origin.y >= MAP_HALFSIZE) return FALSE; + if (pev->origin.z >= MAP_HALFSIZE) return FALSE; + if (pev->origin.x <= -MAP_HALFSIZE) return FALSE; + if (pev->origin.y <= -MAP_HALFSIZE) return FALSE; + if (pev->origin.z <= -MAP_HALFSIZE) return FALSE; + if (pev->velocity.x >= MAX_VELOCITY) return FALSE; + if (pev->velocity.y >= MAX_VELOCITY) return FALSE; + if (pev->velocity.z >= MAX_VELOCITY) return FALSE; + if (pev->velocity.x <= -MAX_VELOCITY) return FALSE; + if (pev->velocity.y <= -MAX_VELOCITY) return FALSE; + if (pev->velocity.z <= -MAX_VELOCITY) return FALSE; + + return TRUE; +} \ No newline at end of file diff --git a/server/ents/baseentity.h b/server/ents/baseentity.h new file mode 100644 index 00000000..29858b55 --- /dev/null +++ b/server/ents/baseentity.h @@ -0,0 +1,324 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + + +#ifndef BASEENTITY_H +#define BASEENTITY_H + +class CBaseEntity +{ +public: + // Constructor. Set engine to use C/C++ callback functions + // pointers to engine data + entvars_t *pev; // Don't need to save/restore this pointer, the engine resets it + + // path corners + CBaseEntity *m_pGoalEnt;// path corner we are heading towards + CBaseEntity *m_pLink;// used for temporary link-list operations. + + float m_fNextThink; + float m_fPevNextThink; + float flTravelTime; //time to moving brushes + int m_iStyle; + int m_iAcessLevel;//acess level for retinal sacners + + //=================================================================================================== + // Xash BaseEntity + //=================================================================================================== + + // Gets the interface to the collideable representation of the entity + virtual void SetModelIndex( int index ); + virtual int GetModelIndex( void ) const; + + // Returns a CBaseAnimating if the entity is derived from CBaseAnimating. + virtual CBaseAnimating* GetBaseAnimating() { return NULL; } + + void SetName( string_t newTarget ); + void SetParent( string_t newParent, CBaseEntity *pActivator ); + virtual void ChangeCamera( string_t newcamera ) {} + +public: + unsigned char m_iParentAttachment; // 0 if we're relative to the parent's absorigin and absangles. + EHANDLE m_hParent; + EHANDLE m_hMoveParent; + EHANDLE m_hMoveChild; + EHANDLE m_hMovePeer; + + //=================================================================================================== + // Xash Parent System 0.2 beta + //=================================================================================================== + + Vector PostOrigin; //child postorigin + Vector PostAngles; //child postangles + Vector PostVelocity; //child postvelocity + Vector PostAvelocity; //child postavelocity + Vector OffsetOrigin; //spawn offset origin + Vector OffsetAngles; //spawn offset angles + Vector pParentAngles; //temp container + Vector pParentOrigin; //temp container + Vector m_vecSpawnOffset; //temp container + int pFlags; //xash flags + BOOL m_physinit; //physics initializator + + virtual void SetParent ( void ) { SetParent(m_iParent); } + void SetParent ( int m_iNewParent, int m_iAttachment = 0 ); + void SetParent ( CBaseEntity* pParent, int m_iAttachment = 0 ); + void ResetParent( void ); + + virtual void SetupPhysics( void );//setup parent system and physics + + CBaseEntity *m_pParent; //pointer to parent entity + CBaseEntity *m_pChild; //pointer to children(may be this) + CBaseEntity *m_pNextChild; //link to next chlidren + CBaseEntity *m_pLinkList; //list of linked childrens + + string_t m_iParent;//name of parent + virtual void SetNextThink( float delay, BOOL correctSpeed = FALSE ); + virtual void AbsoluteNextThink( float time, BOOL correctSpeed = FALSE ); + void SetEternalThink( void ); + void DontThink( void ); + virtual void ThinkCorrection( void ); + + //phys metods + virtual void SetAngularImpulse( float impulse ){} + virtual void SetLinearImpulse( float impulse ) {} + + // initialization functions + virtual void Spawn( void ) { return; } + virtual void Precache( void ) { return; } + virtual void KeyValue( KeyValueData* pkvd) + { + if (FStrEq(pkvd->szKeyName, "parent")) + { + m_iParent = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "style")) + { + m_iStyle = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else pkvd->fHandled = FALSE; + } + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + virtual int ObjectCaps( void ) { return m_pParent?m_pParent->ObjectCaps()&FCAP_ACROSS_TRANSITION:FCAP_ACROSS_TRANSITION; } + virtual void Activate( void ) {} + virtual void PostActivate( void ) {} + virtual void PostSpawn( void ) {} + virtual void DesiredAction( void ) {} + virtual void StartMessage( CBasePlayer *pPlayer ) {} + + // Setup the object->object collision box (pev->mins / pev->maxs is the object->world collision box) + virtual void SetObjectCollisionBox( void ); + + void UTIL_AutoSetSize( void )//automatically set collision box + { + dstudiohdr_t *pstudiohdr; + pstudiohdr = (dstudiohdr_t*)GET_MODEL_PTR( ENT(pev) ); + + if (pstudiohdr == NULL) + { + ALERT(at_console,"Unable to fetch model pointer!\n"); + return; + } + dstudioseqdesc_t *pseqdesc; + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex); + UTIL_SetSize(pev,pseqdesc[ pev->sequence ].bbmin,pseqdesc[ pev->sequence ].bbmax); + } + + // Classify - returns the type of group (e.g., "alien monster", or "human military" so that monsters + // on the same side won't attack each other, even if they have different classnames. + virtual int Classify ( void ) { return CLASS_NONE; }; + virtual void DeathNotice ( entvars_t *pevChild ) {}// monster maker children use this to tell the monster maker that they have died. + + + // global concept of "entities with states", so that state_watchers and + // mastership (mastery? masterhood?) can work universally. + virtual STATE GetState ( void ) { return STATE_OFF; }; + + // For team-specific doors in multiplayer, etc: a master's state depends on who wants to know. + virtual STATE GetState ( CBaseEntity* pEnt ) { return GetState(); }; + + static TYPEDESCRIPTION m_SaveData[]; + + virtual void ClearPointers( void ); + virtual void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + virtual int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ); + virtual int TakeHealth( float flHealth, int bitsDamageType ); + virtual int TakeArmor( float flArmor, int suit = 0 ); + virtual int TakeItem( int iItem ) { return 0; } + virtual void Killed( entvars_t *pevAttacker, int iGib ); + virtual int BloodColor( void ) { return DONT_BLEED; } + virtual void TraceBleed( float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType ); + virtual CBaseMonster *MyMonsterPointer( void ) { return NULL;} + virtual CSquadMonster *MySquadMonsterPointer( void ) { return NULL;} + virtual CBaseBrush *MyBrushPointer( void ) { return NULL; } + virtual void AddPoints( int score, BOOL bAllowNegativeScore ) {} + virtual void AddPointsToTeam( int score, BOOL bAllowNegativeScore ) {} + virtual BOOL AddPlayerItem( CBasePlayerWeapon *pItem ) { return 0; } + virtual BOOL RemovePlayerItem( CBasePlayerWeapon *pItem ) { return 0; } + virtual int GiveAmmo( int iAmount, char *szName, int iMax ) { return -1; }; + virtual float GetDelay( void ) { return 0; } + virtual int IsMoving( void ) { return pev->velocity != g_vecZero; } + virtual void RestorePhysics( void ); + virtual void OverrideReset( void ) {} + virtual int DamageDecal( int bitsDamageType ); + // This is ONLY used by the node graph to test movement through a door + virtual void SetToggleState( int state ) {} + virtual void StartSneaking( void ) {} + virtual void StopSneaking( void ) {} + virtual BOOL OnControls( entvars_t *pev ) { return FALSE; } + virtual BOOL IsSneaking( void ) { return FALSE; } + virtual BOOL IsAlive( void ) { return (pev->deadflag == DEAD_NO) && pev->health > 0; } + virtual BOOL IsBSPModel( void ) { return pev->solid == SOLID_BSP || pev->movetype == MOVETYPE_PUSHSTEP; } + virtual BOOL ReflectGauss( void ) { return ( IsBSPModel() && !pev->takedamage ); } + virtual BOOL HasTarget( string_t targetname ) { return FStrEq(STRING(targetname), STRING(pev->targetname) ); } + virtual BOOL IsInWorld( void ); + virtual BOOL IsPlayer( void ) { return FALSE; } + virtual BOOL IsPushable( void ) { return FALSE; } + virtual BOOL IsMonster( void ) { return (pev->flags & FL_MONSTER ? TRUE : FALSE); } + virtual BOOL IsNetClient( void ) { return FALSE; } + virtual BOOL IsFuncScreen( void ) { return FALSE; } + virtual const char *TeamID( void ) { return ""; } + + virtual CBaseEntity *GetNext( void ) { return NULL; } + virtual CBaseEntity *GetPrev( void ) { return NULL; } + + // fundamental callbacks + void (CBaseEntity ::*m_pfnThink)(void); + void (CBaseEntity ::*m_pfnTouch)( CBaseEntity *pOther ); + void (CBaseEntity ::*m_pfnUse)( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void (CBaseEntity ::*m_pfnBlocked)( CBaseEntity *pOther ); + + virtual void Think( void ) { if (m_pfnThink) (this->*m_pfnThink)(); }; + virtual void Touch( CBaseEntity *pOther ) { if (m_pfnTouch) (this->*m_pfnTouch)( pOther ); }; + virtual void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + if (m_pfnUse) (this->*m_pfnUse)( pActivator, pCaller, useType, value ); + } + virtual void Blocked( CBaseEntity *pOther ) { if (m_pfnBlocked) (this->*m_pfnBlocked)( pOther ); }; + + // allow engine to allocate instance data + void *operator new( size_t stAllocateBlock, entvars_t *pev ) + { + return (void *)ALLOC_PRIVATE(ENT(pev), stAllocateBlock); + }; + + // don't use this. +#if _MSC_VER >= 1200 // only build this code if MSVC++ 6.0 or higher + void operator delete(void *pMem, entvars_t *pev) + { + pev->flags |= FL_KILLME; + }; +#endif + + void UpdateOnRemove( void ); + + // common member functions + void EXPORT Remove( void ); + void EXPORT Fadeout( void ); + void EXPORT PVSRemove( void ); + void EXPORT SUB_CallUseToggle( void ) { this->Use( this, this, USE_TOGGLE, 0 ); } + + void FireBullets( ULONG cShots, Vector vecSrc, Vector vecDirShooting, Vector vecSpread, float flDistance, int iBulletType, int iTracerFreq = 4, int iDamage = 0, entvars_t *pevAttacker = NULL ); + + virtual CBaseEntity *Respawn( void ) { return NULL; } + + // Do the bounding boxes of these two intersect? + int Intersects( CBaseEntity *pOther ); + void MakeDormant( void ); + int IsDormant( void ); + BOOL IsLockedByMaster( void ) { return FALSE; } + + static CBaseEntity *Instance( edict_t *pent ) + { + if ( !pent ) pent = ENT(0); + CBaseEntity *pEnt = (CBaseEntity *)GET_PRIVATE(pent); + return pEnt; + } + + static CBaseEntity *Instance( entvars_t *pev ) { return Instance( ENT( pev ) ); } + static CBaseEntity *Instance( int eoffset) { return Instance( ENT( eoffset) ); } + + CBaseMonster *GetMonsterPointer( entvars_t *pevMonster ) + { + CBaseEntity *pEntity = Instance( pevMonster ); + if ( pEntity ) return pEntity->MyMonsterPointer(); + return NULL; + } + CBaseMonster *GetMonsterPointer( edict_t *pentMonster ) + { + CBaseEntity *pEntity = Instance( pentMonster ); + if ( pEntity ) return pEntity->MyMonsterPointer(); + return NULL; + } + + + // Ugly code to lookup all functions to make sure they are exported when set. +#ifdef _DEBUG + void FunctionCheck( void *pFunction, char *name ) + { + if (pFunction && !NAME_FOR_FUNCTION((unsigned long)(pFunction)) ) + ALERT( at_error, "No EXPORT: %s:%s (%08lx)\n", STRING(pev->classname), name, (unsigned long)pFunction ); + } + + BASEPTR ThinkSet( BASEPTR func, char *name ) + { + m_pfnThink = func; + FunctionCheck( (void *)*((int *)((char *)this + ( offsetof(CBaseEntity,m_pfnThink)))), name ); + return func; + } + ENTITYFUNCPTR TouchSet( ENTITYFUNCPTR func, char *name ) + { + m_pfnTouch = func; + FunctionCheck( (void *)*((int *)((char *)this + ( offsetof(CBaseEntity,m_pfnTouch)))), name ); + return func; + } + USEPTR UseSet( USEPTR func, char *name ) + { + m_pfnUse = func; + FunctionCheck( (void *)*((int *)((char *)this + ( offsetof(CBaseEntity,m_pfnUse)))), name ); + return func; + } + ENTITYFUNCPTR BlockedSet( ENTITYFUNCPTR func, char *name ) + { + m_pfnBlocked = func; + FunctionCheck( (void *)*((int *)((char *)this + ( offsetof(CBaseEntity,m_pfnBlocked)))), name ); + return func; + } + +#endif + // used by monsters that are created by the MonsterMaker + virtual void UpdateOwner( void ) { return; }; + static CBaseEntity *Create( char *szName, const Vector &vecOrigin, const Vector &vecAngles, edict_t *pentOwner = NULL ); + static CBaseEntity *CBaseEntity::CreateGib( char *szName, char *szModel ); + + virtual BOOL FBecomeProne( void ) {return FALSE;}; + edict_t *edict() { return ENT( pev ); }; + EOFFSET eoffset( ) { return OFFSET( pev ); }; + int entindex( ) { return ENTINDEX( edict() ); }; + + virtual Vector Center( ) { return (pev->absmax + pev->absmin) * 0.5; }; // center point of entity + virtual Vector EyePosition( ) { return pev->origin + pev->view_ofs; }; // position of eyes + virtual Vector EarPosition( ) { return pev->origin + pev->view_ofs; }; // position of ears + virtual Vector BodyTarget( const Vector &posSrc ) { return Center( ); }; // position to shoot at + + virtual int Illumination( ) { return GETENTITYILLUM( ENT( pev ) ); }; + + virtual BOOL FVisible ( CBaseEntity *pEntity ); + virtual BOOL FVisible ( const Vector &vecOrigin ); +}; + +inline void CBaseEntity::SetModelIndex( int index ) +{ + pev->modelindex = index; +} + +inline int CBaseEntity::GetModelIndex( void ) const +{ + return pev->modelindex; +} + +#endif //BASEENTITY_H \ No newline at end of file diff --git a/server/ents/basefunc.cpp b/server/ents/basefunc.cpp new file mode 100644 index 00000000..f391ab8c --- /dev/null +++ b/server/ents/basefunc.cpp @@ -0,0 +1,2361 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// basefunc.cpp - brush based entities: tanks, +// cameras, vehicles etc +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "sfx.h" +#include "decals.h" +#include "client.h" +#include "saverestore.h" +#include "gamerules.h" +#include "basebrush.h" +#include "defaults.h" +#include "player.h" + +//======================================================================= +// STATIC BRUSHES +//======================================================================= + +//======================================================================= +// func_wall - standard not moving wall. affect to physics +//======================================================================= +class CFuncWall : public CBaseBrush +{ +public: + void Spawn( void ); + void TurnOn( void ); + void TurnOff( void ); + void Precache( void ){ CBaseBrush::Precache(); } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } +}; +LINK_ENTITY_TO_CLASS( func_wall, CFuncWall ); +LINK_ENTITY_TO_CLASS( func_wall_toggle, CFuncWall ); +LINK_ENTITY_TO_CLASS( func_illusionary, CFuncWall ); + +void CFuncWall :: Spawn( void ) +{ + CBaseBrush::Spawn(); + + if(FClassnameIs(pev, "func_illusionary" )) + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + UTIL_SetModel( ENT(pev), pev->model ); + + //Smart field system ® + if (!FStringNull(pev->angles) && FStringNull(pev->origin)) + { + pev->angles = g_vecZero; + DevMsg( "\n======/Xash SmartFiled System/======\n\n"); + DevMsg( "Create brush origin for %s,\nif we want correctly set angles\n", STRING(pev->classname)); + SHIFT; + } + pev->angles[1] = 0 - pev->angles[1]; + if(pev->spawnflags & SF_START_ON) TurnOff(); + else TurnOn(); +} + +void CFuncWall :: TurnOff( void ) +{ + if(FClassnameIs(pev, "func_wall_toggle" )) + { + pev->solid = SOLID_NOT; + pev->effects |= EF_NODRAW; + UTIL_SetOrigin( this, pev->origin ); + } + else pev->frame = 0; + + m_iState = STATE_OFF; +} + +void CFuncWall :: TurnOn( void ) +{ + if(FClassnameIs(pev, "func_wall_toggle" )) + { + pev->solid = SOLID_BSP; + pev->effects &= ~EF_NODRAW; + UTIL_SetOrigin( this, pev->origin ); + } + else pev->frame = 1; + + m_iState = STATE_ON; +} + +void CFuncWall :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if (useType == USE_ON) TurnOn(); + else if (useType == USE_OFF)TurnOff(); + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Visible: %s\n", GetStringForState( GetState()), pev->effects & EF_NODRAW ? "No" : "Yes"); + Msg( "Contents: %s, Texture frame: %.f\n", GetContentsString( pev->skin ), pev->frame); + } +} +//======================================================================= +// func_breakable - generic breakable brush. +//======================================================================= +class CFuncBreakable : public CBaseBrush +{ +public: + void Spawn( void ); + void Precache( void ){ CBaseBrush::Precache(); } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ){ if (IsBreakable()) Die(); } +}; +LINK_ENTITY_TO_CLASS( func_breakable, CFuncBreakable ); + +void CFuncBreakable :: Spawn( void ) +{ + CBaseBrush::Spawn(); + UTIL_SetModel( ENT(pev), pev->model ); + + if ( FBitSet( pev->spawnflags, SF_BREAK_TRIGGER_ONLY )) pev->takedamage = DAMAGE_NO; + else pev->takedamage = DAMAGE_YES; +} +//======================================================================= +// func_monsterclip - invisible contents that monster can't walk across +//======================================================================= +class CFuncClip : public CBaseEntity +{ +public: + void Spawn( void ) + { + pev->effects = EF_NODRAW; + pev->contents = CONTENTS_MONSTERCLIP; + UTIL_SetModel( ENT(pev), pev->model ); + } +}; +LINK_ENTITY_TO_CLASS( func_monsterclip, CFuncClip ); + +//======================================================================= +// func_lamp - switchable brush light. +//======================================================================= +class CFuncLamp : public CBaseBrush +{ +public: + void Spawn( void ); + void Precache( void ){ CBaseBrush::Precache(); } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ); + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType ); + void EXPORT Flicker( void ); + void Die( void ); +}; +LINK_ENTITY_TO_CLASS( func_lamp, CFuncLamp ); + +void CFuncLamp :: Spawn( void ) +{ + m_Material = Glass; + CBaseBrush::Spawn(); + UTIL_SetModel( ENT(pev), pev->model ); + + if(pev->spawnflags & SF_START_ON) Use( this, this, USE_ON, 0 ); + else Use( this, this, USE_OFF, 0 ); +} + +void CFuncLamp :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if(IsLockedByMaster( useType )) return; + if(m_iState == STATE_DEAD && useType != USE_SHOWINFO)return;//lamp is broken + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if (useType == USE_ON) + { + if(m_flDelay)//make flickering delay + { + pev->frame = 0;//light texture is on + m_iState = STATE_TURN_ON; + LIGHT_STYLE(m_iStyle, "mmamammmmammamamaaamammma"); + SetThink( Flicker ); + SetNextThink(m_flDelay); + } + else + { //instantly enable + m_iState = STATE_ON; + pev->frame = 0;//light texture is on + LIGHT_STYLE(m_iStyle, "k"); + UTIL_FireTargets( pev->target, this, this, USE_ON );//lamp enable + } + } + else if (useType == USE_OFF) + { + pev->frame = 1;//light texture is off + LIGHT_STYLE(m_iStyle, "a"); + UTIL_FireTargets( pev->target, this, this, USE_OFF );//lamp disable + m_iState = STATE_OFF; + } + else if (useType == USE_SET) Die(); + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Light style %d\n", GetStringForState( GetState()), m_iStyle); + Msg( "Texture frame: %.f. Health %.f\n", pev->frame, pev->health); + } +} + +void CFuncLamp::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType ) +{ + if(m_iState == STATE_DEAD) return; + const char *pTextureName; + Vector start = pevAttacker->origin + pevAttacker->view_ofs; + Vector end = start + vecDir * 1024; + edict_t *pWorld = ptr->pHit; + if ( pWorld )pTextureName = TRACE_TEXTURE( pWorld, start, end ); + if(strstr(pTextureName, "+0~") || strstr(pTextureName, "~") )//take damage only at light texture + { + UTIL_Sparks( ptr->vecEndPos ); + pev->oldorigin = ptr->vecEndPos;//save last point of damage + pev->takedamage = DAMAGE_YES;//inflict damage only at light texture + } + else pev->takedamage = DAMAGE_NO; + CBaseLogic::TraceAttack( pevAttacker, flDamage, vecDir, ptr, bitsDamageType ); +} +int CFuncLamp::TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ) +{ + if ( bitsDamageType & DMG_BLAST ) flDamage *= 3; + else if ( bitsDamageType & DMG_CLUB) flDamage *= 2; + else if ( bitsDamageType & DMG_SONIC ) flDamage *= 1.2; + else if ( bitsDamageType & DMG_SHOCK ) flDamage *= 10;//!!!! over voltage + else if ( bitsDamageType & DMG_BULLET) flDamage *= 0.5;//half damage at bullet + pev->health -= flDamage;//calculate health + + if (pev->health <= 0) + { + Die(); + return 0; + } + CBaseBrush::DamageSound(); + + return 1; +} + +void CFuncLamp::Die( void ) +{ + //lamp is random choose die style + if(m_iState == STATE_OFF) + { + pev->frame = 1;//light texture is off + LIGHT_STYLE(m_iStyle, "a"); + DontThink(); + } + else + { //simple randomization + pev->impulse = RANDOM_LONG(1, 2); + SetThink( Flicker ); + SetNextThink( 0.1 + (RANDOM_LONG(1, 2) * 0.1)); + } + + m_iState = STATE_DEAD;//lamp is die + pev->health = 0;//set health to NULL + pev->takedamage = DAMAGE_NO; + UTIL_FireTargets( pev->target, this, this, USE_OFF );//lamp broken + + switch ( RANDOM_LONG(0,1)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/glass/bustglass1.wav", 0.7, ATTN_IDLE); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/glass/bustglass2.wav", 0.8, ATTN_IDLE); break; + } + Vector vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + //spawn glass gibs + SFX_MakeGibs( m_idShard, vecSpot, pev->size, g_vecZero, 50, BREAK_GLASS); +} + +void CFuncLamp :: Flicker( void ) +{ + if(m_iState == STATE_TURN_ON)//flickering on enable + { + LIGHT_STYLE(m_iStyle, "k"); + UTIL_FireTargets( pev->target, this, this, USE_ON );//Lamp enabled + m_iState = STATE_ON; + DontThink(); + return; + } + if(pev->impulse == 1)//fadeout on break + { + pev->frame = 1;//light texture is off + LIGHT_STYLE(m_iStyle, "a"); + SetThink( NULL ); + return; + } + if(pev->impulse == 2)//broken flickering + { + //make different flickering + switch ( RANDOM_LONG(0,3)) + { + case 0: LIGHT_STYLE(m_iStyle, "abcaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); break; + case 1: LIGHT_STYLE(m_iStyle, "acaaabaaaaaaaaaaaaaaaaaaaaaaaaaaa"); break; + case 2: LIGHT_STYLE(m_iStyle, "aaafbaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); break; + case 3: LIGHT_STYLE(m_iStyle, "aaaaaaaaaaaaagaaaaaaaaacaaaacaaaa"); break; + } + pev->frags = RANDOM_FLOAT(0.5, 10); + UTIL_Sparks( pev->oldorigin ); + switch ( RANDOM_LONG(0, 2) ) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark1.wav", 0.4, ATTN_IDLE); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark2.wav", 0.3, ATTN_IDLE); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark3.wav", 0.35, ATTN_IDLE); break; + } + if(pev->frags > 6.5) pev->impulse = 1;//stop sparking obsolete + } + SetNextThink(pev->frags); +} + +//======================================================================= +// func_conveyor - conveyor belt +//======================================================================= +class CFuncConveyor : public CBaseBrush +{ +public: + void Spawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void UpdateSpeed( float speed ); + void TogglePhysic( BOOL enable ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } +}; +LINK_ENTITY_TO_CLASS( func_conveyor, CFuncConveyor ); + +void CFuncConveyor :: Spawn( void ) +{ + CBaseBrush::Spawn(); + UTIL_LinearVector( this ); // movement direction + + if( !m_pParent ) pev->flags |= FL_WORLDBRUSH; + UTIL_SetModel( ENT(pev), STRING( pev->model )); + + if( pev->spawnflags & SF_NOTSOLID ) + TogglePhysic( FALSE ); + else TogglePhysic( TRUE ); + + // smart field system ® + if( pev->speed == 0 ) pev->speed = 100; + pev->frags = pev->speed; // save initial speed + if( pev->spawnflags & SF_START_ON ) Use( this, this, USE_ON, 0 ); +} + +void CFuncConveyor :: TogglePhysic( BOOL enable ) +{ + if( enable ) + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_CONVEYOR; + } + else + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + } +} + +void CFuncConveyor :: UpdateSpeed( float speed ) +{ + // save speed into framerate, and direction into body + pev->framerate = pev->speed; + pev->body = (pev->speed < 0 ); + UTIL_FireTargets( pev->target, this, this, USE_TOGGLE, speed ); // conveyer change speed +} + +void CFuncConveyor :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if(IsLockedByMaster( useType )) return; + + if( useType == USE_TOGGLE ) + { + if( m_iState == STATE_ON ) + useType = USE_OFF; + else useType = USE_ON; + } + if( useType == USE_ON ) + { + UpdateSpeed( pev->speed ); + if(!( pev->spawnflags & SF_NOTSOLID )) //don't push + pev->movetype = MOVETYPE_CONVEYOR; + m_iState = STATE_ON; + } + else if( useType == USE_OFF ) + { + UpdateSpeed( 0.1 ); + pev->movetype = MOVETYPE_NONE; + m_iState = STATE_OFF; + } + else if( useType == USE_SET ) // set new speed + { + if( value ) pev->speed = value; // set new speed ( can be negative ) + else pev->speed = -pev->speed; // just reverse + if( m_iState == STATE_ON ) UpdateSpeed( pev->speed ); + } + else if( useType == USE_RESET ) // restore initial speed + { + pev->speed = pev->frags; + if( m_iState == STATE_ON ) UpdateSpeed( pev->speed ); + } + else if( useType == USE_SHOWINFO ) + { + ALERT( at_console, "======/Xash Debug System/======\n" ); + ALERT( at_console, "classname: %s\n", STRING( pev->classname )); + ALERT( at_console, "State: %s, Conveyor speed %f\n", GetStringForState( GetState()), pev->speed ); + SHIFT; + } +} + +//======================================================================= +// func_mirror - breakable mirror brush +//======================================================================= +class CFuncMirror : public CBaseBrush +{ +public: + void Spawn( void ); + void StartMessage( CBasePlayer *pPlayer ); + void Precache( void ){ CBaseBrush::Precache(); } + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + virtual STATE GetState( void ) { return pev->body ? STATE_ON:STATE_OFF; }; +}; +LINK_ENTITY_TO_CLASS( func_mirror, CFuncMirror ); + +void CFuncMirror::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "alpha")) + { + pev->sequence = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseBrush::KeyValue( pkvd ); +} + +void CFuncMirror :: Spawn( void ) +{ + m_Material = Glass; + CBaseBrush::Spawn(); + if (!m_pParent) pev->flags = FL_WORLDBRUSH; + + if(IsMultiplayer()) + { + ALERT(at_console, "\n======/Xash SmartFiled System/======\n\n"); + ALERT(at_console, "Sorry, %s don't working in multiplayer\nPlease, use static world mirrors\n", STRING(pev->classname)); + SHIFT; + } + UTIL_SetModel( ENT(pev), pev->model ); + if(pev->spawnflags & SF_NOTSOLID)//make illusionary wall + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + pev->body = 1; +} + +void CFuncMirror :: StartMessage( CBasePlayer *pPlayer ) +{ + MESSAGE_BEGIN( MSG_ONE, gmsgAddMirror, NULL, pPlayer->pev ); + WRITE_SHORT( entindex() ); + MESSAGE_END(); +} + +void CFuncMirror :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if( useType == USE_TOGGLE ) + { + if( pev->body ) + useType = USE_OFF; + else useType = USE_ON; + } + if( useType == USE_ON ) pev->body = TRUE; + if( useType == USE_OFF ) pev->body = FALSE; + if( useType == USE_SET ) Die(); + if( useType == USE_SHOWINFO ) + { + DEBUGHEAD; + ALERT(at_console, "State: %s, alpha %d\n", GetStringForState( GetState()), pev->sequence); + ALERT(at_console, "Renderamt: %.f, Health %.f\n", pev->renderamt, pev->health); + } +} + +//======================================================================= +// func_monitor - A monitor that renders the view from a camera entity. +//======================================================================= +class CFuncMonitor : public CBaseBrush +{ +public: + void Spawn( void ); + void Precache( void ){ CBaseBrush::Precache(); } + void StartMessage( CBasePlayer *pPlayer ); + void ChangeCamera( string_t newcamera ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual BOOL IsFuncScreen( void ) { return TRUE; } + virtual STATE GetState( void ) { return pev->body ? STATE_ON:STATE_OFF; }; + virtual int ObjectCaps( void ); + BOOL OnControls( entvars_t *pevTest ); + CBaseEntity *pCamera; +}; +LINK_ENTITY_TO_CLASS( func_monitor, CFuncMonitor ); + +void CFuncMonitor::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "camera")) + { + pev->target = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "type")) + { + pev->impulse = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "mode")) + { + pev->skin = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseBrush::KeyValue( pkvd ); +} + +void CFuncMonitor :: Spawn() +{ + m_Material = Computer; + CBaseBrush::Spawn(); + + if(pev->spawnflags & SF_NOTSOLID)//make illusionary wall + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + UTIL_SetModel(ENT(pev), pev->model ); + + //enable monitor + if(pev->spawnflags & SF_START_ON)pev->body = 1; +} + +int CFuncMonitor :: ObjectCaps( void ) +{ + if (pev->impulse) + return (CBaseEntity::ObjectCaps() & ~FCAP_ACROSS_TRANSITION) | FCAP_IMPULSE_USE; + else return (CBaseEntity::ObjectCaps() & ~FCAP_ACROSS_TRANSITION); +} + +void CFuncMonitor::StartMessage( CBasePlayer *pPlayer ) +{ + //send monitor index + MESSAGE_BEGIN( MSG_ONE, gmsgAddScreen, NULL, pPlayer->pev ); + WRITE_BYTE( entindex() ); + MESSAGE_END(); + ChangeCamera( pev->target ); +} + +void CFuncMonitor::ChangeCamera( string_t newcamera ) +{ + pCamera = UTIL_FindEntityByTargetname( NULL, STRING( newcamera )); + + if( pCamera ) pev->sequence = pCamera->entindex(); + else if( pev->body ) pev->frame = 1; // noise if not found camera + + // member newcamera name + pev->target = newcamera; +} + +void CFuncMonitor :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if(IsLockedByMaster( useType )) return; + + if (useType == USE_TOGGLE) + { + if(pev->body) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON)pev->body = 1; + else if (useType == USE_OFF) + { + pev->body = 0; + pev->frame = 0; + } + else if (useType == USE_SET) + { + if(pActivator->IsPlayer()) + { + if( value ) UTIL_SetView(pActivator, iStringNull, 0 ); + else if(pev->body) + { + UTIL_SetView( pev->target, CAMERA_ON ); + m_pController = (CBasePlayer*)pActivator; + m_pController->m_pMonitor = this; + + if (m_pParent) + m_vecPlayerPos = m_pController->pev->origin - m_pParent->pev->origin; + else m_vecPlayerPos = m_pController->pev->origin; + } + } + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Mode: %s\n", GetStringForState( GetState()), pev->skin ? "Color" : "B&W"); + Msg( "Type: %s. Camera Name: %s\n", pev->impulse ? "Duke Nukem Style" : "Half-Life 2 Style", STRING( pev->target) ); + } +} + +BOOL CFuncMonitor :: OnControls( entvars_t *pevTest ) +{ + if(m_pParent && ((m_vecPlayerPos + m_pParent->pev->origin) - pevTest->origin).Length() <= 30) + return TRUE; + else if((m_vecPlayerPos - pevTest->origin).Length() <= 30 ) + return TRUE; + return FALSE; +} + +//======================================================================= +// func_teleport - classic XASH portal +//======================================================================= +class CFuncTeleport : public CFuncMonitor +{ +public: + void Spawn( void ); + void Touch ( CBaseEntity *pOther ); + void KeyValue( KeyValueData *pkvd ); + void StartMessage( CBasePlayer *pPlayer ); +}; +LINK_ENTITY_TO_CLASS( func_portal, CFuncTeleport ); +LINK_ENTITY_TO_CLASS( trigger_teleport, CFuncTeleport ); + +void CFuncTeleport :: Spawn( void ) +{ + pev->solid = SOLID_TRIGGER; + pev->movetype = MOVETYPE_NONE; + UTIL_SetModel(ENT(pev), pev->model); + if (FClassnameIs(pev, "func_portal")) //portal mode + { + pev->impulse = 0;//can't use teleport + pev->body = 1;//enabled + } + else SetBits( pev->effects, EF_NODRAW );//classic mode +} + +void CFuncTeleport::StartMessage( CBasePlayer *pPlayer ) +{ + //send portal index + MESSAGE_BEGIN( MSG_ONE, gmsgAddPortal, NULL, pPlayer->pev ); + WRITE_BYTE( entindex() ); + MESSAGE_END(); + ChangeCamera( pev->target ); +} +void CFuncTeleport :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "landmark")) + { + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "firetarget")) + { + pev->netname = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CFuncTeleport :: Touch( CBaseEntity *pOther ) +{ + entvars_t* pevToucher = pOther->pev; + CBaseEntity *pTarget = NULL; + + // only teleport monsters or clients or projectiles + if( !FBitSet( pevToucher->flags, FL_CLIENT|FL_MONSTER|FL_PROJECTILE ) && !pOther->IsPushable()) + return; + if( IsLockedByMaster( pOther )) return; + + pTarget = UTIL_FindEntityByTargetname( pTarget, STRING(pev->target) ); + if ( !pTarget ) return; + + CBaseEntity *pLandmark = UTIL_FindEntityByTargetname( NULL, STRING(pev->message) ); + if ( pLandmark ) + { + Vector vecOriginOffs = pTarget->pev->origin - pLandmark->pev->origin; + + // do we need to rotate the entity? + if ( pLandmark->pev->angles != pTarget->pev->angles ) + { + Vector vecVA; + float ydiff = pTarget->pev->angles.y - pLandmark->pev->angles.y; + + // set new angle to face + pOther->pev->angles.y += ydiff; + if (pOther->IsPlayer()) + { + pOther->pev->angles.x = pOther->pev->v_angle.x; + pOther->pev->fixangle = TRUE; + } + + // set new velocity + vecVA = UTIL_VecToAngles(pOther->pev->velocity); + vecVA.y += ydiff; + UTIL_MakeVectors(vecVA); + pOther->pev->velocity = gpGlobals->v_forward * pOther->pev->velocity.Length(); + pOther->pev->velocity.z = -pOther->pev->velocity.z; + + // set new origin + Vector vecPlayerOffs = pOther->pev->origin - pLandmark->pev->origin; + vecVA = UTIL_VecToAngles(vecPlayerOffs); + UTIL_MakeVectors(vecVA); + vecVA.y += ydiff; + UTIL_MakeVectors(vecVA); + Vector vecPlayerOffsNew = gpGlobals->v_forward * vecPlayerOffs.Length(); + vecPlayerOffsNew.z = -vecPlayerOffsNew.z; + vecOriginOffs = vecOriginOffs + vecPlayerOffsNew - vecPlayerOffs; + } + UTIL_SetOrigin( pOther, pOther->pev->origin + vecOriginOffs ); + } + else + { + Vector tmp = pTarget->pev->origin; + + if ( pOther->IsPlayer()) tmp.z -= pOther->pev->mins.z; // make origin adjustments + tmp.z++; + UTIL_SetOrigin( pOther, tmp ); + + pOther->pev->angles = pTarget->pev->angles; + pOther->pev->velocity = g_vecZero; + if ( pOther->IsPlayer() ) + { + pOther->pev->v_angle = pTarget->pev->angles; + pOther->pev->fixangle = TRUE; + } + } + + ChangeCamera( pev->target );//update PVS + pevToucher->flags &= ~FL_ONGROUND; + pevToucher->fixangle = TRUE; + + UTIL_FireTargets( pev->netname, pOther, this, USE_TOGGLE );//fire target +} + +//======================================================================= +// ANGULAR MOVING BRUSHES +//======================================================================= +//======================================================================= +// func_rotate - standard not moving wall. affect to physics +//======================================================================= +class CFuncRotating : public CBaseBrush +{ +public: + void Spawn( void ); + void Precache( void ); + void KeyValue( KeyValueData* pkvd); + void Touch ( CBaseEntity *pOther ); + void Blocked( CBaseEntity *pOther ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Think( void ); + void PostActivate ( void ); + + void RampPitchVol ( void ); + void SetSpeed( float newspeed ); + void SetAngularImpulse( float impulse ); + float flDegree; +}; +LINK_ENTITY_TO_CLASS( func_rotating, CFuncRotating ); + +void CFuncRotating :: KeyValue( KeyValueData* pkvd) +{ + if (FStrEq(pkvd->szKeyName, "spintime")) + { + pev->frags = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseBrush::KeyValue( pkvd ); +} + +void CFuncRotating :: Precache( void ) +{ + CBaseBrush::Precache(); + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + + switch (m_sounds) + { + case 1: + pev->noise3 = UTIL_PrecacheSound ("materials/fans/fan1.wav"); + break; + case 2: + pev->noise3 = UTIL_PrecacheSound ("materials/fans/fan2.wav"); + break; + case 3: + pev->noise3 = UTIL_PrecacheSound ("materials/fans/fan3.wav"); + break; + case 4: + pev->noise3 = UTIL_PrecacheSound ("materials/fans/fan4.wav"); + break; + case 5: + pev->noise3 = UTIL_PrecacheSound ("materials/fans/fan5.wav"); + break; + case 0: + pev->noise3 = UTIL_PrecacheSound ("common/null.wav"); + break; + default: + pev->noise3 = UTIL_PrecacheSound(m_sounds); + break; + } +} + +void CFuncRotating :: Spawn( void ) +{ + Precache(); + CBaseBrush::Spawn(); + + if ( pev->spawnflags & 64 ) + { + pev->solid = SOLID_NOT; + pev->contents = CONTENTS_NONE; + pev->movetype = MOVETYPE_PUSH; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + + SetBits (pFlags, PF_ANGULAR);//enetity has angular velocity + m_iState = STATE_OFF; + m_pitch = PITCH_NORM - 1; + pev->dmg_save = 0;//cur speed is NULL + pev->button = pev->speed;//member start speed + AxisDir(); + + UTIL_SetOrigin(this, pev->origin); + UTIL_SetModel( ENT(pev), pev->model ); +} + +void CFuncRotating :: Touch ( CBaseEntity *pOther ) +{ + // don't hurt toucher - just apply velocity him + entvars_t *pevOther = pOther->pev; + pevOther->velocity = (pevOther->origin - VecBModelOrigin(pev) ).Normalize() * pev->dmg; +} + +void CFuncRotating :: Blocked( CBaseEntity *pOther ) +{ + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE)m_pParent->Blocked( pOther ); + + UTIL_AssignAngles(this, pev->angles ); + if ( gpGlobals->time < m_flBlockedTime)return; + m_flBlockedTime = gpGlobals->time + 0.1; + + if((m_iState == STATE_OFF || m_iState == STATE_TURN_OFF) && pev->avelocity.Length() < 350) + { + SetAngularImpulse(-(pev->dmg_save + RANDOM_FLOAT( 5, 10))); + return; + } + + if(pOther->TakeDamage( pev, pev, fabs(pev->speed), DMG_CRUSH ))//blocked entity died ? + { + SetAngularImpulse(-(pev->dmg_save + RANDOM_FLOAT( 10, 20))); + UTIL_FireTargets( pev->target, this, this, USE_SET );//damage + } + else if (RANDOM_LONG(0,1)) + { + SetSpeed( 0 );//fan damaged - disable it + UTIL_FireTargets( pev->target, this, this, USE_SET ); + //fan damaged( we can use counter and master for bloked broken fan) + } + if(!pOther->IsPlayer() && !pOther->IsMonster())//crash fan + { + if(IsBreakable())//if set strength - give damage myself + CBaseBrush::TakeDamage( pev, pev, fabs(pev->speed/100), DMG_CRUSH );//if we give damage + } +} + +void CFuncRotating :: PostActivate () +{ + if ( pev->spawnflags & SF_START_ON )SetSpeed( pev->speed ); + ClearBits( pev->spawnflags, SF_START_ON); + + if(m_iState == STATE_ON )//restore sound if needed + EMIT_SOUND_DYN( ENT(pev), CHAN_STATIC, (char *)STRING(pev->noise3), m_flVolume, ATTN_NORM, SND_CHANGE_PITCH | SND_CHANGE_VOL, m_pitch); + Msg("CFuncRotating:PostActivate()\n" ); + SetNextThink( 0.05 ); //force to think +} + +void CFuncRotating :: RampPitchVol ( void ) +{ + float fpitch; + int pitch; + float speedfactor = fabs(pev->dmg_save/pev->button); + + m_flVolume = speedfactor; //slowdown volume ramps down to 0 + fpitch = PITCHMIN + (PITCHMAX - PITCHMIN) * speedfactor; + + pitch = (int)fpitch; + if (pitch == PITCH_NORM)pitch = PITCH_NORM-1; + + //normalize volume and pitch + if(m_flVolume > 1.0)m_flVolume = 1.0; + if(m_flVolume < 0.0)m_flVolume = 0.0; + if(pitch < PITCHMIN) pitch = PITCHMIN; + if(pitch > 255) pitch = 255; + m_pitch = pitch;//refresh pitch + + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, (char *)STRING(pev->noise3), m_flVolume, ATTN_NORM, SND_CHANGE_PITCH | SND_CHANGE_VOL, m_pitch); +} + +void CFuncRotating :: SetSpeed( float newspeed ) +{ + float speedfactor = fabs(pev->dmg_save/pev->speed);//speedfactor + pev->armortype = gpGlobals->time; //starttime + pev->armorvalue = newspeed - pev->dmg_save; //speed offset + pev->scale = pev->dmg_save; //cur speed + + //any speed change is turn on + //may be it's wrong, but nice working :) + if((pev->dmg_save > 0 && newspeed < 0) || (pev->dmg_save < 0 && newspeed > 0))//detect fast reverse + { + if(m_iState != STATE_OFF)pev->dmg_take = pev->frags * speedfactor;//fast reverse + else pev->dmg_take = pev->frags;//get normal time + m_iState = STATE_TURN_ON; + } + else if(newspeed == 0) + { + m_iState = STATE_TURN_OFF; + pev->dmg_take = pev->frags * 2.5 * speedfactor;//spin down is longer + } + else //just change speed, not zerocrossing + { + m_iState = STATE_TURN_ON; + pev->dmg_take = pev->frags;//get normal time + } + SetNextThink( 0.05 );//force to think +} + +void CFuncRotating :: SetAngularImpulse( float impulse ) +{ + if( fabs(impulse) < 20)pev->scale = fabs(impulse / MAX_AVELOCITY); + else pev->scale = 0.01;//scale factor + + m_iState = STATE_OFF;//impulse disable fan + pev->dmg_save = pev->dmg_save + impulse;//add our impulse to curspeed + + //normalize avelocity + if(pev->dmg_save > MAX_AVELOCITY)pev->dmg_save = MAX_AVELOCITY; + pev->max_health = MatFrictionTable( m_Material);//set friction coefficient + + SetNextThink(0.05);//pushable entity must thinking for apply velocity +} + +void CFuncRotating :: Think( void ) +{ + if(m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF) + { + flDegree = (gpGlobals->time - pev->armortype)/pev->dmg_take; + + if (flDegree >= 1)//full spin + { + if(m_iState == STATE_TURN_ON) + { + m_iState = STATE_ON; + pev->dmg_save = pev->speed;//normalize speed + UTIL_SetAvelocity(this, pev->movedir * pev->speed);//set normalized avelocity + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, (char *)STRING(pev->noise3), m_flVolume, ATTN_NORM, SND_CHANGE_PITCH | SND_CHANGE_VOL, m_pitch); + UTIL_FireTargets( pev->target, this, this, USE_ON );//fan is full on + } + if(m_iState == STATE_TURN_OFF) + { + m_iState = STATE_OFF; + if (pev->movedir == Vector(0,0,1)) + SetAngularImpulse(RANDOM_FLOAT(3.9, 6.8) * -pev->dmg_save); + //set small negative impulse for pretty phys effect :) + } + } + else + { + //calc new speed every frame + pev->dmg_save = pev->scale + pev->armorvalue * flDegree; + UTIL_SetAvelocity(this, pev->movedir * pev->dmg_save); + RampPitchVol(); //play pitched sound + } + } + if(m_iState == STATE_OFF)//apply impulse and friction + { + if(pev->dmg_save > 0)pev->dmg_save -= pev->max_health * pev->scale + 0.01; + else if(pev->dmg_save < 0)pev->dmg_save += pev->max_health * pev->scale + 0.01; + UTIL_SetAvelocity(this, pev->movedir * pev->dmg_save); + RampPitchVol(); //play pitched sound + } + //ALERT(at_console, "pev->avelocity %.2f %.2f %.2f\n", pev->avelocity.x, pev->avelocity.y, pev->avelocity.z); + SetNextThink( 0.05 ); + + if(pev->avelocity.Length() < 0.5)//watch for speed + { + UTIL_SetAvelocity(this, g_vecZero);//stop + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, (char *)STRING(pev->noise3), 0, 0, SND_STOP, m_pitch); + DontThink();//break thinking + UTIL_FireTargets( pev->target, this, this, USE_OFF );//fan is full stopped + } +} + +void CFuncRotating :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if(IsLockedByMaster( useType )) return; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_TURN_OFF || m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if (useType == USE_ON)SetSpeed( pev->speed ); + else if(useType == USE_OFF)SetSpeed( 0 ); + else if (useType == USE_SET)//reverse speed + { + //write new speed + if(value) + { + if(pev->speed < 0) + pev->speed = -value; + if(pev->speed > 0) + pev->speed = value; + } + else pev->speed = -pev->speed;//just reverse + //apply speed immediately if fan no off or not turning off + if(m_iState != STATE_OFF && m_iState != STATE_TURN_OFF)SetSpeed( pev->speed ); + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + ALERT(at_console, "State: %s, CurSpeed %.1f\n", GetStringForState( GetState()), pev->dmg_save); + ALERT(at_console, "SpinTime: %.2f, MaxSpeed %.f\n", pev->frags, pev->speed); + } +} + +//======================================================================= +// func_pendulum - swinging brush +//======================================================================= +class CPendulum : public CBaseMover +{ +public: + void Spawn ( void ); + void Precache( void ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Think( void ); + void Blocked( CBaseEntity *pOther ); + void KeyValue( KeyValueData *pkvd ); + void Touch( CBaseEntity *pOther ); + void SetImpulse( float speed ); + void SetSwing( float speed ); + void PostActivate( void ); + + int dir;//FIXME +}; +LINK_ENTITY_TO_CLASS( func_pendulum, CPendulum ); + +void CPendulum :: KeyValue( KeyValueData *pkvd ) +{ + if( FStrEq( pkvd->szKeyName, "distance" )) + { + pev->scale = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if( FStrEq( pkvd->szKeyName, "damp" )) + { + pev->max_health = atof( pkvd->szValue ) * 0.01f; + if( pev->max_health > 0.01f ) pev->max_health = 0.01f; + pkvd->fHandled = TRUE; + } + else CBaseBrush::KeyValue( pkvd ); +} + +void CPendulum :: Precache( void ) +{ + CBaseBrush::Precache(); + int m_sounds = UTIL_LoadSoundPreset( m_iMoveSound ); + + switch (m_sounds) + { + case 1: + pev->noise3 = UTIL_PrecacheSound( "materials/swing/swing1.wav" ); + break; + case 2: + pev->noise3 = UTIL_PrecacheSound( "materials/swing/swing2.wav" ); + break; + case 0: + pev->noise3 = UTIL_PrecacheSound( "common/null.wav" ); + break; + default: + pev->noise3 = UTIL_PrecacheSound( m_sounds ); + break; + } +} + +void CPendulum :: Spawn( void ) +{ + Precache(); + CBaseBrush::Spawn(); + + if ( pev->spawnflags & SF_NOTSOLID ) + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_PUSH; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + + SetBits (pFlags, PF_ANGULAR); + dir = 1; // evil stuff... + m_iState = STATE_OFF; + pev->dmg_save = 0; //cur speed is NULL + + // NOTE: I'm not have better idea than it + // So, field "angles" setting movedir only + // Turn your brush into your fucking worldcraft! + UTIL_AngularVector( this ); + + UTIL_SetOrigin( this, pev->origin ); + UTIL_SetModel( ENT( pev ), pev->model ); +} + +void CPendulum :: PostActivate( void ) +{ + if ( pev->spawnflags & SF_START_ON )SetSwing( pev->speed ); + ClearBits( pev->spawnflags, SF_START_ON); + + if(m_iState == STATE_ON )//restore sound if needed + SetNextThink( 0.05 );//force to think +} + +void CPendulum :: Touch ( CBaseEntity *pOther ) +{ + // don't hurt toucher - just apply velocity him + entvars_t *pevOther = pOther->pev; + pevOther->velocity = (pevOther->origin - VecBModelOrigin(pev) ).Normalize() * pev->dmg; +} + +void CPendulum::Blocked( CBaseEntity *pOther ) +{ + // what can do pendulum on blocking ? + // Nothing :) + pev->armortype = gpGlobals->time; +} + +void CPendulum :: SetImpulse( float speed ) +{ + if( dir == pev->impulse ) + return; + dir = pev->impulse; + + SetSwing( speed ); +} + +void CPendulum :: SetSwing( float speed ) +{ + if(m_iState == STATE_OFF) + pev->armorvalue = speed; //save our speed + else pev->armorvalue = pev->armorvalue + speed;//apply impulse + + //silence normalize speed if needed + if(pev->armorvalue > MAX_AVELOCITY)pev->armorvalue = MAX_AVELOCITY; + + //normalize speed at think time + //calc min distance at this speed + float factor = MAX_AVELOCITY * gpGlobals->frametime; // max avelocity * think time + float mindist = (pev->armorvalue/factor) * 2;//full minimal dist + if(mindist > pev->scale) + { + pev->scale = mindist;//set minimal distance for this speed + if(m_iState == STATE_OFF) + { + Msg( "\n======/Xash SmartFiled System/======\n\n"); + Msg( "Warning! %s has too small distance for current speed!\nSmart filed normalize distance to %.f\n", STRING(pev->classname), pev->scale); + SHIFT; + } + } + + if( m_iState == STATE_OFF ) + pev->dmg_take = pev->scale * 0.5; // calc half distance + else pev->dmg_take = pev->dmg_take + sqrt( speed ); + pev->dmg_save = pev->armorvalue; // at center speed is maximum + + //random choose starting dir + if(m_iState == STATE_OFF) + { + if( RANDOM_LONG( 0, 1 )) + pev->impulse = -1; // backward + else pev->impulse = 1; // forward + m_iState = STATE_ON; + } + SetNextThink( 0.01f ); // force to think +} + +void CPendulum :: Think( void ) +{ + float m_flDegree = (gpGlobals->time - pev->armortype)/pev->frags; + float m_flAngle = UTIL_CalcDistance(pev->angles); + float m_flAngleOffset = pev->dmg_take - m_flAngle; + float m_flStep = ((pev->armorvalue * 0.01)/(pev->dmg_take * 0.01)) / M_PI; + float m_damping = pev->max_health; + if(m_iState == STATE_TURN_OFF) m_damping = 0.09;//hack to disable pendulum + + if(m_flDegree >= 1 && m_flAngleOffset <= 0.5)//extremum point + { + //recalc time + pev->armortype = gpGlobals->time; + pev->frags = pev->dmg_take/pev->armorvalue; + pev->impulse = -pev->impulse;//change movedir + } + if(pev->avelocity.Length() > pev->armorvalue * 0.6 && m_damping)//apply damp + { + pev->armorvalue -= pev->armorvalue * m_damping; + pev->dmg_take -= pev->dmg_take * m_damping; + } + + if(pev->avelocity.Length() > pev->armorvalue * 0.95 && pev->dmg_take > 5.0)//zerocrossing + { + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, (char *)STRING(pev->noise3), m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM); + UTIL_FireTargets( pev->target, this, this, USE_OFF ); + } + + pev->dmg_save = pev->armorvalue * (m_flAngleOffset / pev->dmg_take); + UTIL_SetAvelocity(this, pev->movedir * pev->dmg_save * pev->impulse); + + SetNextThink(0.01); + if(pev->avelocity.Length() < 0.5 && pev->dmg_take < 2.0)//watch for speed and dist + { + UTIL_SetAvelocity(this, g_vecZero);//stop + UTIL_AssignAngles(this, pev->angles ); + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, (char *)STRING(pev->noise3), 0, 0, SND_STOP, m_pitch); + DontThink();//break thinking + UTIL_FireTargets( pev->target, this, this, USE_OFF );//pendulum is full stopped + m_iState = STATE_OFF; + } +} + +void CPendulum :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if( IsLockedByMaster( useType )) return; + + if( useType == USE_TOGGLE ) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if( useType == USE_ON ) SetSwing( pev->speed ); + else if( useType == USE_OFF ) m_iState = STATE_TURN_OFF; + else if( useType == USE_SET ) + { + if( value ) pev->speed = value; + else SetImpulse( pev->speed );//any idea ??? + } + else if( useType == USE_RESET ) SetImpulse( pev->speed ); + else if(useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, CurSpeed %.1f\n", GetStringForState( GetState()), pev->dmg_save); + ALERT(at_console, "Distance: %.2f, Swing Time %.f\n", pev->dmg_take, pev->frags); + } +} + +//======================================================================= +// func_clock - simply rotating clock from Quake1.Scourge Of Armagon +//======================================================================= +class CFuncClock : public CBaseLogic +{ +public: + void Spawn ( void ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + void Think( void ); + void PostActivate( void ); + void KeyValue( KeyValueData *pkvd ); + Vector curtime, finaltime; + int bellcount;//calc hours +}; +LINK_ENTITY_TO_CLASS( func_clock, CFuncClock ); + +void CFuncClock :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "type")) + { + switch(atoi( pkvd->szValue )) + { + case 1: pev->impulse = MINUTES; break; //minutes + case 2: pev->impulse = HOURS; break; //hours + case 0: //default: + default: pev->impulse = SECONDS; break; //seconds + } + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "curtime")) + { + UTIL_StringToVector( curtime, pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "event")) + { + pev->netname = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } +} + +void CFuncClock :: Spawn ( void ) +{ + pev->solid = SOLID_NOT; + UTIL_SetModel( ENT(pev), pev->model ); + + // NOTE: I'm not have better idea than it + // So, field "angles" setting movedir only + // Turn your brush into your fucking worldcraft! + UTIL_AngularVector( this ); + SetBits( pFlags, PF_ANGULAR ); + + if( pev->impulse == HOURS ) //do time operations + { + // normalize our time + if( curtime.x > 11 ) curtime.x = 0; + if( curtime.y > 59 ) curtime.y = 0; + if( curtime.z > 59 ) curtime.z = 0; + + // member full hours + pev->team = curtime.x; + + // calculate seconds + finaltime.z = curtime.z * (SECONDS / 60); // seconds + finaltime.y = curtime.y * (MINUTES / 60) + finaltime.z; + finaltime.x = curtime.x * (HOURS / 12) + finaltime.y; + } +} + +void CFuncClock :: PostActivate( void ) +{ + // NOTE: We should be sure what all entities has be spawned + // else we called this from Activate() + + if( pev->frags ) return; + if( pev->impulse == HOURS && curtime != g_vecZero ) // it's hour entity and time set + { + // try to find minutes and seconds entity + CBaseEntity *pEntity = NULL; + while( pEntity = UTIL_FindEntityInSphere( pEntity, pev->origin, pev->size.z )) + { + if( FClassnameIs( pEntity, "func_clock" )) + { + //write start hours, minutes and seconds + if( pEntity->pev->impulse == HOURS ) pEntity->pev->dmg_take = finaltime.x; + if( pEntity->pev->impulse == MINUTES ) pEntity->pev->dmg_take = finaltime.y; + if( pEntity->pev->impulse == SECONDS ) pEntity->pev->dmg_take = finaltime.z; + } + } + } + pev->frags = 1; // clock init + m_iState = STATE_ON;// always on + + SetNextThink( 0 ); // force to think +} + +void CFuncClock :: Think ( void ) +{ + float seconds, ang, pos; + + seconds = gpGlobals->time + pev->dmg_take; + pos = seconds / pev->impulse; + pos = pos - floor( pos ); + ang = 360 * pos; + + UTIL_SetAngles( this, pev->movedir * ang ); + + if(pev->impulse == HOURS) // play bell sound + { + pev->button = pev->angles.Length() / 30; // half hour + if ( pev->team != pev->button ) // new hour + { + // member new hour + bellcount = pev->team = pev->button; + if( bellcount == 0 ) bellcount = 12; // merge for 0.00.00 + UTIL_FireTargets( pev->netname, this, this, USE_SET, bellcount ); // send hours info + UTIL_FireTargets( pev->netname, this, this, USE_ON );//activate bell or logic_generator + } + } + + SetNextThink( 1 ); // refresh at one second +} + +//======================================================================= +// func_healthcharger - brush healthcharger +//======================================================================= +class CWallCharger : public CBaseBrush +{ +public: + void Spawn( ); + void Precache( void ); + void Think (void); + void KeyValue( KeyValueData* pkvd); + virtual int ObjectCaps( void ) + { + int flags = CBaseBrush:: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; + if(m_iState == STATE_DEAD) return flags; + return flags | FCAP_CONTINUOUS_USE; + } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + float m_flNextCharge; + float m_flSoundTime; +}; +LINK_ENTITY_TO_CLASS(func_healthcharger, CWallCharger); +LINK_ENTITY_TO_CLASS(func_recharge, CWallCharger); + +void CWallCharger :: KeyValue( KeyValueData* pkvd) +{ + if (FStrEq(pkvd->szKeyName, "chargetime"))//affect only in multiplayer + { + pev->armortype = atof(pkvd->szValue); + if(pev->armortype <= 0 )pev->armortype = 1; + pkvd->fHandled = TRUE; + } + else CBaseBrush::KeyValue( pkvd ); +} + +void CWallCharger::Spawn() +{ + Precache(); + CBaseBrush::Spawn(); + + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + + UTIL_SetOrigin(this, pev->origin); + UTIL_SetSize(pev, pev->mins, pev->maxs); + UTIL_SetModel(ENT(pev), pev->model ); + + m_iState = STATE_OFF; + pev->frame = 0; + + //set capacity + if (FClassnameIs(this, "func_recharge")) + pev->frags = SUIT_CHARGER_CAP; + else pev->frags = HEATH_CHARGER_CAP; + + pev->dmg_save = pev->frags; +} + +void CWallCharger::Precache() +{ + CBaseBrush::Precache(); + if (FClassnameIs(this, "func_recharge")) + { + UTIL_PrecacheSound("items/suitcharge1.wav"); + UTIL_PrecacheSound("items/suitchargeno1.wav"); + UTIL_PrecacheSound("items/suitchargeok1.wav"); + } + else + { + UTIL_PrecacheSound("items/medshot4.wav"); + UTIL_PrecacheSound("items/medshotno1.wav"); + UTIL_PrecacheSound("items/medcharge4.wav"); + } +} + +void CWallCharger::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if(IsLockedByMaster( useType )) return; + + if(useType == USE_SHOWINFO)//show info + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, Volume %.1f\n", GetStringForState( GetState()), m_flVolume ); + ALERT(at_console, "Texture frame: %.f. ChargeLevel: %.f\n", pev->frame, pev->dmg_save); + } + else if(pActivator && pActivator->IsPlayer())//take health + { + if (pev->dmg_save <= 0) + { + if (m_iState == STATE_IN_USE || m_iState == STATE_ON) + { + pev->frame = 1; + if (FClassnameIs(this, "func_recharge")) + STOP_SOUND( ENT(pev), CHAN_STATIC, "items/suitcharge1.wav" ); + else STOP_SOUND( ENT(pev), CHAN_STATIC, "items/medcharge4.wav" ); + m_iState = STATE_DEAD;//recharge in multiplayer + if(IsMultiplayer()) SetNextThink( 3);//delay before recahrge + else DontThink(); + } + } + + if((m_iState == STATE_DEAD) || (!(((CBasePlayer *)pActivator)->m_iHideHUD & ITEM_SUIT))) + { + if (m_flSoundTime <= gpGlobals->time) + { + m_flSoundTime = gpGlobals->time + 0.62; + if (FClassnameIs(this, "func_recharge")) + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/suitchargeno1.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + else EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/medshotno1.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + + } + return; + } + SetNextThink( 0.25);//time before disable + + if (m_flNextCharge >= gpGlobals->time) return; + + if (m_iState == STATE_OFF) + { + m_iState = STATE_ON; + if (FClassnameIs(this, "func_recharge")) + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/suitchargeok1.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + else EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/medshot4.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + + m_flSoundTime = 0.56 + gpGlobals->time; + } + if (m_iState == STATE_ON && m_flSoundTime <= gpGlobals->time) + { + m_iState = STATE_IN_USE; + if (FClassnameIs(this, "func_recharge")) + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/suitcharge1.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + else EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/medcharge4.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + } + + if (FClassnameIs(this, "func_recharge")) + { + if (m_hActivator->pev->armorvalue < 100) + { + UTIL_FireTargets( pev->target, this, this, USE_OFF, pev->dmg_save );//decrement counter + pev->dmg_save--; + m_hActivator->pev->armorvalue++; + } + } + else + { + if ( pActivator->TakeHealth(1, DMG_GENERIC )) + { + UTIL_FireTargets( pev->target, this, this, USE_OFF, pev->dmg_save );//decrement counter + pev->dmg_save--; + } + } + m_flNextCharge = gpGlobals->time + 0.1; + } +} + +void CWallCharger :: Think( void ) +{ + if (m_iState == STATE_IN_USE || m_iState == STATE_ON) + { + if (FClassnameIs(this, "func_recharge")) + STOP_SOUND( ENT(pev), CHAN_STATIC, "items/suitcharge1.wav" ); + else STOP_SOUND( ENT(pev), CHAN_STATIC, "items/medcharge4.wav" ); + + m_iState = STATE_OFF; + DontThink(); + return; + } + if(m_iState == STATE_DEAD && IsMultiplayer())//recharge + { + pev->dmg_save++;//ressurection + UTIL_FireTargets( pev->target, this, this, USE_ON, pev->dmg_save );//increment counter + if(pev->dmg_save >= pev->frags) + { + //take full charge + if (FClassnameIs(this, "func_recharge")) + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/suitchargeok1.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + else EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "items/medshot4.wav", m_flVolume, ATTN_IDLE, SND_CHANGE_VOL, PITCH_NORM ); + pev->dmg_save = pev->frags;//normalize + pev->frame = 0;//enable + m_iState = STATE_OFF;//can use + DontThink(); + return; + } + } + SetNextThink( pev->armortype ); +} + +//======================================================================= +// func_button - standard linear button from Quake1 +//======================================================================= +class CBaseButton : public CBaseMover +{ +public: + void Spawn( void ); + void PostSpawn( void ); + void Precache( void ); + virtual void KeyValue( KeyValueData* pkvd); + virtual int ObjectCaps( void ) + { + int flags = CBaseMover:: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; + if(m_iState == STATE_DEAD) return flags; + return flags | FCAP_IMPULSE_USE | FCAP_ONLYDIRECT_USE; + } + void EXPORT ButtonUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT ShowInfo ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT ButtonTouch( CBaseEntity *pOther ); + void EXPORT ButtonReturn( void ); + void EXPORT ButtonDone( void ); + void ButtonActivate( void ); +}; +LINK_ENTITY_TO_CLASS( func_button, CBaseButton ); + +void CBaseButton::KeyValue( KeyValueData *pkvd ) +{ + //rename standard fields for button + if (FStrEq(pkvd->szKeyName, "locksound")) + { + m_iStopSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "pushsound")) + { + m_iStartSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseMover::KeyValue( pkvd ); +} + +void CBaseButton::Precache( void ) +{ + CBaseBrush::Precache();//precache damage sound + + int m_sounds = UTIL_LoadSoundPreset(m_iStartSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise = UTIL_PrecacheSound ("materials/buttons/blip1.wav");break; + case 2: pev->noise = UTIL_PrecacheSound ("materials/buttons/blip2.wav");break; + case 3: pev->noise = UTIL_PrecacheSound ("materials/buttons/blip3.wav");break; + case 4: pev->noise = UTIL_PrecacheSound ("materials/buttons/blip4.wav");break; + case 5: pev->noise = UTIL_PrecacheSound ("materials/buttons/blip5.wav");break; + case 6: pev->noise = UTIL_PrecacheSound ("materials/buttons/blip6.wav");break; + case 0: pev->noise = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + if (!FStringNull(m_sMaster))//button has master + { + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + switch (m_sounds)//load locked sounds + { + case 1: pev->noise3 = UTIL_PrecacheSound ("materials/buttons/locked1.wav");break; + case 2: pev->noise3 = UTIL_PrecacheSound ("materials/buttons/locked2.wav");break; + case 0: pev->noise3 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise3 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + } +} + +void CBaseButton::Spawn( ) +{ + Precache(); + CBaseBrush::Spawn(); + + if(pev->spawnflags & SF_NOTSOLID)//not solid button + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + pev->contents = CONTENTS_NONE; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + UTIL_SetModel(ENT(pev), pev->model); + + //determine work style + if(m_iMode == 0)//classic HL button - only USE + { + SetUse ( ButtonUse ); + SetTouch ( NULL ); + } + if(m_iMode == 1)//classic QUAKE button - only TOUCH + { + SetUse ( ShowInfo );//show info only + SetTouch ( ButtonTouch ); + } + if(m_iMode == 2)//combo button - USE and TOUCH + { + SetUse ( ButtonUse ); + SetTouch ( ButtonTouch ); + } + + //as default any button is toggleable, but if mapmaker set waittime > 0 + //button will transformed into timebased button + //if waittime is -1 - button forever stay pressed + if(m_flWait == 0) pev->impulse = 1;//toggleable button + + if (m_flLip == 0) m_flLip = 4;//standart offset from Quake1 + + m_iState = STATE_OFF;//disable at spawn + UTIL_LinearVector( this );//movement direction + + m_vecPosition1 = pev->origin; + m_vecPosition2 = m_vecPosition1 + (pev->movedir * (fabs( pev->movedir.x * (pev->size.x-2) ) + fabs( pev->movedir.y * (pev->size.y-2) ) + fabs( pev->movedir.z * (pev->size.z-2) ) - m_flLip)); + + // Is this a non-moving button? + if( pev->speed == 0 )m_vecPosition2 = m_vecPosition1; +} + +void CBaseButton :: PostSpawn( void ) +{ + if (m_pParent) m_vecPosition1 = pev->origin - m_pParent->pev->origin; + else m_vecPosition1 = pev->origin; + m_vecPosition2 = m_vecPosition1 + (pev->movedir * (fabs( pev->movedir.x * (pev->size.x-2) ) + fabs( pev->movedir.y * (pev->size.y-2) ) + fabs( pev->movedir.z * (pev->size.z-2) ) - m_flLip)); + + // Is this a non-moving button? + if ( pev->speed == 0 ) m_vecPosition2 = m_vecPosition1; +} + +void CBaseButton :: ShowInfo ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(useType == USE_SHOWINFO)//show info + { + DEBUGHEAD; + ALERT(at_console, "State: %s, Speed %.2f\n", GetStringForState( GetState()), pev->speed ); + ALERT(at_console, "Texture frame: %.f. WaitTime: %.2f\n", pev->frame, m_flWait); + } +} + +void CBaseButton :: ButtonUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator;//save our activator + + if(IsLockedByMaster( useType ))//passed only USE_SHOWINFO + { + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise3), 1, ATTN_NORM); + return; + } + if(useType == USE_SHOWINFO)//show info + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, Speed %.2f\n", GetStringForState( GetState()), pev->speed ); + ALERT(at_console, "Texture frame: %.f. WaitTime: %.2f\n", pev->frame, m_flWait); + } + else if(m_iState != STATE_DEAD)//activate button + { + //NOTE: STATE_DEAD is better method for simulate m_flWait -1 without fucking SetThink() + if(pActivator && pActivator->IsPlayer())//code for player + { + if(pev->impulse == 1)//toggleable button + { + if (m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF )return;//buton in-moving + else if( m_iState == STATE_ON )//button is active, disable + { + ButtonReturn(); + } + else if( m_iState == STATE_OFF )//activate + { + ButtonActivate(); + } + } + else //time based button + { + //can activate only disabled button + if( m_iState == STATE_OFF ) ButtonActivate(); + else return;//does nothing :) + } + } + else //activate button from other entity + { + //NOTE: Across activation just fire output without changes + UTIL_FireTargets( pev->target, pActivator, pCaller, useType, value ); + } + } +} + +void CBaseButton:: ButtonTouch( CBaseEntity *pOther ) +{ + //make delay before retouching + if ( gpGlobals->time < m_flBlockedTime) return; + m_flBlockedTime = gpGlobals->time + 1.0; + + if(pOther->IsPlayer())ButtonUse ( pOther, this, USE_TOGGLE, 1 );//player always sending 1 +} + +void CBaseButton::ButtonActivate( ) +{ + ASSERT(m_iState == STATE_OFF); + m_iState = STATE_TURN_ON; + + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise), 1, ATTN_NORM); + + if(pev->speed) + { + SetMoveDone( ButtonDone ); + LinearMove( m_vecPosition2, pev->speed); + } + else ButtonDone();//immediately switch +} + +void CBaseButton::ButtonDone( void ) +{ + if(m_iState == STATE_TURN_ON)//turn on + { + m_iState = STATE_ON; + pev->frame = 1; // change skin + + if(pev->impulse) + UTIL_FireTargets( pev->target, m_hActivator, this, USE_ON, 0 );//fire target + else UTIL_FireTargets( pev->target, m_hActivator, this, USE_TOGGLE, 0 );//fire target + + if(m_flWait == -1) + { + m_iState = STATE_DEAD;//keep button in this position + return; + } + + if(pev->impulse == 0)//time base button + { + SetThink( ButtonReturn ); + SetNextThink( m_flWait ); + } + } + if(m_iState == STATE_TURN_OFF)//turn off + { + m_iState = STATE_OFF;//just change state :) + if(pev->impulse) + UTIL_FireTargets( pev->target, m_hActivator, this, USE_OFF, 0 );//fire target + } +} + +void CBaseButton::ButtonReturn( void ) +{ + ASSERT(m_iState == STATE_ON); + m_iState = STATE_TURN_OFF; + pev->frame = 0;// use normal textures + + //make sound for toggleable button + if(pev->impulse) EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise), 1, ATTN_NORM); + if(pev->speed) + { + SetMoveDone( ButtonDone ); + LinearMove( m_vecPosition1, pev->speed); + } + else ButtonDone();//immediately switch +} + +//======================================================================= +// func_lever - momentary rotating button +//======================================================================= +class CFuncLever : public CBaseMover +{ +public: + void Spawn ( void ); + void Precache( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual int ObjectCaps( void ) + { + int flags = CBaseMover:: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; + if(m_iState == STATE_DEAD) return flags; + return flags | FCAP_CONTINUOUS_USE; + } + + BOOL MaxRange( void ) + { + if(pev->impulse == 1 && m_flMoveDistance > 0) return TRUE; + if(pev->impulse == -1 && m_flMoveDistance < 0) return TRUE; + return FALSE; + } + BOOL BackDir( void )//determine start direction + { + if( pev->dmg_save && m_flMoveDistance < 0) return TRUE; + if(!pev->dmg_save && m_flMoveDistance > 0) return TRUE; + return FALSE; + } + void PlayMoveSound( void ); + void CalcValue( void ); + void Think( void ); + + float mTimeToReverse, SendTime, nextplay; +}; +LINK_ENTITY_TO_CLASS( func_lever, CFuncLever ); + +void CFuncLever::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "backspeed")) + { + pev->dmg_save = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "movesound")) + { + m_iMoveSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "stopsound")) + { + m_iStopSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseMover::KeyValue( pkvd ); +} + +void CFuncLever::Precache( void ) +{ + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise = UTIL_PrecacheSound ("materials/buttons/lever1.wav");break; + case 2: pev->noise = UTIL_PrecacheSound ("materials/buttons/lever2.wav");break; + case 3: pev->noise = UTIL_PrecacheSound ("materials/buttons/lever3.wav");break; + case 4: pev->noise = UTIL_PrecacheSound ("materials/buttons/lever4.wav");break; + case 5: pev->noise = UTIL_PrecacheSound ("materials/buttons/lever5.wav");break; + case 0: pev->noise = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + switch (m_sounds)//load locked sounds + { + case 1: pev->noise2 = UTIL_PrecacheSound ("materials/buttons/lstop1.wav");break; + case 0: pev->noise2 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise2 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } +} + +void CFuncLever::Spawn( void ) +{ + Precache(); + if(pev->spawnflags & SF_NOTSOLID)//not solid button + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + pev->contents = CONTENTS_NONE; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + UTIL_SetModel(ENT(pev), pev->model); + UTIL_SetOrigin(this, pev->origin); + + AxisDir(); + SetBits (pFlags, PF_ANGULAR); + + //Smart field system ® + if( pev->speed == 0 ) pev->speed = 100;//check null speed + if( pev->speed > 800) pev->speed = 800;//check max speed + if( fabs(m_flMoveDistance) < pev->speed/2) + { + if(m_flMoveDistance > 0)m_flMoveDistance = -pev->speed/2; + if(m_flMoveDistance < 0)m_flMoveDistance = pev->speed/2; + if(m_flMoveDistance ==0)m_flMoveDistance = 45; + } + //check for direction (right direction will be set at first called use) + if(BackDir())pev->impulse = -1;//backward dir + else pev->impulse = 1;//forward dir +} + +void CFuncLever::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + if(IsLockedByMaster( useType ))//strange stuff... + { + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise2), 1, ATTN_NORM); + return; + } + if(useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, Value %.2f\n", GetStringForState( GetState()), pev->ideal_yaw ); + ALERT(at_console, "Distance: %.f. Speed %.2f\n", m_flMoveDistance, pev->speed ); + } + else if(m_iState != STATE_DEAD) + { + if(pActivator && pActivator->IsPlayer())//code only for player + { + if(gpGlobals->time > mTimeToReverse && !pev->dmg_save)//don't switch if backspeed set + pev->impulse = -pev->impulse; + mTimeToReverse = gpGlobals->time + 0.3;//max time to change dir + + pev->frags = pev->speed;//send speed + m_iState = STATE_IN_USE;//in use + SetNextThink(0.01); + } + } +} + +void CFuncLever::Think( void ) +{ + UTIL_SetAvelocity(this, pev->movedir * pev->frags * pev->impulse);//set speed and dir + if(m_iState == STATE_TURN_OFF && pev->dmg_save)pev->frags = -pev->dmg_save;//backspeed + if(!pev->dmg_save)pev->frags -= pev->frags * 0.05; + CalcValue(); + PlayMoveSound(); + + //wacthing code + if(pev->avelocity.Length() < 0.5)m_iState = STATE_OFF;//watch min speed + if(pev->ideal_yaw <= 0.01)//min range + { + if(pev->dmg_save && m_iState != STATE_IN_USE) + { + UTIL_SetAvelocity(this, g_vecZero); + m_iState = STATE_OFF;//off + } + else if(!MaxRange())UTIL_SetAvelocity(this, g_vecZero); + } + if(pev->ideal_yaw >= 0.99)//max range + { + if(pev->dmg_save && m_iState == STATE_IN_USE) + { + UTIL_SetAvelocity(this, g_vecZero); + if(pev->spawnflags & SF_START_ON)//Stay open flag + m_iState = STATE_DEAD;//dead + else m_iState = STATE_TURN_OFF;//must return + } + else if(MaxRange() && m_iState == STATE_IN_USE) + { + UTIL_SetAvelocity(this, g_vecZero); + if(pev->spawnflags & SF_START_ON)//Stay open flag + m_iState = STATE_DEAD;//dead + else m_iState = STATE_OFF;//dist is out + } + } + if(m_iState == STATE_IN_USE)m_iState = STATE_TURN_OFF;//try disable + + SetNextThink(0.01); + + if(m_iState == STATE_OFF || m_iState == STATE_DEAD)//STATE_DEAD is one end way + { + if(pev->dmg_save)//play sound only auto return + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise2), 1, ATTN_NORM); + UTIL_SetAvelocity(this, g_vecZero); + UTIL_AssignAngles( this, pev->angles ); + DontThink();//break thinking + } + //ALERT(at_console, "Ideal Yaw %f\n", pev->ideal_yaw); +} + +void CFuncLever::CalcValue( void ) +{ + //calc value to send + //pev->ideal_yaw = UTIL_CalcDistance(pev->angles) / fabs(m_flMoveDistance); + pev->ideal_yaw = pev->angles.Length() / fabs(m_flMoveDistance); + if(pev->ideal_yaw >= 1)pev->ideal_yaw = 1;//normalize value + else if(pev->ideal_yaw <= 0)pev->ideal_yaw = 0;//normalize value + + if(gpGlobals->time > SendTime) + { + UTIL_FireTargets( pev->target, this, this, USE_SET, pev->ideal_yaw );//send value + SendTime = gpGlobals->time + 0.01;//time to nextsend + } +} + +void CFuncLever::PlayMoveSound( void ) +{ + if(m_iState == STATE_TURN_OFF && pev->dmg_save ) + nextplay = fabs((pev->dmg_save * 0.01) / (m_flMoveDistance * 0.01)); + else nextplay = fabs((pev->frags * 0.01) / (m_flMoveDistance * 0.01)); + nextplay = 1/(nextplay * 5); + + if(gpGlobals->time > m_flBlockedTime) + { + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise), 1, ATTN_NORM); + m_flBlockedTime = gpGlobals->time + nextplay; + } +} + +//======================================================================= +// func_ladder - makes an area vertically negotiable +//======================================================================= +class CLadder : public CBaseEntity +{ +public: + void Spawn( void ); + void Precache( void ); +}; +LINK_ENTITY_TO_CLASS( func_ladder, CLadder ); + +void CLadder :: Precache( void ) +{ + pev->solid = SOLID_NOT; + pev->contents = CONTENTS_LADDER; + + pev->effects |= EF_NODRAW; +} + +void CLadder :: Spawn( void ) +{ + Precache(); + + UTIL_SetModel(ENT(pev), pev->model); // set size and link into world + pev->movetype = MOVETYPE_PUSH; +} + +//======================================================================= +// func_scaner - retinal scaner +//======================================================================= +class CFuncScaner : public CBaseBrush +{ +public: + void Spawn( void ); + void KeyValue( KeyValueData *pkvd ); + void Precache( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void PostActivate( void ); + void Think( void ); + BOOL VisionCheck( void ); + BOOL CanSee(CBaseEntity *pLooker ); + CBaseEntity *pSensor; + CBaseEntity *pLooker; +}; +LINK_ENTITY_TO_CLASS( func_scaner, CFuncScaner ); + +void CFuncScaner::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "sensor")) + { + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "acesslevel")) + { + pev->impulse = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "sense")) + { + pev->health = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CFuncScaner :: Precache( void ) +{ + CBaseBrush::Precache(); + + UTIL_PrecacheSound( "buttons/blip1.wav" ); + UTIL_PrecacheSound( "buttons/blip2.wav" ); + UTIL_PrecacheSound( "buttons/button7.wav" ); + UTIL_PrecacheSound( "buttons/button11.wav" ); +} + +void CFuncScaner :: Spawn( void ) +{ + Precache(); + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + if(!pev->health) pev->health = 15; + UTIL_SetModel(ENT(pev), pev->model); + UTIL_SetOrigin(this, pev->origin); + m_flDelay = 0.1; + + SetNextThink( 1 ); +} + +void CFuncScaner :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Acess Level %d\n", GetStringForState( GetState()), pev->impulse); + Msg( "Sense Corner: %g\n", pev->health); + } + else if (useType == USE_SET) + { + if(value) + { + pev->armorvalue = (int)value;//set new acess level for looker + pev->frame = 1; + } + } +} + +void CFuncScaner :: Think( void ) +{ + if (VisionCheck() && m_iState != STATE_ON && m_iState != STATE_DEAD) + { + if(pev->armorvalue) + { + m_iState = STATE_DEAD; + pLooker->m_iAcessLevel = (int)pev->armorvalue; + pev->armorvalue = 0; + //change acess level sound + EMIT_SOUND( edict(), CHAN_BODY, "buttons/blip2.wav", 1.0, ATTN_NORM ); + } + else if(m_iState == STATE_OFF)//scaner is off + { + //ok, we have seen entity + if(pLooker->pev->flags & FL_CLIENT) m_flDelay = 0.1; + else m_flDelay = 0.9; + m_iState = STATE_TURN_ON; + } + else if(m_iState == STATE_TURN_ON)//scaner is turn on + { + EMIT_SOUND( edict(), CHAN_BODY, "buttons/blip1.wav", 1.0, ATTN_NORM ); + pev->frame = 1; + m_flDelay = 0.3; + pev->button++; + if(pev->button > 9) + { + m_iState = STATE_ON; + pev->team = pLooker->m_iAcessLevel;//save our level acess + pev->button = 0; + } + } + } + else if(m_iState == STATE_ON)//scaner is on + { + EMIT_SOUND( edict(), CHAN_BODY, "buttons/blip1.wav", 1.0, ATTN_NORM ); + m_flDelay = 0.1; + pev->button++; + if(pev->button > 9) + { + m_iState = STATE_OFF; + pev->frame = 0; + pev->button = 0; + m_flDelay = 2.0; + + if(pev->team >= pev->impulse )//acees level is math or higher ? + { + EMIT_SOUND( edict(), CHAN_BODY, "buttons/button7.wav", 1.0, ATTN_NORM ); + UTIL_FireTargets( pev->target, this, this, USE_TOGGLE ); + } + else + { + EMIT_SOUND( edict(), CHAN_BODY, "buttons/button11.wav", 1.0, ATTN_NORM ); + UTIL_FireTargets( pev->netname, this, this, USE_TOGGLE ); + } + } + } + else if(m_iState == STATE_TURN_ON) + { + EMIT_SOUND( edict(), CHAN_BODY, "buttons/button11.wav", 1.0, ATTN_NORM ); + m_iState = STATE_OFF; + pev->button = 0; + pev->frame = 0; + m_flDelay = 2.0; + } + else if(m_iState == STATE_DEAD) + { + m_iState = STATE_OFF; + pev->button = 0; + m_flDelay = 2.0; + } + + // is this a sensible rate? + SetNextThink( m_flDelay ); +} + +void CFuncScaner :: PostActivate( void ) +{ + if (pev->message) + { + pSensor = UTIL_FindEntityByTargetname(NULL, STRING(pev->message)); + if(!pSensor) pSensor = this; + } + else pSensor = this; +} + +BOOL CFuncScaner :: VisionCheck( void ) +{ + pLooker = UTIL_FindEntityInSphere( NULL, pSensor->pev->origin, 30 ); + + if (pLooker) + { + while( pLooker != NULL ) + { + if(pLooker && pLooker->pev->flags & (FL_MONSTER | FL_CLIENT)) + return CanSee(pLooker);//looker found + pLooker = UTIL_FindEntityInSphere( pLooker, pSensor->pev->origin, 30 ); + } + } + return FALSE;//no lookers +} + +BOOL CFuncScaner :: CanSee(CBaseEntity *pLooker ) +{ + if(!pSensor || !pLooker) return FALSE; + + if ((pLooker->EyePosition() - pSensor->pev->origin).Length() > 30) return FALSE; + + // copied from CBaseMonster's FInViewCone function + Vector2D vec2LOS; + float flDot, flComp = cos(pev->health/2 * M_PI/180.0); + + UTIL_MakeVectors ( pLooker->pev->angles ); + vec2LOS = ( pSensor->pev->origin - pLooker->pev->origin ).Make2D(); + vec2LOS = vec2LOS.Normalize(); + flDot = DotProduct (vec2LOS , gpGlobals->v_forward.Make2D() ); + + if (flDot <= flComp) return FALSE; + return TRUE; +} + +//======================================================================= +// volume of space that the player must stand in to control the train +//======================================================================= +class CFuncTrainControls : public CPointEntity +{ +public: + void Spawn( void ); + void PostSpawn( void ); +}; +LINK_ENTITY_TO_CLASS( func_traincontrols, CFuncTrainControls ); + +void CFuncTrainControls :: PostSpawn( void ) +{ + CBaseEntity *pTarget = NULL; + + do + { + pTarget = UTIL_FindEntityByTargetname( pTarget, STRING(pev->target) ); + } while ( pTarget && !FClassnameIs(pTarget->pev, "func_tracktrain") ); + + if ( !pTarget ) + { + ALERT( at_debug, "TrackTrainControls: No train %s\n", STRING(pev->target) ); + return; + } + + CFuncTrackTrain *ptrain = (CFuncTrackTrain*)pTarget; + ptrain->SetControls( pev ); + UTIL_Remove( this ); +} + +void CFuncTrainControls :: Spawn( void ) +{ + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + UTIL_SetModel( ENT(pev), pev->model ); + + UTIL_SetSize( pev, pev->mins, pev->maxs ); + UTIL_SetOrigin( this, pev->origin ); +} \ No newline at end of file diff --git a/server/ents/basefx.cpp b/server/ents/basefx.cpp new file mode 100644 index 00000000..d5a86b06 --- /dev/null +++ b/server/ents/basefx.cpp @@ -0,0 +1,2900 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#include "extdll.h" +#include "defaults.h" +#include "utils.h" +#include "cbase.h" +#include "basebeams.h" +#include "baseweapon.h" +#include "decals.h" +#include "basebrush.h" +#include "shake.h" +#include "monsters.h" +#include "client.h" +#include "player.h" +#include "soundent.h" + +//======================================================================= +// main functions () +//======================================================================= +class CBaseParticle : public CBaseLogic +{ +public: + void Spawn( void ); + void Precache( void ){ pev->netname = UTIL_PrecacheAurora(pev->message); } + void KeyValue( KeyValueData *pkvd ); + void PostActivate( void ){ Switch(); } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Switch( void ); +}; + +void CBaseParticle :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "aurora")) + { + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CBaseParticle::Switch( void ) +{ + int renderfx = (m_iState ? kRenderFxAurora : kRenderFxNone ); + if( pev->target ) + { + CBaseEntity *pTarget = UTIL_FindEntityByTargetname(NULL, STRING(pev->target), m_hActivator ); + while (pTarget) + { + UTIL_SetAurora( pTarget, pev->netname); + pTarget->pev->renderfx = renderfx; + pTarget = UTIL_FindEntityByTargetname(pTarget, STRING(pev->target), m_hActivator ); + } + } + else + { + UTIL_SetAurora( this, pev->netname); + pev->renderfx = renderfx; + } +} + +void CBaseParticle::Spawn( void ) +{ + Precache(); + UTIL_SetModel(edict(), "sprites/null.spr"); + UTIL_SetOrigin(this, pev->origin); + pev->solid = SOLID_NOT; + if(pev->spawnflags & SF_START_ON ) m_iState = STATE_ON; + else m_iState = STATE_OFF; +} + +void CBaseParticle::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + m_iState = STATE_ON; + Switch(); + } + else if (useType == USE_OFF) + { + m_iState = STATE_OFF; + Switch(); + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, ParticleName: %s\n", GetStringForState( GetState()), STRING( pev->netname )); + if(pev->target)Msg( "Attach to: %s\n", STRING( pev->target )); + else Msg("\n"); + } +} +LINK_ENTITY_TO_CLASS( env_particle, CBaseParticle ); + +//========================================================= +// env_sky, an unreal tournament - style sky effect +//========================================================= +class CEnvSky : public CBaseLogic +{ +public: + void StartMessage( CBasePlayer *pPlayer ); + void PostSpawn( void ); +}; +LINK_ENTITY_TO_CLASS( env_sky, CEnvSky ); + +void CEnvSky :: PostSpawn( void ) +{ +} + +void CEnvSky :: StartMessage( CBasePlayer *pPlayer ) +{ + pev->effects |= EF_NODRAW; + MESSAGE_BEGIN( MSG_ONE, gmsgSetSky, NULL, pPlayer->pev ); + WRITE_BYTE( 1 ); // mode + WRITE_COORD( pev->origin.x ); // view position + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z ); + MESSAGE_END(); + +} + +//======================================================================= +// env_global +//======================================================================= + +#define SF_GLOBAL_SET 1 //Set global state to initial state on spawn + +class CEnvGlobal : public CBaseLogic +{ +public: + void Spawn( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( env_global, CEnvGlobal ); + +void CEnvGlobal::KeyValue( KeyValueData *pkvd ) +{ + if(FStrEq(pkvd->szKeyName, "triggermode")) + { + pev->impulse = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if(FStrEq(pkvd->szKeyName, "initialstate")) + { + pev->button = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CEnvGlobal::Spawn( void ) +{ + if ( !m_globalstate ) UTIL_Remove( this ); + else if( FBitSet( pev->spawnflags, SF_GLOBAL_SET ) ) + { + if ( !gGlobalState.EntityInTable( m_globalstate ) ) + gGlobalState.EntityAdd( m_globalstate, gpGlobals->mapname, (GLOBALESTATE)pev->button ); + } +} + +void CEnvGlobal::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + GLOBALESTATE oldState = gGlobalState.EntityGetState( m_globalstate ); + GLOBALESTATE newState; + + if (useType == USE_TOGGLE) + { + switch( pev->impulse ) + { + case 0: + newState = GLOBAL_OFF; + break; + case 1: + newState = GLOBAL_ON; + break; + case 2: + newState = GLOBAL_DEAD; + break; + case 3: + default: + if ( oldState == GLOBAL_ON ) newState = GLOBAL_OFF; + else if ( oldState == GLOBAL_OFF ) newState = GLOBAL_ON; + else newState = oldState; + } + } + else if(useType == USE_ON ) newState = GLOBAL_ON; + else if(useType == USE_OFF) newState = GLOBAL_OFF; + else if(useType == USE_SET) newState = GLOBAL_DEAD; + + if ( gGlobalState.EntityInTable( m_globalstate )) + gGlobalState.EntitySetState( m_globalstate, newState ); + else gGlobalState.EntityAdd( m_globalstate, gpGlobals->mapname, newState ); +} + +//======================================================================= +// env_local +//======================================================================= +class CEnvLocal : public CBaseLogic +{ +public: + void Spawn( void ); + void Think( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( env_local, CEnvLocal ); + + +void CEnvLocal::Spawn( void ) +{ + if (pev->spawnflags & SF_START_ON) + m_iState = STATE_ON; + else m_iState = STATE_OFF; +} + +void CEnvLocal::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; //save activator + pev->scale = value; //save value + + if(IsLockedByMaster( useType )) return; + + if (useType == USE_TOGGLE) + { //set use type + if(m_iState == STATE_TURN_OFF || m_iState == STATE_OFF) useType = USE_ON; + else if(m_iState == STATE_TURN_ON || m_iState == STATE_ON) useType = USE_OFF; + } + if(useType == USE_ON) //enable entity + { + if(m_iState == STATE_TURN_OFF || m_iState == STATE_OFF) //activate turning off entity + { + + if (m_flDelay) //we have time to turning on + { + m_iState = STATE_TURN_ON; + SetNextThink( m_flDelay ); + } + else //just enable entity + { + m_iState = STATE_ON; + UTIL_FireTargets(pev->target, pActivator, this, USE_ON, pev->scale ); + DontThink();//break thinking + } + } + } + else if(useType == USE_OFF) //disable entity + { + if(m_iState == STATE_TURN_ON || m_iState == STATE_ON) //activate turning off entity + { + + if (m_flWait) //we have time to turning off + { + m_iState = STATE_TURN_OFF; + SetNextThink( m_flWait ); + } + else //just enable entity + { + m_iState = STATE_OFF; + UTIL_FireTargets(pev->target, pActivator, this, USE_OFF, pev->scale ); + DontThink();//break thinking + } + } + } + else if(useType == USE_SET) //absolutely on + { + if(value) m_iState = STATE_ON; + else m_iState = STATE_OFF; + DontThink();//break thinking + } + else if(useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg("time, before enable %g, time, before disable %g\n", m_flDelay, m_flWait ); + Msg("current state %s\n", GetStringForState( GetState()) ); + } +} + +void CEnvLocal::Think( void ) +{ + if (m_iState == STATE_TURN_ON) + { + m_iState = STATE_ON; + UTIL_FireTargets(pev->target, m_hActivator, this, USE_ON, pev->scale ); + } + else if (m_iState == STATE_TURN_OFF) + { + m_iState = STATE_OFF; + UTIL_FireTargets(pev->target, m_hActivator, this, USE_OFF, pev->scale ); + } + DontThink();//break thinking +} + + +//========================================================= +// UTIL_ScreenFade (fade screen) +//========================================================= +#define SF_FADE_IN 0x0001 // Fade in, not out +#define SF_FADE_MODULATE 0x0002 // Modulate, don't blend +#define SF_FADE_ALL 0x0004 // fade all clients +#define SF_FADE_CAMERA 0x0008 // fading only for camera + +class CFade : public CBaseLogic +{ +public: + void Spawn( void ) { m_iState = STATE_OFF; } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData *pkvd ); +}; +LINK_ENTITY_TO_CLASS( env_fade, CFade ); + +void CFade::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "duration")) + { + pev->dmg_take = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "holdtime")) + { + pev->dmg_save = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CFade::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "Fadecolor: %g %g %g, Fadealpha: %g, Fadetime: %g\n", pev->rendercolor.x, pev->rendercolor.y, pev->rendercolor.z, pev->renderamt, pev->dmg_take ); + Msg( "HoldTime %g, Mode: %s, Modulate: %s\n", pev->dmg_save, pev->spawnflags & SF_FADE_IN ? "FADE IN" : "FADE OUT", pev->spawnflags & SF_FADE_MODULATE ? "YES" : "NO" ); + } + else + { + //setup fade flags + int fadeFlags = 0; + if(!(pev->spawnflags & SF_FADE_IN)) fadeFlags |= FFADE_OUT; + if ( pev->spawnflags & SF_FADE_MODULATE ) fadeFlags |= FFADE_MODULATE; + if ( pev->spawnflags & SF_FADE_CAMERA ) fadeFlags |= FFADE_CUSTOMVIEW; + + //apply fade + if ( pev->spawnflags & SF_FADE_ALL ) UTIL_ScreenFadeAll( pev->rendercolor, pev->dmg_take, pev->dmg_save, pev->renderamt, fadeFlags ); + else UTIL_ScreenFade( pev->rendercolor, pev->dmg_take, pev->dmg_save, pev->renderamt, fadeFlags ); + } +} + +//======================================================================= +// env_zoom - change fov for player +//======================================================================= +class CEnvZoom : public CBaseLogic +{ + void Spawn (void ){ if(!pev->button) pev->button = CVAR_GET_FLOAT( "default_fov" ); } + void EXPORT Think( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData* pkvd ); + void SetFadeTime( void ); +}; + +LINK_ENTITY_TO_CLASS( env_zoom, CEnvZoom ); + +void CEnvZoom::KeyValue( KeyValueData* pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "duration")) + { + pev->takedamage = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "fov")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CEnvZoom::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if ( !pActivator || !pActivator->IsPlayer()) pActivator = UTIL_PlayerByIndex( 1 ); + m_hActivator = pActivator; //save activator + + if (m_iState == STATE_ON) return; + if (useType == USE_TOGGLE || useType == USE_ON) SetFadeTime(); + else if (useType == USE_OFF)((CBasePlayer *)pActivator)->m_iFOV = CVAR_GET_FLOAT( "default_fov" ); + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg("State: %s, Fade time %.00f\n", GetStringForState( GetState()), pev->takedamage); + Msg("Current FOV: %d, Final FOV: %d\n", ((CBasePlayer *)pActivator)->m_iFOV, pev->button); + } +} + +void CEnvZoom::SetFadeTime( void ) +{ + int CurFOV; + int Length; + + if( pev->takedamage == 0) //instant apply fov + { + ((CBasePlayer *)(CBaseEntity *)m_hActivator)->m_iFOV = pev->button; + return; + } + else + { + CurFOV = ((CBasePlayer *)(CBaseEntity *)m_hActivator)->m_iFOV; + if(CurFOV == 0) CurFOV = ((CBasePlayer *)(CBaseEntity *)m_hActivator)->m_iFOV = CVAR_GET_FLOAT( "default_fov" ); + + if(CurFOV > pev->button) Length = CurFOV - pev->button; + else if (CurFOV < pev->button) + { + Length = pev->button - CurFOV; + pev->body = 1;//increment fov + } + else return;//no change + + pev->health = pev->takedamage / Length; + SetNextThink ( pev->health ); + } +} + +void CEnvZoom::Think ( void ) +{ + if( ((CBasePlayer *)(CBaseEntity *)m_hActivator)->m_iFOV == pev->button ) + { + //calculate fov is over + SetThink( NULL ); + DontThink(); + m_iState = STATE_OFF; + //fire target after finished //transfer fov + UTIL_FireTargets( pev->target, m_hActivator, this, USE_TOGGLE, pev->button ); + return; + } + else + { + if(pev->body ) ((CBasePlayer *)(CBaseEntity *)m_hActivator)->m_iFOV++; + else ((CBasePlayer *)(CBaseEntity *)m_hActivator)->m_iFOV--; + } + m_iState = STATE_ON; + SetNextThink ( pev->health ); +} + +//========================================================= +// env_sound HL1:Legacy +//========================================================= +class CEnvSound : public CPointEntity +{ +public: + void KeyValue( KeyValueData* pkvd); + void Spawn( void ){ SetNextThink(0.5); } + void Think( void ); +}; +LINK_ENTITY_TO_CLASS( env_sound, CEnvSound ); + +void CEnvSound :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "radius")) + { + pev->frags = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "roomtype")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } +} + +void CEnvSound :: Think( void ) +{ + CBasePlayer *pPlayer = UTIL_FindPlayerInPVS(edict()); + + if (pPlayer && pPlayer->m_iSndRoomtype != pev->button) + { + if(UTIL_FindPlayerInSphere( pev->origin, pev->frags )) + { + if(!pev->impulse) + { + pev->impulse = 1; + pPlayer->m_iSndRoomtype = pev->button; //set new dsp + pPlayer->hearNeedsUpdate = 1; + } + } + else pev->impulse = 0; + SetNextThink( 0.25 ); + } + else SetNextThink( 0.75 ); +} + +//========================================================= +// display message +//========================================================= +class CMessage : public CPointEntity +{ +public: + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + CBaseEntity *pPlayer = NULL; + + if ( IsMultiplayer() ) UTIL_ShowMessageAll( STRING(pev->message) ); + else + { + if ( pActivator && pActivator->IsPlayer() ) pPlayer = pActivator; + else pPlayer = UTIL_PlayerByIndex( 1 ); + } + + if ( pPlayer ) UTIL_ShowMessage( STRING(pev->message), pPlayer ); + if ( pev->spawnflags & 1 ) UTIL_Remove( this ); + } +}; +LINK_ENTITY_TO_CLASS( env_message, CMessage ); + +//========================================================= +// env_rain, custom client weather effects +//========================================================= +class CEnvRain : public CPointEntity +{ +public: + void KeyValue( KeyValueData *pkvd ) + { + if (FStrEq(pkvd->szKeyName, "radius")) + { + pev->armorvalue = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "mode")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + } +}; +LINK_ENTITY_TO_CLASS( env_rain, CEnvRain ); + +//========================================================= +// env_beverage - Beverage Dispenser +//========================================================= +class CEnvBeverage : public CBaseLogic +{ +public: + void Spawn( void ); + void Precache( void ){ UTIL_PrecacheEntity( "item_sodacan" ); } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( env_beverage, CEnvBeverage ); + +void CEnvBeverage::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if ( pev->impulse != 0 || pev->health <= 0 ) + { + // no more cans while one is waiting in the dispenser, or if I'm out of cans. + return; + } + + CBaseEntity *pCan = CBaseEntity::Create( "item_sodacan", pev->origin, pev->angles, edict() ); + + if ( pev->skin == 6 ) + { + // random + pCan->pev->skin = RANDOM_LONG( 0, 5 ); + } + else + { + pCan->pev->skin = pev->skin; + } + + pev->impulse = 1; + pev->health--; + + //SetThink (SUB_Remove); + //pev->nextthink = gpGlobals->time; +} + +void CEnvBeverage::Spawn( void ) +{ + Precache(); + pev->solid = SOLID_NOT; + pev->effects = EF_NODRAW; + pev->frags = 0; + + if ( pev->health == 0 ) + { + pev->health = 10; + } +} + +//======================================================================= +// env_sprite - classic HALF-LIFE sprites +//======================================================================= +void CSprite::Spawn( void ) +{ + Precache(); + + UTIL_SetModel( ENT(pev), pev->model ); + + //Smart Field System © + if(!pev->renderamt) pev->renderamt = 200; //light transparency + if(!pev->framerate) pev->framerate = Frames(); //play sequence at one second + if(!pev->rendermode) pev->rendermode = kRenderTransAdd; + + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NOCLIP; + pev->frame = 0; + SetBits( pev->flags, FL_POINTENTITY ); + + pev->angles.x = -pev->angles.x; + pev->angles.y = 180 - pev->angles.y; + pev->angles[1] = 0 - pev->angles[1]; + TurnOff(); +} + +void CSprite::PostSpawn( void ) +{ + m_pGoalEnt = UTIL_FindEntityByTargetname (NULL, STRING(pev->target)); + if(m_pGoalEnt) UTIL_SetOrigin( this, m_pGoalEnt->pev->origin ); +} + +void CSprite::Think( void ) +{ + SetNextThink( 0 ); + + if(pev->spawnflags & SF_TEMPSPRITE && gpGlobals->time > pev->pain_finished ) UTIL_Remove(this); + else if ( pev->framerate ) Animate( pev->framerate * (gpGlobals->time - pev->dmgtime) ); + + Move(); + pev->dmgtime = gpGlobals->time; +} + +void CSprite::Move( void ) +{ + // Not moving on a path, return + if (!m_pGoalEnt) return; + + // Subtract movement from the previous frame + pev->frags -= pev->speed * gpGlobals->frametime; + + // Have we moved enough to reach the target? + if ( pev->frags <= 0 ) + { + // Fire the passtarget if there is one + UTIL_FireTargets(m_pGoalEnt->pev->message, this, this, USE_TOGGLE ); + if ( FBitSet( m_pGoalEnt->pev->spawnflags, SF_CORNER_FIREONCE ) ) m_pGoalEnt->pev->message = iStringNull; + if ( FBitSet( m_pGoalEnt->pev->spawnflags, SF_CORNER_TELEPORT ) ) + { + m_pGoalEnt = m_pGoalEnt->GetNext(); + if ( m_pGoalEnt ) UTIL_AssignOrigin( this, m_pGoalEnt->pev->origin); + } + if ( FBitSet( m_pGoalEnt->pev->spawnflags, SF_CORNER_WAITTRIG ) ) + { + TurnOff(); //wait for retrigger + return; + } + + // Time to go to the next target + m_pGoalEnt = m_pGoalEnt->GetNext(); + + // Set up next corner + if ( !m_pGoalEnt ) UTIL_SetVelocity( this, g_vecZero ); + else + { + pev->target = m_pGoalEnt->pev->targetname; //save last corner + ((CPathCorner *)m_pGoalEnt)->GetSpeed( &pev->armorvalue ); + + Vector delta = m_pGoalEnt->pev->origin - pev->origin; + pev->frags = delta.Length(); + pev->movedir = delta.Normalize(); + m_flDelay = gpGlobals->time + ((CPathCorner *)m_pGoalEnt)->GetDelay(); + } + } + + if ( m_flDelay > gpGlobals->time ) + pev->speed = UTIL_Approach( 0, pev->speed, 5 * gpGlobals->frametime ); + else pev->speed = UTIL_Approach( pev->armorvalue, pev->speed, 5 * gpGlobals->frametime ); + + float fraction = 2 * gpGlobals->frametime; + UTIL_SetVelocity( this, ((pev->movedir * pev->speed) * fraction) + (pev->velocity * (1-fraction))); +} + +void CSprite::TurnOff( void ) +{ + SetBits( pev->effects, EF_NODRAW ); + m_iState = STATE_OFF; + if(m_pGoalEnt) m_pGoalEnt = m_pGoalEnt->GetPrev(); + UTIL_SetVelocity( this, g_vecZero ); + DontThink(); +} + +void CSprite::TurnOn( void ) +{ + pev->dmgtime = gpGlobals->time; + ClearBits( pev->effects, EF_NODRAW ); + pev->frame = 0; + m_iState = STATE_ON; + + pev->armorvalue = pev->speed; + m_flDelay = gpGlobals->time; + pev->frags = 0; + + Move(); + SetNextThink( 0 ); +} + +void CSprite::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + pActivator = m_hActivator; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) TurnOn(); + else if ( useType == USE_OFF ) TurnOff(); + else if( useType == USE_SHOWINFO ) + { + DEBUGHEAD; + Msg( "State: %s, speed %g\n", GetStringForState( GetState()), pev->speed ); + Msg( "NextPath: %s, framerate %g\n", m_pGoalEnt ? STRING(m_pGoalEnt->pev->targetname) : "no path", pev->framerate ); + } +} +LINK_ENTITY_TO_CLASS( env_sprite, CSprite ); +LINK_ENTITY_TO_CLASS( env_glow, CSprite ); + +//================================================================= +// env_model: like env_sprite, except you can specify a sequence. +//================================================================= +#define SF_DROPTOFLOOR 4 + +class CEnvModel : public CBaseAnimating +{ + void Spawn( void ); + void Precache( void ){ UTIL_PrecacheModel( pev->model ); } + void EXPORT Animate( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + void SetAnim( void ); +}; +LINK_ENTITY_TO_CLASS( env_model, CEnvModel ); + +void CEnvModel::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "mode")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszSequence")) + { + pev->netname = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseAnimating::KeyValue( pkvd ); +} + +void CEnvModel :: Spawn( void ) +{ + Precache(); + UTIL_SetModel( ENT(pev), pev->model ); + UTIL_SetOrigin(this, pev->origin); + + pev->takedamage = DAMAGE_NO; + if (!(pev->spawnflags & SF_NOTSOLID)) + { + pev->solid = SOLID_SLIDEBOX; + UTIL_AutoSetSize(); + } + + if (pev->spawnflags & SF_DROPTOFLOOR) + { + pev->origin.z += 1; + DROP_TO_FLOOR ( ENT(pev) ); + } + + InitBoneControllers(); + ResetSequenceInfo( ); + m_iState = STATE_OFF; + if(pev->spawnflags & SF_START_ON) Use( this, this, USE_ON, 0 ); +} + +void CEnvModel::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + SetAnim(); + SetThink( Animate ); + SetNextThink( 0.1 ); + m_iState = STATE_ON; + } + else if ( useType == USE_OFF ) + { + m_iState = STATE_OFF; + DontThink(); + } +} + +void CEnvModel::SetAnim( void ) +{ + pev->sequence = LookupSequence( STRING( pev->netname )); + if (pev->sequence == -1)pev->sequence = 0; + pev->frame = 0; + ResetSequenceInfo(); +} + +void CEnvModel::Animate( void ) +{ + SetNextThink( 0.1 ); + StudioFrameAdvance(); + + if (m_fSequenceFinished ) + { + if(pev->button) + { + //pev->frame = 0; + m_iState = STATE_OFF; + DontThink(); + } + else SetAnim(); + } +} + +//======================================================================= +// env_counter - digital 7 segment indicator as default +//======================================================================= +class CDecLED : public CBaseLogic +{ +public: + void Spawn( void ); + void CheckState( void ); + void Precache( void ){ UTIL_PrecacheModel( pev->model, "sprites/decimal.spr" ); } + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + float Frames( void ) { return MODEL_FRAMES( pev->modelindex ) - 1; } + float flashtime; + float curframe( void ) + { + if (pev->frame - floor(pev->frame) > 0.5) return ceil(pev->frame); + else return floor(pev->frame); + } +}; +LINK_ENTITY_TO_CLASS( env_counter, CDecLED ); + +void CDecLED :: Spawn( void ) +{ + Precache( ); + UTIL_SetModel( ENT(pev), pev->model, "sprites/decimal.spr" ); + + //Smart Field System © + if(!pev->renderamt) pev->renderamt = 200; //light transparency + if(!pev->rendermode) pev->rendermode = kRenderTransAdd; + if(!pev->frags) pev->frags = Frames(); + if(!pev->impulse) pev->impulse = -1; + CheckState(); + + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + pev->angles.x = -pev->angles.x; + pev->angles.y = 180 - pev->angles.y; + pev->angles[1] = 0 - pev->angles[1]; + pev->button = TRUE; +} + +void CDecLED :: CheckState( void ) +{ + switch( (int)pev->dmg ) + { + case 1: + if( pev->impulse >= curframe() ) m_iState = STATE_ON; + else m_iState = STATE_OFF; + break; + case 2: + if( pev->impulse <= curframe() ) m_iState = STATE_ON; + else m_iState = STATE_OFF; + break; + default: + if( pev->impulse == curframe() ) m_iState = STATE_ON; + else m_iState = STATE_OFF; + break; + } +} + +void CDecLED :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "maxframe")) + { + pev->frags = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "keyframe")) + { + pev->impulse = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "type")) + { + pev->dmg = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CDecLED :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_ON || useType == USE_TOGGLE) + { + if (pev->frame == pev->frags) + { + UTIL_FireTargets( pev->target, pActivator, this, useType, value ); + pev->frame = 0; + } + else pev->frame++; + + pev->button = TRUE; + pev->effects &= ~EF_NODRAW; + flashtime = gpGlobals->time + 0.5; + } + else if (useType == USE_OFF ) + { + if (pev->frame == 0) + { + UTIL_FireTargets( pev->target, pActivator, this, useType, value ); + pev->frame = pev->frags; + } + else pev->frame--; + + pev->button = FALSE; + pev->effects &= ~EF_NODRAW; + flashtime = gpGlobals->time + 0.5; + } + else if (useType == USE_SET) //set custom frame + { + if(value != 0) + { + if(value == -1) value = 0; //reset frame + pev->frame = fmod( value, pev->frags + 1); + float next = value / (pev->frags + 1); + if(next) UTIL_FireTargets( pev->target, pActivator, this, useType, next ); + pev->button = TRUE; + pev->effects &= ~EF_NODRAW; + flashtime = gpGlobals->time + 0.5; + } + else if(gpGlobals->time > flashtime) + { + if( pev->button) + { + pev->button = FALSE; + pev->effects |= EF_NODRAW; + } + else + { + pev->button = TRUE; + pev->effects &= ~EF_NODRAW; + } + } + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "Current frame: %.0f, Keyframe %d\n", pev->frame, pev->impulse ); + Msg( "State: %s, Maxframe %g\n", GetStringForState( GetState()), pev->frags); + } + CheckState(); +} + +//======================================================================= +// env_shake - earthquake (screen shake) +//======================================================================= +class CShake : public CBaseLogic +{ +public: + void Spawn( void ){ m_iState = STATE_OFF; } + void Think( void ) { m_iState = STATE_OFF; }; + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + if(useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Amplitude: %g, Frequency: %g\n", GetStringForState( GetState()), pev->scale, pev->dmg_save); + Msg( "Radius: %g, Duration: %d, Smoke max scale: %d\n", pev->dmg, pev->dmg_take); + } + else + { + UTIL_ScreenShake( pev->origin, Amplitude(), Frequency(), Duration(), Radius() ); + m_iState = STATE_ON; + SetNextThink( Duration() ); + } + } + void KeyValue( KeyValueData *pkvd ) + { + if (FStrEq(pkvd->szKeyName, "amplitude")) + { + SetAmplitude( atof(pkvd->szValue) ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "frequency")) + { + SetFrequency( atof(pkvd->szValue) ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "duration")) + { + SetDuration( atof(pkvd->szValue) ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "radius")) + { + SetRadius( atof(pkvd->szValue) ); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); + } + + inline float Amplitude( void ) { return pev->scale; } + inline float Frequency( void ) { return pev->dmg_save; } + inline float Duration( void ) { return pev->dmg_take; } + inline float Radius( void ) { return pev->dmg; } + inline void SetAmplitude( float amplitude ) { pev->scale = amplitude; } + inline void SetFrequency( float frequency ) { pev->dmg_save = frequency; } + inline void SetDuration( float duration ) { pev->dmg_take = duration; } + inline void SetRadius( float radius ) { pev->dmg = radius; } +}; +LINK_ENTITY_TO_CLASS( env_shake, CShake ); + +//======================================================================= +// env_sparks - classic HALF-LIFE sparks +//======================================================================= +class CEnvSpark : public CBaseLogic +{ +public: + void Spawn(void); + void Think(void); + void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS(env_spark, CEnvSpark); + +void CEnvSpark::Spawn(void) +{ + if(!m_flDelay)m_flDelay = 1.0;//Smart field system ® + if(pev->spawnflags & 64) Use( this, this, USE_ON, 0 ); +} + +void CEnvSpark::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + m_iState = STATE_ON; + SetNextThink( m_flDelay ); + } + else if (useType == USE_OFF) + { + m_iState = STATE_OFF; + DontThink(); + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, MaxDelay %.2f\n\n", GetStringForState( GetState()), m_flDelay ); + } +} + +void CEnvSpark::Think(void) +{ + float flVolume = RANDOM_FLOAT ( 0.25 , 0.75 ) * 0.4;//random volume range + switch ( RANDOM_LONG(0,5)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark1.wav", flVolume, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark2.wav", flVolume, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark3.wav", flVolume, ATTN_NORM); break; + case 3: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark4.wav", flVolume, ATTN_NORM); break; + case 4: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark5.wav", flVolume, ATTN_NORM); break; + case 5: EMIT_SOUND(ENT(pev), CHAN_VOICE, "materials/spark6.wav", flVolume, ATTN_NORM); break; + } + + UTIL_Sparks( pev->origin + pev->size * 0.5 ); + if(m_iState == STATE_ON) SetNextThink( 0.1 + RANDOM_FLOAT (0, m_flDelay) ); //!!! +} + +//========================================================= +// env_explosion (classic explode ) +//========================================================= +class CEnvExplosion : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Precache( void ) { if(pev->message) pev->button = UTIL_PrecacheModel( pev->model ); } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Explode( void ); +}; +LINK_ENTITY_TO_CLASS( env_explosion, CEnvExplosion ); + +void CEnvExplosion::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "radius") || FStrEq(pkvd->szKeyName, "iMagnitude")) + { + pev->dmg = atoi(pkvd->szValue); + if(pev->dmg < 10) pev->dmg = 10; + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CEnvExplosion::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "Radius %g\n\n", pev->dmg ); + } + else Explode(); +} + +void CEnvExplosion::Explode( void ) +{ + short m_usExplode; + short m_usWExplode; + TraceResult tr; + Vector vecSpot = pev->origin;// trace starts here! + UTIL_TraceLine ( vecSpot + Vector( 0, 0, 8 ), vecSpot + Vector ( 0, 0, -32 ), ignore_monsters, ENT(pev), &tr); + + if(!FBitSet( pev->spawnflags, 4 )) + { + if(pev->button)//custom sprite + { + m_usExplode = pev->button; + m_usWExplode = pev->button; + } + else + { + m_usExplode = g_sModelIndexFireball; + m_usWExplode = g_sModelIndexWExplosion; + } + } + else + { + m_usExplode = g_sModelIndexNullSprite; + m_usWExplode = g_sModelIndexNullSprite; + } + + // Pull out of the wall a bit + if ( tr.flFraction != 1.0 ) pev->origin = tr.vecEndPos + (tr.vecPlaneNormal * (pev->dmg - 24) * 0.6); + int iContents = UTIL_PointContents ( pev->origin ); + + entvars_t *pevOwner; + if ( pev->owner ) pevOwner = VARS( pev->owner ); + else pevOwner = NULL; + pev->owner = NULL; // can't traceline attack owner if this is set + if(pev->dmg <= 0)pev->dmg = 100; //Smart field system ® + + + if( iContents & MASK_WATER ) + { + SFX_Explode( m_usWExplode, pev->origin, pev->dmg, TE_EXPLFLAG_NONE ); + if(!FBitSet( pev->spawnflags, 1 )) + RadiusDamage ( pev->origin, pev, pevOwner, pev->dmg / 2, pev->dmg * 2.5, CLASS_NONE, DMG_BLAST ); + } + else + { + CSoundEnt::InsertSound ( bits_SOUND_COMBAT, pev->origin, pev->dmg * 5, 3.0 ); + SFX_Explode( m_usExplode, pev->origin, pev->dmg, TE_EXPLFLAG_NONE ); + if(!FBitSet( pev->spawnflags, 1 )) + RadiusDamage ( pev->origin, pev, pevOwner, pev->dmg, pev->dmg * 2.5, CLASS_NONE, DMG_BLAST ); + + if(!FBitSet( pev->spawnflags, 8 )) + { + CBaseEntity *pWreckage = Create( "smokeent", pev->origin, pev->angles ); + pWreckage->SetNextThink( 0.2 ); + pWreckage->pev->impulse = ( pev->dmg - 50 ) * 0.6; + pWreckage->pev->dmgtime = gpGlobals->time + 1.5; + } + if(!FBitSet( pev->spawnflags, 32 )) + { + int sparkCount = RANDOM_LONG(0,3); //make sparks + for ( int i = 0; i < sparkCount; i++ ) + Create( "sparkleent", pev->origin, tr.vecPlaneNormal, NULL ); + } + if(!FBitSet( pev->spawnflags, 16 )) + { + if ( RANDOM_FLOAT( 0 , 1 ) < 0.5 ) UTIL_DecalTrace( &tr, DECAL_SCORCH1 ); + else UTIL_DecalTrace( &tr, DECAL_SCORCH2 ); + } + } + if(!FBitSet( pev->spawnflags, 2 )) UTIL_Remove( this ); +} + +//========================================================= +// gibs (sprite or models gib) +//========================================================= +void CGib :: Spawn( const char *szGibModel ) +{ + pev->movetype = MOVETYPE_BOUNCE; + pev->friction = 0.55; // deading the bounce a bit + pev->renderamt = 255; + pev->rendermode = kRenderNormal; + pev->renderfx = kRenderFxNone; + pev->solid = SOLID_TRIGGER; + pev->classname = MAKE_STRING("gib"); + + UTIL_SetModel(ENT(pev), szGibModel); + UTIL_SetSize(pev, g_vecZero, g_vecZero); + m_lifeTime = MAX_GIB_LIFETIME; + + m_material = None; + m_cBloodDecals = 5;// how many blood decals this gib can place (1 per bounce until none remain). +} + +void CGib :: WaitTillLand ( void ) +{ + if (!IsInWorld()) + { + UTIL_Remove( this ); + return; + } + + if ( pev->velocity == g_vecZero ) + { + if(m_lifeTime == -1) //waiting for pvs + { + SetThink( PVSRemove ); + SetNextThink( MAX_GIB_LIFETIME ); + } + else + { + SetThink( Fadeout ); + SetNextThink( m_lifeTime ); + } + if(m_bloodColor != DONT_BLEED) + { + CSoundEnt::InsertSound ( bits_SOUND_MEAT, pev->origin, 384, 25 ); + } + } + else SetNextThink( 0.5 ); +} + +void CGib :: BounceGibTouch ( CBaseEntity *pOther ) +{ + Vector vecSpot; + TraceResult tr; + + if ( pOther != g_pWorld) UTIL_FireTargets( pev->target, pOther, this, USE_ON ); + else UTIL_FireTargets( pev->target, pOther, this, USE_SET ); //world touch + + if( pev->flags & FL_ONGROUND ) + { + pev->velocity = pev->velocity * 0.9; + pev->angles.x = 0; + pev->angles.z = 0; + pev->avelocity.x = 0; + pev->avelocity.z = 0; + } + else + { + if (m_cBloodDecals > 0 && m_bloodColor != DONT_BLEED ) + { + vecSpot = pev->origin + Vector ( 0 , 0 , 8 ); //move up a bit, and trace down. + UTIL_TraceLine ( vecSpot, vecSpot + Vector ( 0, 0, -24 ), ignore_monsters, ENT(pev), & tr); + UTIL_BloodDecalTrace( &tr, m_bloodColor ); + m_cBloodDecals--; + } + if ( m_material != None && RANDOM_LONG(0,2) == 0 ) + { + float volume; + float zvel = fabs(pev->velocity.z); + volume = 0.8 * min(1.0, ((float)zvel) / 450.0); + CBaseBrush::PlayRandomSound( edict(), (Materials)m_material, volume ); + } + } +} + +void CGib :: StickyGibTouch ( CBaseEntity *pOther ) +{ + Vector vecSpot; + TraceResult tr; + + if(m_lifeTime == -1) //waiting for pvs + { + SetThink( PVSRemove ); + SetNextThink( MAX_GIB_LIFETIME ); + } + else + { + SetThink( Fadeout ); + SetNextThink( m_lifeTime ); + } + + if ( pOther != g_pWorld ) + { + UTIL_FireTargets( pev->target, pOther, this, USE_ON ); + UTIL_Remove( this ); + return; + } + else UTIL_FireTargets( pev->target, pOther, this, USE_SET ); //world touch + + UTIL_TraceLine ( pev->origin, pev->origin + pev->velocity * 32, ignore_monsters, ENT(pev), & tr); + UTIL_BloodDecalTrace( &tr, m_bloodColor ); + + pev->velocity = tr.vecPlaneNormal * -1; + pev->angles = UTIL_VecToAngles ( pev->velocity ); + pev->velocity = g_vecZero; + pev->avelocity = g_vecZero; + pev->movetype = MOVETYPE_NONE; +} + +CGib *CGib :: CreateGib( CBaseEntity *pVictim, const char *szGibModel, int gibtype ) +{ + CGib *pGib = GetClassPtr( (CGib *)NULL ); + pGib->Spawn( szGibModel ); + + dstudiohdr_t *pstudiohdr = (dstudiohdr_t *)(GET_MODEL_PTR( ENT(pGib->pev) )); + if (! pstudiohdr) return NULL; + dstudiobodyparts_t *pbodypart = (dstudiobodyparts_t *)((byte *)pstudiohdr + pstudiohdr->bodypartindex); + + if ( pVictim ) //spawn at gibbed monster + { + if(gibtype == 1) //skull gib + { + pGib->pev->body = 0; //spawn skull + pGib->pev->origin = pVictim->pev->origin + pVictim->pev->view_ofs; + + edict_t *pentPlayer = FIND_CLIENT_IN_PVS( pGib->edict()); + + if ( RANDOM_LONG ( 0, 100 ) <= 5 && pentPlayer ) + { + // 5% chance head will be thrown at player's face. + entvars_t *pevPlayer; + + pevPlayer = VARS( pentPlayer ); + pGib->pev->velocity = ( ( pevPlayer->origin + pevPlayer->view_ofs ) - pGib->pev->origin ).Normalize() * 300; + pGib->pev->velocity.z += 100; + } + else pGib->pev->velocity = Vector (RANDOM_FLOAT(-100,100), RANDOM_FLOAT(-100,100), RANDOM_FLOAT(200,300)); + } + else + { + pGib->pev->body = RANDOM_LONG(1, pbodypart->nummodels - 1); //random gibs + + // spawn the gib somewhere in the monster's bounding volume + pGib->pev->origin.x = pVictim->pev->absmin.x + pVictim->pev->size.x * (RANDOM_FLOAT ( 0 , 1 ) ); + pGib->pev->origin.y = pVictim->pev->absmin.y + pVictim->pev->size.y * (RANDOM_FLOAT ( 0 , 1 ) ); + pGib->pev->origin.z = pVictim->pev->absmin.z + pVictim->pev->size.z * (RANDOM_FLOAT ( 0 , 1 ) ) + 1; + + // make the gib fly away from the attack vector + pGib->pev->velocity = g_vecAttackDir * -1; + + // mix in some noise + pGib->pev->velocity.x += RANDOM_FLOAT ( -0.25, 0.25 ); + pGib->pev->velocity.y += RANDOM_FLOAT ( -0.25, 0.25 ); + pGib->pev->velocity.z += RANDOM_FLOAT ( -0.25, 0.25 ); + + pGib->pev->velocity = pGib->pev->velocity * RANDOM_FLOAT ( 300, 400 ); + } + + //shared settings + pGib->pev->avelocity.x = RANDOM_FLOAT ( 100, 200 ); + pGib->pev->avelocity.y = RANDOM_FLOAT ( 100, 300 ); + pGib->m_bloodColor = (CBaseEntity::Instance(pVictim->edict()))->BloodColor(); + + if ( pVictim->pev->health > -50) pGib->pev->velocity = pGib->pev->velocity * 0.7; + else if ( pVictim->pev->health > -200) pGib->pev->velocity = pGib->pev->velocity * 2; + else pGib->pev->velocity = pGib->pev->velocity * 4; + + //LimitVelocity + float length = pGib->pev->velocity.Length(); + if ( length > (MAX_VELOCITY / 2)) + pGib->pev->velocity = pGib->pev->velocity.Normalize() * MAX_VELOCITY / 2; + } + + // global settings + UTIL_SetSize ( pGib->pev, g_vecZero, g_vecZero ); + + if(gibtype == 0 || gibtype == 1) //normal random gibs + { + pGib->pev->solid = SOLID_BBOX; + pGib->SetTouch( BounceGibTouch ); + pGib->SetThink( WaitTillLand ); + pGib->SetNextThink( 4 ); + } + if(gibtype == 2) //sticky gib + { + pGib->pev->movetype = MOVETYPE_TOSS; + pGib->SetTouch( StickyGibTouch ); + } + return pGib; +} + +//========================================================= +// env_shooter (sprite or models shooter) +//========================================================= +class CEnvShooter : public CBaseLogic +{ + void Precache( void ) { if(ParseGibFile()) Setup(); } + void KeyValue( KeyValueData *pkvd ); + void Spawn( void ); + int ParseGibFile( void ); + void Setup( void ); + void Think( void ); + CBaseEntity *CreateGib( Vector vecPos, Vector vecVel ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + //this is will be restoring on precache - no need to save + Materials m_Material; + Physics m_Physics; + string_t entity; + + RandomRange velocity; + RandomRange variance; + RandomRange lifetime; +}; +LINK_ENTITY_TO_CLASS( env_shooter, CEnvShooter ); + +void CEnvShooter::Spawn( void ) +{ + Precache(); + pev->effects = EF_NODRAW; + if ( m_flDelay == 0 ) m_flDelay = 0.1; + UTIL_LinearVector( this );//movement direction +} + +void CEnvShooter :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "file")) + { + pev->message = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "gibcount")) + { + pev->impulse = pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "gibname")) + { + m_sSet = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "gibtarget")) + { + pev->netname = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + + +void CEnvShooter::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + SetNextThink( 0 ); +} + +void CEnvShooter :: Think ( void ) +{ + TraceResult tr; //don't create stack monsters + UTIL_TraceLine ( pev->origin, pev->origin - Vector ( 0, 0, 2048 ), ignore_monsters, ENT(pev), &tr ); + Vector mins = pev->origin - Vector( 34, 34, 0 ); + Vector maxs = pev->origin + Vector( 34, 34, 0 ); + maxs.z = pev->origin.z; + mins.z = tr.vecEndPos.z; + + CBaseEntity *pList[2]; + if (UTIL_EntitiesInBox( pList, 2, mins, maxs, FL_CLIENT|FL_MONSTER )) + { + // don't build a stack of monsters! + SetNextThink( m_flDelay ); + return; + } + Vector vecShootDir = pev->movedir; + vecShootDir = vecShootDir + gpGlobals->v_up * variance.Random(); + vecShootDir = vecShootDir + gpGlobals->v_right * variance.Random(); + vecShootDir = vecShootDir + gpGlobals->v_forward * variance.Random(); + vecShootDir = vecShootDir.Normalize(); + CBaseEntity *pGib = CreateGib(pev->origin, vecShootDir * velocity.Random() ); + + if ( pGib )UTIL_FireTargets( pev->target, pGib, this, USE_TOGGLE, 0 ); + if(pev->button > 0) pev->impulse--; + + if ( pev->impulse <= 0 ) + { + if ( pev->spawnflags & SF_FIREONCE ) + { + UTIL_Remove( this ); + return; + } + else + { + pev->impulse = pev->button; + DontThink(); + } + } + SetNextThink( m_flDelay ); +} + + +void CEnvShooter :: Setup( void ) +{ + //Smart field system ® + if(!FStringNull( m_sMaster )) UTIL_PrecacheModel( m_sMaster ); + if(!FStringNull( entity )) UTIL_PrecacheEntity( entity ); + if(velocity.m_flMax == 0) velocity.m_flMax = 100; + if(velocity.m_flMax > MAX_VELOCITY) velocity.m_flMax = MAX_VELOCITY; + if(lifetime.Random() == 0)lifetime = RandomRange( -1.0 ); + if(pev->scale == 0)pev->scale = 1; + CBaseBrush::MaterialSoundPrecache( m_Material ); + + //setup physics + pev->solid = SOLID_SLIDEBOX; + switch( m_Physics ) + { + case Noclip: pev->team = MOVETYPE_NOCLIP; pev->solid = SOLID_NOT; break; + case Bounce: pev->team = MOVETYPE_BOUNCE; break; + case Sticky: pev->team = MOVETYPE_TOSS; pev->solid = SOLID_BBOX; break; + case Fly: pev->team = MOVETYPE_FLY; break; + case Toss: pev->team = MOVETYPE_TOSS; break; + case WalkStep: pev->team = MOVETYPE_STEP; break; + } +} + +int CEnvShooter :: ParseGibFile( void ) +{ + char token[256]; + char *pfile = (char *)LOAD_FILE( (char *)STRING(pev->message), NULL ); + + if(!pfile) + { + Msg( "Warning: Gib script file for %s not found!\n", STRING(pev->targetname) ); + return 0; + } + else + { + + pfile = COM_ParseFile(pfile, token); + while(pfile) + { + if ( !stricmp( token, "model" )) + { + pfile = COM_ParseFile(pfile, token); + m_sMaster = ALLOC_STRING( token ); + } + else if ( !stricmp( token, "entity" )) + { + pfile = COM_ParseFile(pfile, token); + entity = ALLOC_STRING( token ); + } + else if ( !stricmp( token, "speed" )) + { + pfile = COM_ParseFile(pfile, token); + velocity = RandomRange((char *)STRING(ALLOC_STRING(token))); + } + else if ( !stricmp( token, "variance" )) + { + pfile = COM_ParseFile(pfile, token); + variance = RandomRange((char *)STRING(ALLOC_STRING(token))); + } + else if ( !stricmp( token, "livetime" )) + { + pfile = COM_ParseFile(pfile, token); + lifetime = RandomRange((char *)STRING(ALLOC_STRING(token))); + } + else if ( !stricmp( token, "friction" )) + { + pfile = COM_ParseFile(pfile, token); + RandomRange((char *)STRING(ALLOC_STRING(token))); + Msg("param %s, min %g max %g\n", token, m_flMin, m_flMax ); + pev->friction = RANDOM_FLOAT( m_flMin, m_flMax ); + } + else if ( !stricmp( token, "scale" )) + { + pfile = COM_ParseFile(pfile, token); + RandomRange((char *)STRING(ALLOC_STRING(token))); + Msg("param %s, min %g max %g\n", token, m_flMin, m_flMax ); + pev->scale = RANDOM_FLOAT( m_flMin, m_flMax ); + } + else if ( !stricmp( token, "body" )) + { + pfile = COM_ParseFile(pfile, token); + RandomRange((char *)STRING(ALLOC_STRING(token))); + Msg("param %s, min %g max %g\n", token, m_flMin, m_flMax ); + pev->body = RANDOM_FLOAT( m_flMin, m_flMax - 1 ); + } + else if ( !stricmp( token, "skin" )) + { + pfile = COM_ParseFile(pfile, token); + RandomRange((char *)STRING(ALLOC_STRING(token))); + Msg("param %s, min %g max %g\n", token, m_flMin, m_flMax ); + pev->skin = RANDOM_FLOAT( m_flMin, m_flMax ); + } + else if ( !stricmp( token, "frame" )) + { + pfile = COM_ParseFile(pfile, token); + RandomRange((char *)STRING(ALLOC_STRING(token))); + Msg("param %s, min %g max %g\n", token, m_flMin, m_flMax ); + pev->frame = RANDOM_FLOAT( m_flMin, m_flMax ); + } + else if ( !stricmp( token, "alpha" )) + { + pfile = COM_ParseFile(pfile, token); + RandomRange((char *)STRING(ALLOC_STRING(token))); + Msg("param %s, min %g max %g\n", token, m_flMin, m_flMax ); + pev->renderamt = RANDOM_FLOAT( m_flMin, m_flMax ); + } + else if ( !stricmp( token, "color" )) + { + pfile = COM_ParseFile(pfile, token); + UTIL_StringToVector((float *)pev->rendercolor, token ); + } + else if ( !stricmp( token, "rendermode" )) + { + pfile = COM_ParseFile(pfile, token); + if ( !stricmp( token, "normal" )) pev->rendermode = kRenderNormal; + else if ( !stricmp( token, "color" )) pev->rendermode = kRenderTransColor; + else if ( !stricmp( token, "texture" )) pev->rendermode = kRenderTransTexture; + else if ( !stricmp( token, "glow" )) pev->rendermode = kRenderGlow; + else if ( !stricmp( token, "solid" )) pev->rendermode = kRenderTransAlpha; + else if ( !stricmp( token, "additive" )) pev->rendermode = kRenderTransAdd; + } + else if ( !stricmp( token, "sfx" )) + { + pfile = COM_ParseFile(pfile, token); + if ( !stricmp( token, "hologramm" )) pev->renderfx = kRenderFxHologram; + else if ( !stricmp( token, "glowshell" )) pev->renderfx = kRenderFxGlowShell; + } + else if ( !stricmp( token, "size" )) + { + UTIL_StringToVector((float*)pev->size, token); + pev->size = pev->size/2; + } + else if ( !stricmp( token, "material" )) + { + pfile = COM_ParseFile(pfile, token); + if ( !stricmp( token, "none" )) m_Material = None; + else if ( !stricmp( token, "bones" )) m_Material = Bones; + else if ( !stricmp( token, "flesh" )) m_Material = Flesh; + else if ( !stricmp( token, "cinder block" )) m_Material = CinderBlock; + else if ( !stricmp( token, "concrete" )) m_Material = Concrete; + else if ( !stricmp( token, "rocks" )) m_Material = Rocks; + else if ( !stricmp( token, "computer" )) m_Material = Computer; + else if ( !stricmp( token, "glass" )) m_Material = Glass; + else if ( !stricmp( token, "metalplate" )) m_Material = MetalPlate; + else if ( !stricmp( token, "metal" )) m_Material = Metal; + else if ( !stricmp( token, "airduct" )) m_Material = AirDuct; + else if ( !stricmp( token, "ceiling tile" )) m_Material = CeilingTile; + else if ( !stricmp( token, "wood" )) m_Material = Wood; + } + else if ( !stricmp( token, "physics" )) + { + pfile = COM_ParseFile(pfile, token); + if ( !stricmp( token, "bounce" )) m_Physics = Bounce; + else if ( !stricmp( token, "sticky" )) m_Physics = Sticky; + else if ( !stricmp( token, "noclip" )) m_Physics = Noclip; + else if ( !stricmp( token, "fly" )) m_Physics = Fly; + else if ( !stricmp( token, "toss" )) m_Physics = Toss; + } else if ( !stricmp( token, "walk" )) m_Physics = WalkStep; + pfile = COM_ParseFile(pfile, token); + } + COM_FreeFile( pfile ); + if(FStringNull( m_sMaster) && FStringNull( entity )) + { + Msg("Warning: model or entity not specified for %s\n", STRING(pev->targetname)); + return 0; + } + return 1; + } +} + +CBaseEntity *CEnvShooter :: CreateGib ( Vector vecPos, Vector vecVel ) +{ + if( !FStringNull( entity ))//custom precached entity + { + CBaseEntity *pEnt = CBaseEntity::CreateGib( (char *)STRING( entity ), (char *)STRING( m_sMaster)); + //CBaseEntity *pEnt = CBaseEntity::Create( (char *)STRING( entity ), pev->origin, g_vecZero, edict() ); + //if(!pEnt && pEnt->edict()) return NULL; + if(!pEnt) return NULL; + + //UTIL_SetOrigin( pEnt, vecPos ); + pEnt->pev->origin = vecPos; + pEnt->pev->velocity = vecVel; + pEnt->pev->renderamt = pev->renderamt; + //pEnt->pev->model = m_sMaster; + pEnt->pev->rendermode = pev->rendermode; + pEnt->pev->rendercolor = pev->rendercolor; + pEnt->pev->renderfx = pev->renderfx; + pEnt->pev->target = pev->netname; + pEnt->pev->skin = pev->skin; + pEnt->pev->body = pev->body; + pEnt->pev->scale = pev->scale; + pEnt->pev->frame = pev->frame; + pEnt->pev->friction = pev->friction; + pEnt->pev->movetype = pev->team; + pEnt->pev->solid = pev->solid; + pEnt->pev->targetname = m_sSet; + + //UTIL_SetModel(ENT(pEnt->pev), m_sMaster ); + return pEnt; + } + else if(m_Physics == Bounce || m_Physics == Sticky) // normal or sticky gib + { + CGib *pGib = CGib::CreateGib( NULL, (char *)STRING( m_sMaster ), m_Physics ); + if(!pGib && pGib->edict()) return NULL; + + pGib->pev->body = pev->body; + pGib->pev->origin = vecPos; + pGib->pev->velocity = vecVel; + pGib->m_material = m_Material; + pGib->pev->rendermode = pev->rendermode; + pGib->pev->renderamt = pev->renderamt; + pGib->pev->rendercolor = pev->rendercolor; + pGib->pev->renderfx = pev->renderfx; + pGib->pev->target = pev->netname; + pGib->pev->friction = pev->friction; + pGib->pev->scale = pev->scale; + pGib->pev->skin = pev->skin; + pGib->pev->avelocity.x = RANDOM_FLOAT ( 100, 200 ); + pGib->pev->avelocity.y = RANDOM_FLOAT ( 100, 300 ); + pGib->m_lifeTime = pev->health; + pGib->pev->scale = pev->scale; + pGib->pev->frame = pev->frame; + pGib->pev->targetname = m_sSet; + + return pGib; + } + else //not custom entity, other physics type + { + CShot *pShot = CShot::CreateShot ( (char *)STRING( m_sMaster ), pev->size ); + if(!pShot && pShot->edict()) return NULL; + + pShot->pev->origin = vecPos; + pShot->pev->velocity = vecVel; + pShot->pev->renderamt = pev->renderamt; + pShot->pev->rendermode = pev->rendermode; + pShot->pev->rendercolor = pev->rendercolor; + pShot->pev->renderfx = pev->renderfx; + pShot->pev->target = pev->netname; + pShot->pev->skin = pev->skin; + pShot->pev->body = pev->body; + pShot->pev->scale = pev->scale; + pShot->pev->frame = pev->frame; + pShot->pev->framerate = pev->framerate; + pShot->pev->friction = pev->friction; + pShot->pev->movetype = pev->team; + pShot->pev->targetname = m_sSet; + + if (pev->health || pev->health == -1) + { + //animate it + if (pShot->pev->framerate && pShot->Frames() > 1.0) + { + pShot->AnimateAndDie( 10 ); + pShot->pev->pain_finished = gpGlobals->time + pev->health; + pShot->SetNextThink( 0 ); + pShot->pev->dmgtime = gpGlobals->time; + } + else if( pev->health ) + { + pShot->SetThink( Fadeout ); + pShot->SetNextThink(pev->health); + } + else + { + pShot->SetThink( PVSRemove ); + pShot->SetNextThink( MAX_GIB_LIFETIME ); + } + } + return pShot; + } + return NULL; +} + +//========================================================= +// LRC - Decal effect +//========================================================= +class CEnvDecal : public CPointEntity +{ +public: + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData *pkvd ); +}; + +LINK_ENTITY_TO_CLASS( env_decal, CEnvDecal ); + +void CEnvDecal :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "texture")) + { + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CEnvDecal::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if( useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg("Decal name %s\n\n", GetStringForDecalName( pev->message )); + } + else + { + int m_decal = UTIL_LoadDecalPreset( pev->message ); + switch(m_decal) + { + case 1: pev->skin = gDecals[ DECAL_GUNSHOT1 + RANDOM_LONG(0,4)].index; break; + case 2: pev->skin = gDecals[ DECAL_BLOOD1 + RANDOM_LONG(0,5)].index; break; + case 3: pev->skin = gDecals[ DECAL_YBLOOD1 + RANDOM_LONG(0,5)].index; break; + case 4: pev->skin = gDecals[ DECAL_GLASSBREAK1 + RANDOM_LONG(0,2)].index; break; + case 5: pev->skin = gDecals[ DECAL_BIGSHOT1 + RANDOM_LONG(0,4)].index; break; + case 6: pev->skin = gDecals[ DECAL_SCORCH1 + RANDOM_LONG(0,1)].index; break; + case 7: pev->skin = gDecals[ DECAL_SPIT1 + RANDOM_LONG(0,1)].index; break; + default: pev->skin = DECAL_INDEX(STRING(m_decal)); break; + } + + Vector vecPos = pev->origin; + UTIL_MakeVectors(pev->angles); + Vector vecOffs = gpGlobals->v_forward; + vecOffs = vecOffs.Normalize() * MAP_HALFSIZE; + TraceResult trace; + UTIL_TraceLine( vecPos, vecPos+vecOffs, ignore_monsters, NULL, &trace ); + if (trace.flFraction == 1.0) return; // didn't hit anything, oh well + int entityIndex = (short)ENTINDEX(trace.pHit); + SFX_Decal( trace.vecEndPos, pev->skin, entityIndex, (int)VARS(trace.pHit)->modelindex ); + } +} + +//========================================================= +// env_warpball +//========================================================= +#define SF_REMOVE_ON_FIRE 0x0001 +#define SF_KILL_CENTER 0x0002 + +class CEnvWarpBall : public CBaseLogic +{ +public: + void Precache( void ); + void Spawn( void ) { Precache(); } + void KeyValue( KeyValueData *pkvd ); + void Think( void ); + void Affect( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + Vector vecOrigin; +}; +LINK_ENTITY_TO_CLASS( env_warpball, CEnvWarpBall ); + +void CEnvWarpBall :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "radius")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "warp_target")) + { + pev->message = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "damage_delay")) + { + pev->frags = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CEnvWarpBall::Precache( void ) +{ + UTIL_PrecacheModel( "sprites/lgtning.spr" ); + UTIL_PrecacheModel( "sprites/Fexplo1.spr" ); + UTIL_PrecacheSound( "debris/beamstart2.wav" ); + UTIL_PrecacheSound( "debris/beamstart7.wav" ); +} + +void CEnvWarpBall::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + pActivator = m_hActivator; //save it for later + + if (useType == USE_TOGGLE || useType == USE_ON) Affect(); + else if( useType == USE_SET && value > 10) pev->button = value; + else if( useType == USE_SHOWINFO ) + { + DEBUGHEAD; + Msg( "Radius: %d, Warp target %s\n\n", pev->button, STRING( pev->message )); + } +} + +void CEnvWarpBall::Affect( void ) +{ + int iTimes = 0; + int iDrawn = 0; + TraceResult tr; + Vector vecDest; + CBeam *pBeam; + CBaseEntity *pEntity = UTIL_FindEntityByTargetname ( NULL, STRING(pev->message)); + edict_t *pos; + + if(pEntity)//target found ? + { + vecOrigin = pEntity->pev->origin; + pos = pEntity->edict(); + } + else + { //use as center + vecOrigin = pev->origin; + pos = edict(); + } + EMIT_SOUND( pos, CHAN_BODY, "debris/beamstart2.wav", 1, ATTN_NORM ); + UTIL_ScreenShake( vecOrigin, 6, 160, 1.0, pev->button ); + CSprite *pSpr = CSprite::SpriteCreate( "sprites/Fexplo1.spr", vecOrigin, TRUE ); + pSpr->SetTransparency(kRenderGlow, 77, 210, 130, 255, kRenderFxNoDissipation); + pSpr->AnimateAndDie( 18 ); + + EMIT_SOUND( pos, CHAN_ITEM, "debris/beamstart7.wav", 1, ATTN_NORM ); + int iBeams = RANDOM_LONG(20, 40); + while (iDrawn < iBeams && iTimes < (iBeams * 3)) + { + vecDest = pev->button * (Vector(RANDOM_FLOAT(-1,1), RANDOM_FLOAT(-1,1), RANDOM_FLOAT(-1,1)).Normalize()); + UTIL_TraceLine( vecOrigin, vecOrigin + vecDest, ignore_monsters, NULL, &tr); + if (tr.flFraction != 1.0) + { + // we hit something. + iDrawn++; + pBeam = CBeam::BeamCreate("sprites/lgtning.spr", 200); + pBeam->PointsInit( vecOrigin, tr.vecEndPos ); + pBeam->SetColor( 20, 243, 20 ); + pBeam->SetNoise( 65 ); + pBeam->SetBrightness( 220 ); + pBeam->SetWidth( 30 ); + pBeam->SetScrollRate( 35 ); + pBeam->SetThink( Remove ); + pBeam->pev->nextthink = gpGlobals->time + RANDOM_FLOAT(0.5, 1.6); + } + iTimes++; + } + SetNextThink( pev->frags ); +} + +void CEnvWarpBall::Think( void ) +{ + FireTargets(); + + if ( pev->spawnflags & SF_KILL_CENTER ) //Blue-Shift strange feature + { + CBaseEntity *pMonster = NULL; + while ((pMonster = UTIL_FindEntityInSphere( pMonster, vecOrigin, 72 )) != NULL) + { + if ( FBitSet( pMonster->pev->flags, FL_MONSTER ) || FBitSet( pMonster->pev->flags, FL_CLIENT )) + pMonster->TakeDamage ( pev, pev, 100, DMG_GENERIC ); + } + } + if ( pev->spawnflags & SF_REMOVE_ON_FIRE ) UTIL_Remove( this ); +} + +//======================================================================= +// base beams () +//======================================================================= +LINK_ENTITY_TO_CLASS( beam, CBeam ); + +void CBeam::Spawn( void ) +{ + Precache(); + pev->solid = SOLID_NOT; +} + +void CBeam::Precache( void ) +{ + if ( pev->owner ) SetStartEntity( ENTINDEX( pev->owner ) ); + if ( pev->aiment ) SetEndEntity( ENTINDEX( pev->aiment ) ); +} + +void CBeam::SetStartEntity( int entityIndex ) +{ + pev->sequence = (entityIndex & 0x0FFF) | ((pev->sequence&0xF000)<<12); + pev->owner = g_engfuncs.pfnPEntityOfEntIndex( entityIndex ); +} + +void CBeam::SetEndEntity( int entityIndex ) +{ + pev->skin = (entityIndex & 0x0FFF) | ((pev->skin&0xF000)<<12); + pev->aiment = g_engfuncs.pfnPEntityOfEntIndex( entityIndex ); +} + +const Vector &CBeam::GetStartPos( void ) +{ + if ( GetType() == BEAM_ENTS ) + { + edict_t *pent = g_engfuncs.pfnPEntityOfEntIndex( GetStartEntity() ); + return pent->v.origin; + } + return pev->origin; +} + +const Vector &CBeam::GetEndPos( void ) +{ + int type = GetType(); + if ( type == BEAM_POINTS || type == BEAM_HOSE ) return pev->angles; + + edict_t *pent = g_engfuncs.pfnPEntityOfEntIndex( GetEndEntity() ); + if ( pent ) return pent->v.origin; + return pev->angles; +} + +CBeam *CBeam::BeamCreate( const char *pSpriteName, int width ) +{ + // Create a new entity with CBeam private data + CBeam *pBeam = GetClassPtr( (CBeam *)NULL ); + pBeam->pev->classname = MAKE_STRING("beam"); + pBeam->BeamInit( pSpriteName, width ); + + return pBeam; +} + +void CBeam::BeamInit( const char *pSpriteName, int width ) +{ + pev->flags |= FL_CUSTOMENTITY; + SetColor( 255, 255, 255 ); + SetBrightness( 255 ); + SetNoise( 0 ); + SetFrame( 0 ); + SetScrollRate( 0 ); + pev->model = MAKE_STRING( pSpriteName ); + SetTexture( UTIL_PrecacheModel( pev->model ) ); + SetWidth( width ); + pev->skin = 0; + pev->sequence = 0; + pev->rendermode = 0; +} + +void CBeam::PointsInit( const Vector &start, const Vector &end ) +{ + SetType( BEAM_POINTS ); + SetStartPos( start ); + SetEndPos( end ); + SetStartAttachment( 0 ); + SetEndAttachment( 0 ); + RelinkBeam(); +} + +void CBeam::HoseInit( const Vector &start, const Vector &direction ) +{ + SetType( BEAM_HOSE ); + SetStartPos( start ); + SetEndPos( direction ); + SetStartAttachment( 0 ); + SetEndAttachment( 0 ); + RelinkBeam(); +} + +void CBeam::PointEntInit( const Vector &start, int endIndex ) +{ + SetType( BEAM_ENTPOINT ); + SetStartPos( start ); + SetEndEntity( endIndex ); + SetStartAttachment( 0 ); + SetEndAttachment( 0 ); + RelinkBeam(); +} + +void CBeam::EntsInit( int startIndex, int endIndex ) +{ + SetType( BEAM_ENTS ); + SetStartEntity( startIndex ); + SetEndEntity( endIndex ); + SetStartAttachment( 0 ); + SetEndAttachment( 0 ); + RelinkBeam(); +} + +void CBeam::RelinkBeam( void ) +{ + const Vector &startPos = GetStartPos(), &endPos = GetEndPos(); + + pev->mins.x = min( startPos.x, endPos.x ); + pev->mins.y = min( startPos.y, endPos.y ); + pev->mins.z = min( startPos.z, endPos.z ); + pev->maxs.x = max( startPos.x, endPos.x ); + pev->maxs.y = max( startPos.y, endPos.y ); + pev->maxs.z = max( startPos.z, endPos.z ); + pev->mins = pev->mins - pev->origin; + pev->maxs = pev->maxs - pev->origin; + + UTIL_SetSize( pev, pev->mins, pev->maxs ); + UTIL_SetOrigin( this, pev->origin ); +} + +void CBeam::SetObjectCollisionBox( void ) +{ + const Vector &startPos = GetStartPos(), &endPos = GetEndPos(); + + pev->absmin.x = min( startPos.x, endPos.x ); + pev->absmin.y = min( startPos.y, endPos.y ); + pev->absmin.z = min( startPos.z, endPos.z ); + pev->absmax.x = max( startPos.x, endPos.x ); + pev->absmax.y = max( startPos.y, endPos.y ); + pev->absmax.z = max( startPos.z, endPos.z ); +} + +void CBeam::Touch( CBaseEntity *pOther ) +{ + if ( pOther->pev->flags & (FL_CLIENT | FL_MONSTER) ) + { + if ( pev->owner ) + { + CBaseEntity *pOwner = CBaseEntity::Instance(pev->owner); + pOwner->Use( pOther, this, USE_TOGGLE, 0 ); + } + } +} + +CBaseEntity* CBeam::GetTripEntity( TraceResult *ptr ) +{ + CBaseEntity* pTrip; + + if (ptr->flFraction == 1.0 || ptr->pHit == NULL) return NULL; + + pTrip = CBaseEntity::Instance(ptr->pHit); + if (pTrip == NULL) return NULL; + + if (pTrip->pev->flags & (FL_CLIENT | FL_MONSTER ) && !pTrip->IsPushable())//physics ents can move too + return pTrip; + return NULL; +} + +void CBeam::BeamDamage( TraceResult *ptr ) +{ + RelinkBeam(); + if ( ptr->flFraction != 1.0 && ptr->pHit != NULL ) + { + CBaseEntity *pHit = CBaseEntity::Instance(ptr->pHit); + if ( pHit ) + { + if (pev->dmg > 0) + { + ClearMultiDamage(); + pHit->TraceAttack( pev, pev->dmg * (gpGlobals->time - pev->dmgtime), (ptr->vecEndPos - pev->origin).Normalize(), ptr, DMG_ENERGYBEAM ); + ApplyMultiDamage( pev, pev ); + if ( pev->dmg > 40 && pHit->IsBSPModel())//wall damage + { + UTIL_DecalTrace( ptr, DECAL_BIGSHOT1 + RANDOM_LONG(0,4) ); + } + } + else pHit->TakeHealth( -(pev->dmg * (gpGlobals->time - pev->dmgtime)), DMG_GENERIC ); + } + } + pev->dmgtime = gpGlobals->time; +} + +CBaseEntity *CBeam::RandomTargetname( const char *szName ) +{ + int total = 0; + + CBaseEntity *pEntity = NULL; + CBaseEntity *pNewEntity = NULL; + while ((pNewEntity = UTIL_FindEntityByTargetname( pNewEntity, szName )) != NULL) + { + total++; + if (RANDOM_LONG(0,total-1) < 1) pEntity = pNewEntity; + } + return pEntity; +} + +void CBeam::DoSparks( const Vector &start, const Vector &end ) +{ + if ( pev->dmg > 100 ) UTIL_Sparks( start ); + if ( pev->dmg > 40 ) UTIL_Sparks( end ); +} + +//======================================================================= +// env_beam - toggleable beam +//======================================================================= +class CEnvBeam : public CBeam +{ +public: + void Spawn( void ); + void Precache( void ); + void KeyValue( KeyValueData *pkvd ); + void Activate( void ); + void Think( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + int IsPointEntity( CBaseEntity *pEnt ); + + void BeamUpdatePoints( void ); + void BeamUpdateVars( void ); + + CBaseEntity *pEnd; +}; +LINK_ENTITY_TO_CLASS( env_beam, CEnvBeam ); + +void CEnvBeam::Precache( void ) +{ + pev->team = UTIL_PrecacheModel( pev->message, "sprites/laserbeam.spr" ); + CBeam::Precache(); +} + +int CEnvBeam::IsPointEntity( CBaseEntity *pEnt ) +{ + if (pEnt->pev->modelindex && !(pEnt->pev->flags & FL_CUSTOMENTITY)) + return 0; + return 1; +} + +void CEnvBeam::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "endpoint")) + { + pev->netname = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "damage")) + { + pev->dmg = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "sprite")) + { + pev->message = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "width")) + { + SetWidth( atoi(pkvd->szValue)); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "noisewidth")) + { + SetNoise( atoi(pkvd->szValue)); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "shade")) + { + pev->frags = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBeam::KeyValue( pkvd ); +} + +void CEnvBeam::BeamUpdateVars( void ) +{ + pev->skin = 0; + pev->sequence = 0; + pev->rendermode = 0; + pev->flags |= FL_CUSTOMENTITY; + SetTexture( pev->team ); + + BeamUpdatePoints(); + SetFrame( 0 ); + SetScrollRate( pev->speed ); + if ( pev->frags == 1 ) SetFlags( BEAM_FSHADEIN ); + if ( pev->frags == 2 ) SetFlags( BEAM_FSHADEOUT ); + if ( pev->renderamt == 255 ) SetFlags( BEAM_FSOLID ); + else SetFlags( 0 ); +} + +void CEnvBeam::Activate( void ) +{ + BeamUpdateVars(); + CBeam::Activate(); +} + + +void CEnvBeam::BeamUpdatePoints( void ) +{ + int beamType; + + pEnd = UTIL_FindEntityByTargetname ( NULL, STRING(pev->netname) ); + if (!pEnd) return; + int pointEnd = IsPointEntity( pEnd ); + + beamType = BEAM_ENTS; + if ( !pointEnd ) beamType = BEAM_ENTPOINT; + else beamType = BEAM_POINTS; + + SetType( beamType ); + if ( beamType == BEAM_POINTS || beamType == BEAM_ENTPOINT || beamType == BEAM_HOSE ) + { + SetStartPos( pev->origin ); + if ( beamType == BEAM_POINTS || beamType == BEAM_HOSE ) + SetEndPos( pEnd->pev->origin ); + else SetEndEntity( ENTINDEX(ENT(pEnd->pev)) ); + } + else + { + SetStartEntity(ENTINDEX(ENT(pev))); + SetEndEntity( ENTINDEX(ENT(pEnd->pev)) ); + } + + RelinkBeam(); +} + +void CEnvBeam::Spawn( void ) +{ + UTIL_SetModel(edict(), "sprites/null.spr");//beam start point + pev->solid = SOLID_NOT; // Remove model & collisions + Precache(); + + if (FStringNull(pev->netname)) + { + Msg( "Warning: %s end entity not found!\n", STRING(pev->classname) ); + SetThink( Remove ); + SetNextThink( 0 ); + return; + } + + pev->dmgtime = gpGlobals->time; + if (pev->rendercolor == g_vecZero) pev->rendercolor = Vector(255, 255, 255); + if(pev->spawnflags & SF_START_ON) Use( this, this, USE_ON, 0 ); +} + +void CEnvBeam::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + m_iState = STATE_ON; + BeamUpdatePoints(); + pev->effects &= ~EF_NODRAW; + SetNextThink( 0 ); + pev->dmgtime = gpGlobals->time; + } + else if (useType == USE_OFF) + { + m_iState = STATE_OFF; + pev->effects |= EF_NODRAW; + DontThink(); + } + else if(useType == USE_SET) //set new damage level + { + pev->dmg = value; + } + else if(useType == USE_RESET) //set new brightness + { + if(value)pev->renderamt = value; + BeamUpdateVars(); + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Damage %g\n", GetStringForState( GetState()), pev->dmg ); + Msg("Beam model %s\n", STRING( pev->message )); + } +} + +void CEnvBeam::Think( void ) +{ + if ( pev->dmg || !FStringNull(pev->message))//apply damage & trip entity + { + TraceResult tr; + UTIL_TraceLine( pev->origin, pEnd->pev->origin, dont_ignore_monsters, NULL, &tr ); + BeamDamage( &tr ); + + CBaseEntity* pTrip = GetTripEntity( &tr ); + if (pTrip) + { + if (!FBitSet(pev->spawnflags, SF_BEAM_TRIPPED)) + { + UTIL_FireTargets(pev->target, pTrip, this, USE_TOGGLE); + pev->spawnflags |= SF_BEAM_TRIPPED; + } + } + else pev->spawnflags &= ~SF_BEAM_TRIPPED; + } + SetNextThink( 0.1 ); +} + +//======================================================================= +// env_laser - classic HALF-LIFE laser +//======================================================================= +LINK_ENTITY_TO_CLASS( env_laser, CLaser ); + +TYPEDESCRIPTION CLaser::m_SaveData[] = +{ + DEFINE_FIELD( CLaser, m_pStartSprite, FIELD_CLASSPTR ), + DEFINE_FIELD( CLaser, m_pEndSprite, FIELD_CLASSPTR ), +}; + +IMPLEMENT_SAVERESTORE( CLaser, CBeam ); + +void CLaser::Spawn( void ) +{ + pev->frame = 0; + pev->solid = SOLID_NOT; // Remove model & collisions + Precache(); + pev->flags |= FL_CUSTOMENTITY; +} + +void CLaser::Activate( void ) +{ + if ( m_pStartSprite && m_pEndSprite ) + EntsInit( m_pStartSprite->entindex(), m_pEndSprite->entindex() ); +} + +void CLaser::SetObjectCollisionBox( void ) +{ + if ( m_pStartSprite && m_pEndSprite ) + { + const Vector &startPos = m_pStartSprite->pev->origin, &endPos = m_pEndSprite->pev->origin; + pev->absmin.x = min( startPos.x, endPos.x ); + pev->absmin.y = min( startPos.y, endPos.y ); + pev->absmin.z = min( startPos.z, endPos.z ); + pev->absmax.x = max( startPos.x, endPos.x ); + pev->absmax.y = max( startPos.y, endPos.y ); + pev->absmax.z = max( startPos.z, endPos.z ); + } + else + { + pev->absmin = g_vecZero; + pev->absmax = g_vecZero; + } +} + +void CLaser::PostSpawn( void ) +{ + if (!FStringNull(pev->netname)) + { + m_pStartSprite = CSprite::SpriteCreate( (char *)STRING(pev->netname), pev->origin, TRUE ); + if (m_pStartSprite) m_pStartSprite->SetTransparency( kRenderGlow, pev->rendercolor.x, pev->rendercolor.y, pev->rendercolor.z, pev->renderamt, pev->renderfx ); + else m_pStartSprite = CSprite::SpriteCreate( "sprites/null.spr", pev->origin, TRUE ); + } + else m_pStartSprite = CSprite::SpriteCreate( "sprites/null.spr", pev->origin, TRUE ); + m_pEndSprite = CSprite::SpriteCreate( "sprites/null.spr", pev->origin, TRUE ); + + if ( pev->spawnflags & SF_START_ON ) TurnOn(); + else TurnOff(); +} + +void CLaser::Precache( void ) +{ + pev->modelindex = UTIL_PrecacheModel( pev->model, "sprites/laserbeam.spr" ); + UTIL_PrecacheModel( pev->netname ); +} + +void CLaser::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "sprite")) + { + pev->model = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "startsprite")) + { + pev->netname = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "width")) + { + SetWidth( atof(pkvd->szValue) ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "noisewidth")) + { + SetNoise( atoi(pkvd->szValue) ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "damage")) + { + pev->dmg = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBeam::KeyValue( pkvd ); +} + +void CLaser::TurnOff( void ) +{ + SetBits(pev->effects, EF_NODRAW ); + m_iState = STATE_OFF; + if ( m_pStartSprite ) + { + m_pStartSprite->TurnOff(); + UTIL_SetVelocity(m_pStartSprite, g_vecZero); + } + if ( m_pEndSprite ) + { + m_pEndSprite->TurnOff(); + UTIL_SetVelocity(m_pEndSprite, g_vecZero); + } + DontThink(); +} + +void CLaser::TurnOn( void ) +{ + ClearBits( pev->effects, EF_NODRAW ); + + if ( m_pStartSprite ) + m_pStartSprite->TurnOn(); + + if ( m_pEndSprite ) + m_pEndSprite->TurnOn(); + + pev->dmgtime = gpGlobals->time; + m_iState = STATE_ON; + SetScrollRate( pev->speed ); + SetFlags( BEAM_FSHADEOUT ); + SetNextThink( 0 ); +} + +void CLaser::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) TurnOn(); + else if(useType == USE_OFF) TurnOff(); + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Damage %g\n", GetStringForState( GetState()), pev->dmg ); + Msg("Laser model %s\n", STRING( pev->model )); + } +} + +void CLaser::FireAtPoint( Vector startpos, TraceResult &tr ) +{ + if (m_pStartSprite && m_pEndSprite) + { + UTIL_SetVelocity(m_pStartSprite, (startpos - m_pStartSprite->pev->origin)*100 ); + UTIL_SetVelocity(m_pEndSprite, (tr.vecEndPos - m_pEndSprite->pev->origin)*100 ); + } + BeamDamage( &tr ); + DoSparks( startpos, tr.vecEndPos ); +} + +void CLaser:: Think( void ) +{ + Vector startpos = pev->origin; + TraceResult tr; + + UTIL_MakeVectors( pev->angles ); + Vector vecProject = startpos + gpGlobals->v_forward * MAP_HALFSIZE; + UTIL_TraceLine( startpos, vecProject, dont_ignore_monsters, ignore_glass, NULL, &tr ); + FireAtPoint( startpos, tr ); + if(m_iState == STATE_ON) SetNextThink( 0.01 ); //!!! +} + +//========================================================= +// env_lightning - random zap strike +//========================================================= +class CEnvLightning : public CBeam +{ +public: + void Spawn( void ); + void Precache( void ); + void KeyValue( KeyValueData *pkvd ); + void Think( void ); + void RandomPoint( Vector &vecSrc ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void BeamDamage( TraceResult *ptr ); +}; +LINK_ENTITY_TO_CLASS( env_lightning, CEnvLightning ); + +void CEnvLightning::Precache( void ) +{ + pev->team = UTIL_PrecacheModel( pev->message, "sprites/laserbeam.spr" ); + CBeam::Precache(); +} + +void CEnvLightning::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "lifetime"))//beam lifetime (leave blank for toggle beam) + { + pev->armorvalue = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "damage")) + { + pev->dmg = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "sprite")) + { + pev->message = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "width")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "noisewidth")) + { + pev->impulse = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "radius")) + { + pev->frags = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBeam::KeyValue( pkvd ); +} + +void CEnvLightning::BeamDamage( TraceResult *ptr ) +{ + RelinkBeam(); + if ( ptr->flFraction != 1.0 && ptr->pHit != NULL ) + { + CBaseEntity *pHit = CBaseEntity::Instance(ptr->pHit); + if ( pHit ) + { + if (pev->dmg > 0) + { + ClearMultiDamage(); + pHit->TraceAttack( pev, pev->dmg * (gpGlobals->time - pev->dmgtime), (ptr->vecEndPos - pev->origin).Normalize(), ptr, DMG_ENERGYBEAM ); + ApplyMultiDamage( pev, pev ); + if ( pev->dmg > 40 && pHit->IsBSPModel())//wall damage + { + UTIL_DecalTrace( ptr, DECAL_BIGSHOT1 + RANDOM_LONG(0,4) ); + } + } + else pHit->TakeHealth( -(pev->dmg * (gpGlobals->time - pev->dmgtime)), DMG_GENERIC ); + } + } + pev->dmgtime = gpGlobals->time; +} + +void CEnvLightning::RandomPoint( Vector &vecSrc ) +{ + int iLoops = 0; + + for (iLoops = 0; iLoops < 10; iLoops++) + { + Vector vecDir1 = Vector( RANDOM_FLOAT( -1.0, 1.0 ), RANDOM_FLOAT( -1.0, 1.0 ),RANDOM_FLOAT( -1.0, 1.0 ) ); + vecDir1 = vecDir1.Normalize(); + TraceResult tr1; + UTIL_TraceLine( vecSrc, vecSrc + vecDir1 * pev->frags, ignore_monsters, ENT(pev), &tr1 ); + + if ((tr1.vecEndPos - vecSrc).Length() < pev->frags * 0.1) continue; + if (tr1.flFraction == 1.0) continue; + + SFX_Zap ( pev, vecSrc, tr1.vecEndPos ); + DoSparks( vecSrc, tr1.vecEndPos ); + BeamDamage( &tr1 ); + UTIL_FireTargets( pev->target, this, this, USE_TOGGLE ); + break; + } +} + +void CEnvLightning::Spawn( void ) +{ + UTIL_SetModel(edict(), "sprites/null.spr");//beam start point + pev->solid = SOLID_NOT; // Remove model & collisions + Precache(); + + pev->dmgtime = gpGlobals->time; + if (pev->rendercolor == g_vecZero) pev->rendercolor = Vector(255, 255, 255); + if(pev->spawnflags & SF_START_ON) Use( this, this, USE_ON, 0 ); +} + +void CEnvLightning::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + m_iState = STATE_ON; + pev->effects &= ~EF_NODRAW; + SetNextThink( 0 ); + pev->dmgtime = gpGlobals->time; + } + else if (useType == USE_OFF) + { + m_iState = STATE_OFF; + pev->effects |= EF_NODRAW; + DontThink(); + } + else if(useType == USE_SET) + { + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, MaxDelay %.2f\n\n", GetStringForState( GetState()), m_flDelay ); + } +} + +void CEnvLightning::Think( void ) +{ + RandomPoint( pev->origin ); + SetNextThink( pev->armorvalue + RANDOM_FLOAT( 0, m_flDelay ) ); +} + +//======================================================================= +// env_beamring - make ring from beams +//======================================================================= +class CEnvBeamRing : public CBeam +{ +public: + void Spawn( void ); + void Think( void ); + void Precache( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( env_beamring, CEnvBeamRing ); + +void CEnvBeamRing::Precache( void ) +{ + pev->team = UTIL_PrecacheModel( pev->message, "sprites/laserbeam.spr" ); + CBeam::Precache(); +} + +void CEnvBeamRing::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "lifetime"))//beam lifetime (leave blank for toggle beam) + { + pev->armorvalue = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "sprite")) + { + pev->message = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "width")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "noisewidth")) + { + pev->impulse = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "radius")) + { + pev->frags = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBeam::KeyValue( pkvd ); +} + +void CEnvBeamRing::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + m_iState = STATE_ON; + pev->effects &= ~EF_NODRAW; + SetNextThink( 0 ); + pev->dmgtime = gpGlobals->time; + } + else if (useType == USE_OFF) + { + m_iState = STATE_OFF; + pev->effects |= EF_NODRAW; + DontThink(); + } + else if(useType == USE_SET) + { + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, MaxDelay %.2f\n\n", GetStringForState( GetState()), m_flDelay ); + } +} + +void CEnvBeamRing::Spawn( void ) +{ + UTIL_SetModel(edict(), "sprites/null.spr");//beam start point + pev->solid = SOLID_NOT; // Remove model & collisions + Precache(); + + pev->dmgtime = gpGlobals->time; + if (pev->rendercolor == g_vecZero) pev->rendercolor = Vector(255, 255, 255); + if(pev->frags == 0)pev->frags = 20; + + //create second point + Vector vecAngles, vecPos; + vecAngles = pev->angles; + UTIL_MakeVectors(vecAngles); + vecPos = pev->origin + (gpGlobals->v_forward * pev->frags); + CBaseEntity *pRing = CBaseEntity::Create( "info_target", vecPos, vecAngles, edict() ); + if(m_iParent)pRing->m_iParent = m_iParent; + pev->enemy = pRing->edict(); //save our pointer + + if(pev->spawnflags & SF_START_ON) Use( this, this, USE_ON, 0 ); +} + +void CEnvBeamRing::Think( void ) +{ + CBaseEntity *pRing = CBaseEntity::Instance(pev->enemy); + SFX_Ring( pev, pRing->pev ); + SetNextThink( pev->armorvalue + RANDOM_FLOAT( 0, m_flDelay ) ); +} + +//===================================================== +// env_render: change render parameters +//===================================================== + +#define SF_RENDER_MASKFX (1<<0) +#define SF_RENDER_MASKAMT (1<<1) +#define SF_RENDER_MASKMODE (1<<2) +#define SF_RENDER_MASKCOLOR (1<<3) + +class CRenderFxManager : public CBaseLogic +{ +public: + void Affect( void ); + void KeyValue( KeyValueData *pkvd ); + void Use ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + m_hActivator = pActivator; Affect(); + } +}; +LINK_ENTITY_TO_CLASS( env_render, CRenderFxManager ); + +void CRenderFxManager :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "fadetime")) + { + pev->speed = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CRenderFxManager::Affect( void ) +{ + BOOL first = TRUE; + + if(FStringNull(pev->target)) return; + CBaseEntity* pTarget = UTIL_FindEntityByTargetname( NULL, STRING(pev->target), m_hActivator); + + while ( pTarget != NULL ) + { + entvars_t *pevTarget = pTarget->pev; + + if (pev->speed == 0) // aplly settings instantly ? + { + if ( !FBitSet( pev->spawnflags, SF_RENDER_MASKAMT ) ) + pevTarget->renderamt = pev->renderamt; + if ( !FBitSet( pev->spawnflags, SF_RENDER_MASKCOLOR ) ) + pevTarget->rendercolor = pev->rendercolor; + if ( !FBitSet( pev->spawnflags, SF_RENDER_MASKFX ) ) + pevTarget->renderfx = pev->renderfx; + if ( !FBitSet( pev->spawnflags, SF_RENDER_MASKMODE ) ) + pevTarget->rendermode = pev->rendermode; + + pevTarget->scale = pev->scale; + pevTarget->framerate = pev->framerate; + if (first) UTIL_FireTargets( pev->netname, pTarget, this, USE_TOGGLE ); + } + else + { + if (!pevTarget->renderamt && !pevTarget->rendermode) pevTarget->renderamt = 255; + pevTarget->renderfx = pev->renderfx; + pevTarget->rendermode = pev->rendermode; + CBaseEntity *pFader = Create( "faderent", pev->origin, pev->angles, pTarget->edict()); + pFader->pev->renderamt = pevTarget->renderamt; + pFader->pev->rendercolor = pevTarget->rendercolor; + pFader->pev->scale = pevTarget->scale; + if (pFader->pev->scale == 0) pFader->pev->scale = 1; + pFader->pev->spawnflags = pev->spawnflags; + + if (first) pFader->pev->target = pev->netname; + + pFader->pev->max_health = pev->framerate - pevTarget->framerate; + pFader->pev->health = pev->renderamt - pevTarget->renderamt; + pFader->pev->movedir.x = pev->rendercolor.x - pevTarget->rendercolor.x; + pFader->pev->movedir.y = pev->rendercolor.y - pevTarget->rendercolor.y; + pFader->pev->movedir.z = pev->rendercolor.z - pevTarget->rendercolor.z; + + if ( pev->scale ) pFader->pev->frags = pev->scale - pevTarget->scale; + else pFader->pev->frags = 0; + + pFader->pev->dmgtime = gpGlobals->time; + pFader->pev->speed = pev->speed; + pFader->SetNextThink( 0 ); + pFader->Spawn(); + } + + first = FALSE;//not a first target + pTarget = UTIL_FindEntityByTargetname( pTarget, STRING(pev->target), m_hActivator ); + } +} \ No newline at end of file diff --git a/server/ents/baseinfo.cpp b/server/ents/baseinfo.cpp new file mode 100644 index 00000000..dbd16e57 --- /dev/null +++ b/server/ents/baseinfo.cpp @@ -0,0 +1,229 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// baseinfo.cpp - point info entities. +// e.g. info_target +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" + +//======================================================================= +// info_target (target entity) +//======================================================================= + +class CInfoTarget : public CPointEntity +{ +public: + void Spawn( void ) + { + pev->solid = SOLID_NOT; + UTIL_SetModel(ENT(pev),"models/common/null.mdl"); + UTIL_SetSize(pev, g_vecZero, g_vecZero); + SetBits( pev->flags, FL_POINTENTITY ); + } +}; + +void CBaseDMStart::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "master")) + { + pev->netname = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CPointEntity::KeyValue( pkvd ); +} + +STATE CBaseDMStart::GetState( CBaseEntity *pEntity ) +{ + if (UTIL_IsMasterTriggered( pev->netname, pEntity )) + return STATE_ON; + else return STATE_OFF; +} + +//========================================================= +// static infodecal +//========================================================= +class CDecal : public CBaseEntity +{ +public: + void KeyValue( KeyValueData *pkvd ) + { + if (FStrEq(pkvd->szKeyName, "texture")) + { + pev->skin = DECAL_INDEX( pkvd->szValue ); + if ( pev->skin >= 0 ) return; + Msg( "Can't find decal %s\n", pkvd->szValue ); + } + } + void PostSpawn( void ) { if(FStringNull(pev->targetname))MakeDecal(); } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) { MakeDecal(); } + void MakeDecal( void ) + { + if ( pev->skin < 0 ) { REMOVE_ENTITY(ENT(pev)); return; } + TraceResult trace; + int entityIndex, modelIndex; + UTIL_TraceLine( pev->origin - Vector(5,5,5), pev->origin + Vector(5,5,5), ignore_monsters, ENT(pev), &trace ); + entityIndex = (short)ENTINDEX(trace.pHit); + if ( entityIndex ) modelIndex = (int)VARS(trace.pHit)->modelindex; + else modelIndex = 0; + + if(FStringNull(pev->targetname)) g_engfuncs.pfnStaticDecal( pev->origin, (int)pev->skin, entityIndex, modelIndex ); + else + { + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY); + WRITE_BYTE( TE_BSPDECAL ); + WRITE_COORD( pev->origin.x ); + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z ); + WRITE_SHORT( (int)pev->skin ); + WRITE_SHORT( entityIndex ); + if(entityIndex) WRITE_SHORT( modelIndex ); + MESSAGE_END(); + } + + SetThink( Remove ); + SetNextThink( 0.3 ); + } +}; + +//========================================================= +// Multiplayer intermission spots. +//========================================================= +class CInfoIntermission:public CPointEntity +{ + void Spawn( void ); + void Think( void ); + void PostActivate( void ); + CBaseEntity *pTarget; +}; + +void CInfoIntermission::Spawn( void ) +{ + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NOCLIP; + UTIL_SetOrigin( this, pev->origin ); + UTIL_SetModel( ENT( pev ), "sprites/null.spr" ); + + SetNextThink( 1 );// let targets spawn! +} + +void CInfoIntermission::PostActivate( void ) +{ + pTarget = UTIL_FindEntityByTargetname( NULL, STRING( pev->target )); + if( !pev->speed ) pev->speed = 100; +} + +void CInfoIntermission::Think ( void ) +{ + if( pTarget ) + { + UTIL_WatchTarget( this, pTarget ); + SetNextThink( 0 ); + } +} + +//==================================================================== +// multisource +//==================================================================== + +TYPEDESCRIPTION CMultiSource::m_SaveData[] = +{ + DEFINE_ARRAY( CMultiSource, m_rgEntities, FIELD_EHANDLE, MAX_MULTI_TARGETS ), + DEFINE_ARRAY( CMultiSource, m_rgTriggered, FIELD_INTEGER, MAX_MULTI_TARGETS ), + DEFINE_FIELD( CMultiSource, m_iTotal, FIELD_INTEGER ), +}; IMPLEMENT_SAVERESTORE( CMultiSource, CBaseLogic ); +LINK_ENTITY_TO_CLASS( multisource, CMultiSource ); + +void CMultiSource::Spawn() +{ + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + SetNextThink( 0.1 ); + pev->spawnflags |= SF_START_ON; + SetThink( Register ); +} + +void CMultiSource::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + int i = 0; + + // Find the entity in our list + while (i < m_iTotal) if ( m_rgEntities[i++] == pCaller ) break; + + // if we didn't find it, report error and leave + if (i > m_iTotal) return; + + STATE s = GetState(); + m_rgTriggered[i-1] ^= 1; + if ( s == GetState()) return; + + if ( s == STATE_OFF ) + { + USE_TYPE useType = USE_TOGGLE; + if ( m_globalstate ) useType = USE_ON; + UTIL_FireTargets( pev->target, NULL, this, useType, value ); + UTIL_FireTargets( m_iszKillTarget, NULL, this, USE_REMOVE ); + } +} + +STATE CMultiSource::GetState( void ) +{ + // Is everything triggered? + int i = 0; + + // Still initializing? + if ( pev->spawnflags & SF_START_ON ) return STATE_OFF; + + while (i < m_iTotal) + { + if (m_rgTriggered[i] == 0) break; + i++; + } + + if (i == m_iTotal) + { + if ( !m_globalstate || gGlobalState.EntityGetState( m_globalstate ) == GLOBAL_ON ) + return STATE_ON; + } + return STATE_OFF; +} + +void CMultiSource::Register(void) +{ + m_iTotal = 0; + memset( m_rgEntities, 0, MAX_MULTI_TARGETS * sizeof(EHANDLE) ); + + SetThink(NULL); + + // search for all entities which target this multisource (pev->targetname) + + CBaseEntity *pTarget = UTIL_FindEntityByTarget( NULL, STRING(pev->targetname) ); + while (pTarget && (m_iTotal < MAX_MULTI_TARGETS)) + { + m_rgEntities[m_iTotal++] = pTarget; + pTarget = UTIL_FindEntityByTarget( pTarget, STRING(pev->targetname)); + } + + pTarget = UTIL_FindEntityByClassname(NULL, "multi_manager"); + while (pTarget && (m_iTotal < MAX_MULTI_TARGETS)) + { + if ( pTarget->HasTarget(pev->targetname) ) m_rgEntities[m_iTotal++] = pTarget; + pTarget = UTIL_FindEntityByClassname( pTarget, "multi_manager" ); + } + pev->spawnflags &= ~SF_START_ON; +} + + +LINK_ENTITY_TO_CLASS( infodecal, CDecal ); +LINK_ENTITY_TO_CLASS( info_target, CInfoTarget ); +LINK_ENTITY_TO_CLASS( info_teleport_destination, CPointEntity ); +LINK_ENTITY_TO_CLASS( info_null, CNullEntity); +LINK_ENTITY_TO_CLASS( info_texlights, CNullEntity); +LINK_ENTITY_TO_CLASS( info_compile_parameters, CNullEntity); +LINK_ENTITY_TO_CLASS( info_intermission, CInfoIntermission ); +LINK_ENTITY_TO_CLASS( info_player_deathmatch, CBaseDMStart); +LINK_ENTITY_TO_CLASS( info_player_start, CPointEntity); +LINK_ENTITY_TO_CLASS( info_landmark, CPointEntity); \ No newline at end of file diff --git a/server/ents/baseinfo.h b/server/ents/baseinfo.h new file mode 100644 index 00000000..583baa47 --- /dev/null +++ b/server/ents/baseinfo.h @@ -0,0 +1,44 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#ifndef BASEINFO_H +#define BASEINFO_H + +class CPointEntity : public CBaseEntity +{ +public: + void Spawn( void ){ pev->solid = SOLID_NOT; } + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } +}; + +class CNullEntity : public CBaseEntity +{ +public: + void Spawn( void ){ REMOVE_ENTITY(ENT(pev)); } +}; + +class CBaseDMStart : public CPointEntity +{ +public: + void KeyValue( KeyValueData *pkvd ); + STATE GetState( CBaseEntity *pEntity ); + +private: +}; + +class CLaserSpot : public CBaseEntity//laser spot for different weapons +{ + void Spawn( void ); + void Precache( void ); + int ObjectCaps( void ) { return FCAP_DONT_SAVE; } +public: + void Suspend( float flSuspendTime ); + void Update( CBasePlayer *m_pPlayer ); + void EXPORT Revive( void ); + void Killed( void ){ UTIL_Remove( this ); } + + static CLaserSpot *CreateSpot( void ); +}; + +#endif //BASEINFO_H \ No newline at end of file diff --git a/server/ents/baseitem.cpp b/server/ents/baseitem.cpp new file mode 100644 index 00000000..f59c4e41 --- /dev/null +++ b/server/ents/baseitem.cpp @@ -0,0 +1,628 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// item_.cpp - items entities: suit, +// helmet, lighter, etc +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "saverestore.h" +#include "baseweapon.h" +#include "player.h" +#include "gamerules.h" +#include "defaults.h" + +extern int gEvilImpulse101; +extern int gmsgItemPickup; + +//*********************************************************** +// main functions () +//*********************************************************** + +void CItem::Spawn( void ) +{ + Precache(); + pev->movetype = MOVETYPE_TOSS; + pev->solid = SOLID_BBOX; + + UTIL_SetOrigin( this, pev->origin ); + UTIL_SetSize(pev, g_vecZero, g_vecZero ); + + SetTouch( ItemTouch ); + SetThink( ItemFall ); + + UTIL_SetModel(ENT(pev), pev->model, (char *)Model() ); + SetNextThink( 0.1 ); +} + +void CItem::Precache( void ) +{ + UTIL_PrecacheModel( pev->model, (char *)Model() ); + UTIL_PrecacheSound((char *)PickSound()); + UTIL_PrecacheSound((char *)FallSound()); +} + +void CItem::ItemTouch( CBaseEntity *pOther ) +{ + //removed this limitation for monsters + if ( !pOther->IsPlayer() ) return; + CBasePlayer *pPlayer = (CBasePlayer *)pOther; + if (!UTIL_IsMasterTriggered(m_sMaster, pPlayer)) return; + if (pPlayer->pev->deadflag != DEAD_NO) return; + + if (AddItem( pPlayer )) + { + UTIL_FireTargets( pev->target, pOther, this, USE_TOGGLE ); + SetTouch( NULL ); + + if (IsItem() && gmsgItemPickup) + { + //show icon for item + MESSAGE_BEGIN( MSG_ONE, gmsgItemPickup, NULL, pPlayer->pev ); + WRITE_STRING( STRING(pev->classname) ); + MESSAGE_END(); + } + + //play pickup sound + EMIT_SOUND( pPlayer->edict(), CHAN_ITEM, (char *)PickSound(), 1, ATTN_NORM ); + + // tell the owner item was taken + if (!FNullEnt( pev->owner )) pev->owner->v.impulse = 0; + + //enable respawn in multiplayer + if ( g_pGameRules->IsMultiplayer()) Respawn(); + else UTIL_Remove( this ); + } + else if (gEvilImpulse101) UTIL_Remove( this ); +} + +void CItem::ItemFall ( void ) +{ + SetNextThink( 0.1 ); + + if ( pev->flags & FL_ONGROUND ) + { + // clatter if we have an owner (i.e., dropped by someone) + // don't clatter if the item is waiting to respawn (if it's waiting, it is invisible!) + if ( !FNullEnt( pev->owner )) + { + EMIT_SOUND(ENT(pev), CHAN_ITEM, (char *)FallSound(), 1, ATTN_NORM); + ItemOnGround(); //do somewhat if needed + } + + // lie flat + pev->angles.x = 0; + pev->angles.z = 0; + + pev->solid = SOLID_TRIGGER; + + if (IsAmmo()) UTIL_SetSize(pev, Vector(-8, -8, -8), Vector(8, 8, 8)); + if (IsItem()) UTIL_SetSize ( pev, Vector ( -8, -8, 0 ), Vector ( 8, 8, 8 ) ); + else UTIL_AutoSetSize(); + + UTIL_SetOrigin( this, pev->origin );// link into world. + + SetTouch( ItemTouch ); + SetThink (NULL); + } +} + +CBaseEntity* CItem::Respawn( void ) +{ + SetTouch( NULL ); + pev->effects |= EF_NODRAW; + + SetThink( Materialize ); + AbsoluteNextThink( ItemRespawnTime( this ) ); + return this; +} + +float CItem::ItemRespawnTime( CItem *pItem ) +{ + //makes different time to respawn for weapons and items + float flRespawnTime; + + if (IsAmmo()) flRespawnTime = RESPAWN_TIME_30SEC; + if (IsItem()) flRespawnTime = RESPAWN_TIME_120SEC; + return gpGlobals->time + flRespawnTime; +} + + +void CItem::Materialize( void ) +{ + if ( pev->effects & EF_NODRAW ) + { + // changing from invisible state to visible. + pev->effects &= ~EF_NODRAW; + pev->renderfx = kRenderFxGlowShell; + pev->renderamt = 40; + pev->frags = 0; + pev->rendercolor.x = RANDOM_LONG(25, 255); + pev->rendercolor.y = RANDOM_LONG(25, 255); + pev->rendercolor.z = RANDOM_LONG(25, 255); + pev->scale = 0.01; + SetNextThink (0.001); + } + if( pev->scale > 1.2 ) pev->frags = 1; + if ( pev->frags == 1 ) + { //set effects for respawn item + if(pev->scale > 1.0) pev->scale -= 0.05; + else + { + pev->renderfx = kRenderFxNone; + pev->frags = 0; + SetTouch( ItemTouch ); + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, "items/respawn.wav", 1, ATTN_NORM, 0, 150 ); + SetThink( NULL ); + DontThink(); + } + } + else pev->scale += 0.05; + SetNextThink (0.001); + +} + +//*********************************************************** +// generic item +//*********************************************************** +class CGenericItem : public CItem +{ + const char *Model( void ){ return "models/w_adrenaline.mdl"; } + BOOL AddItem( CBaseEntity *pOther ) + { + CBasePlayer *pPlayer = (CBasePlayer *)pOther; + if (pPlayer->pev->deadflag != DEAD_NO) return FALSE; + if (pOther->GiveAmmo( AMMO_GLOCKCLIP_GIVE, "9mm", _9MM_MAX_CARRY ) != -1) + { + EMIT_SOUND(ENT(pev), CHAN_ITEM, "weapons/glock/clip_in.wav", 1, ATTN_NORM); + return TRUE; + } + MESSAGE_BEGIN( MSG_ONE, gmsgItemPickup, NULL, pPlayer->pev ); + WRITE_STRING( STRING(pev->classname) ); + MESSAGE_END(); + return FALSE; + } +}; +LINK_ENTITY_TO_CLASS( item_generic, CGenericItem ); + +//*********************************************************** +// items +//*********************************************************** +class CItemSuit : public CItem +{ + const char *Model( void ){ return "models/w_suit.mdl"; } + BOOL AddItem( CBaseEntity *pOther ) + { + CBasePlayer *pPlayer = (CBasePlayer *)pOther; + if ( pPlayer->pev->deadflag != DEAD_NO ) return FALSE; + + if ( pPlayer->m_iHideHUD & ITEM_SUIT ) + return FALSE; + + if ( pev->spawnflags & 1 )//SF_SUIT_SHORTLOGON + EMIT_SOUND_SUIT(pPlayer->edict(), "!HEV_A0"); // short version of suit logon, + else EMIT_SOUND_SUIT(pPlayer->edict(), "!HEV_AAx"); // long version of suit logon + + pPlayer->m_iHideHUD |= ITEM_SUIT; + return TRUE; + } +}; +LINK_ENTITY_TO_CLASS(item_suit, CItemSuit); + +class CItemLongJump : public CItem +{ + const char *Model( void ){ return "models/w_longjump.mdl"; } + const char *PickSound( void ){ return "buttons/bell1.wav"; } + BOOL AddItem( CBaseEntity *pOther ) + { + CBasePlayer *pPlayer = (CBasePlayer *)pOther; + if ( pPlayer->m_iHideHUD & ITEM_SUIT && !pPlayer->m_fLongJump) + { + pPlayer->m_fLongJump = TRUE;// player now has longjump module + EMIT_SOUND_SUIT( pPlayer->edict(), "!HEV_A1" ); + return TRUE; + } + return FALSE; + } +}; +LINK_ENTITY_TO_CLASS( item_longjump, CItemLongJump ); + + +class CItemBattery : public CItem +{ + const char *Model( void ){ return "models/w_battery.mdl"; } + const char *PickSound( void ){ return "items/gunpickup2.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->TakeArmor( BATTERY_CHARGE, TRUE ); } +}; +LINK_ENTITY_TO_CLASS(item_battery, CItemBattery); + +class CHealthKit : public CItem +{ + const char *Model( void ){ return "models/items/w_medkit.mdl"; } + const char *PickSound( void ){ return "items/smallmedkit1.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->TakeHealth( MEDKIT_CAP, DMG_GENERIC ); } +}; +LINK_ENTITY_TO_CLASS( item_healthkit, CHealthKit ); + +class CItemSoda : public CItem +{ +public: + const char *Model( void ){ return "models/can.mdl"; } + const char *FallSound( void ){ return "weapons/g_bounce3.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { pOther->TakeHealth( pev->skin * 1, DMG_GENERIC ); return 1; } +}; +LINK_ENTITY_TO_CLASS( item_sodacan, CItemSoda ); + +class CItemSecurity : public CItem +{ + const char *Model( void ){ return "models/w_security.mdl"; } + const char *PickSound( void ){ return "items/gunpickup2.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return TRUE; } +}; +LINK_ENTITY_TO_CLASS(item_security, CItemSecurity); + +class CItemArmorVest : public CItem +{ + const char *Model( void ){ return "models/w_vest.mdl"; } + const char *PickSound( void ){ return "items/gunpickup2.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->TakeArmor( 60 ); } +}; +LINK_ENTITY_TO_CLASS(item_armorvest, CItemArmorVest); + +class CItemHelmet : public CItem +{ + const char *Model( void ){ return "models/w_helmet.mdl"; } + const char *PickSound( void ){ return "items/gunpickup2.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->TakeArmor( 40 ); } +}; +LINK_ENTITY_TO_CLASS(item_helmet, CItemHelmet); + +//*********************************************************** +// ammo +//*********************************************************** +class CGlockAmmo : public CItem +{ + const char *Model( void ){ return "models/w_9mmclip.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_GLOCKCLIP_GIVE, "9mm", 250 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_9mmclip, CGlockAmmo ); +LINK_ENTITY_TO_CLASS( ammo_glockclip, CGlockAmmo ); + +class CPythonAmmo : public CItem +{ + const char *Model( void ){ return "models/w_357ammobox.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_357BOX_GIVE, "357", 21 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_357, CPythonAmmo ); + +class CSniperAmmo : public CItem +{ + const char *Model( void ){ return "models/w_m40a1clip.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_357BOX_GIVE, "762", 21 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_762, CSniperAmmo ); + +class CRpgAmmo : public CItem +{ + const char *Model( void ){ return "models/w_rpgammo.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_RPGCLIP_GIVE, "rockets", 3 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_rpgclip, CRpgAmmo ); + +class CMP5AmmoClip : public CItem +{ + const char *Model( void ){ return "models/w_9mmARclip.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_MP5CLIP_GIVE, "9mm", 250); } +}; +LINK_ENTITY_TO_CLASS( ammo_9mmAR, CMP5AmmoClip ); +LINK_ENTITY_TO_CLASS( ammo_mp5clip, CMP5AmmoClip ); + +class CSawAmmo : public CItem +{ + const char *Model( void ){ return "models/w_saw_clip.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_CHAINBOX_GIVE, "556", SAW_MAX_CLIP); } +}; +LINK_ENTITY_TO_CLASS( ammo_556, CSawAmmo ); + +class CMP5AmmoGrenade : public CItem +{ + const char *Model( void ){ return "models/w_ARgrenade.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_M203BOX_GIVE, "m203", 10 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_m203, CMP5AmmoGrenade ); + +class CGaussAmmo : public CItem +{ + const char *Model( void ){ return "models/w_gaussammo.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( AMMO_URANIUMBOX_GIVE, "uranium", 100 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_gaussclip, CGaussAmmo ); + +class CShotgunAmmoBox : public CItem +{ + const char *Model( void ){ return "models/w_shotbox.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( 12, "buckshot", 125 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_buckshot, CShotgunAmmoBox ); + +class CCrossbowAmmo : public CItem +{ + const char *Model( void ){ return "models/w_crossbow_clip.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( 5, "bolts", 50 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_crossbow, CCrossbowAmmo ); + +class CShotgunAmmoShell : public CItem +{ + const char *Model( void ){ return "models/shellBuck.mdl"; } + const char *PickSound( void ){ return "weapons/glock/clip_in.wav"; } + BOOL AddItem( CBaseEntity *pOther ) { return pOther->GiveAmmo( 1, "buckshot", 125 ); } +}; +LINK_ENTITY_TO_CLASS( ammo_buckshell, CShotgunAmmoShell ); + +//*********************************************************** +// item_weaponbox - a single entity that can store weapons +// and ammo. +//*********************************************************** +LINK_ENTITY_TO_CLASS( item_weaponbox, CWeaponBox ); + +TYPEDESCRIPTION CWeaponBox::m_SaveData[] = +{ + DEFINE_ARRAY( CWeaponBox, m_rgAmmo, FIELD_INTEGER, MAX_AMMO_SLOTS ), + DEFINE_ARRAY( CWeaponBox, m_rgiszAmmo, FIELD_STRING, MAX_AMMO_SLOTS ), + DEFINE_ARRAY( CWeaponBox, m_rgpPlayerItems, FIELD_CLASSPTR, MAX_ITEM_TYPES ), + DEFINE_FIELD( CWeaponBox, m_cAmmoTypes, FIELD_INTEGER ), +}; IMPLEMENT_SAVERESTORE( CWeaponBox, CBaseEntity ); + +void CWeaponBox::Precache( void ) +{ + UTIL_PrecacheModel("models/w_weaponbox.mdl"); +} + +void CWeaponBox :: KeyValue( KeyValueData *pkvd ) +{ + if ( m_cAmmoTypes < MAX_AMMO_SLOTS ) + { + PackAmmo( ALLOC_STRING(pkvd->szKeyName), atoi(pkvd->szValue) ); + m_cAmmoTypes++;// count this new ammo type. + + pkvd->fHandled = TRUE; + } + else Msg( "WeaponBox too full! only %d ammotypes allowed\n", MAX_AMMO_SLOTS ); +} + +void CWeaponBox::Spawn( void ) +{ + Precache( ); + + pev->movetype = MOVETYPE_TOSS; + pev->solid = SOLID_TRIGGER; + + UTIL_SetSize( pev, g_vecZero, g_vecZero ); + UTIL_SetModel( ENT(pev), "models/w_weaponbox.mdl"); +} + +void CWeaponBox::Kill( void ) +{ + CBasePlayerWeapon *pWeapon; + int i; + + // destroy the weapons + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + pWeapon = m_rgpPlayerItems[ i ]; + + while ( pWeapon ) + { + pWeapon->SetThink( Remove ); + pWeapon->SetNextThink( 0.1 ); + pWeapon = pWeapon->m_pNext; + } + } + + // remove the box + UTIL_Remove( this ); +} + +void CWeaponBox::Touch( CBaseEntity *pOther ) +{ + if ( !(pev->flags & FL_ONGROUND ) ) return; + if ( !pOther->IsPlayer() ) return; + if ( !pOther->IsAlive() ) return; + + CBasePlayer *pPlayer = (CBasePlayer *)pOther; + int i; + + // dole out ammo + for ( i = 0 ; i < MAX_AMMO_SLOTS ; i++ ) + { + if ( !FStringNull( m_rgiszAmmo[ i ] ) ) + { + // there's some ammo of this type. + pPlayer->GiveAmmo( m_rgAmmo[ i ], (char *)STRING( m_rgiszAmmo[ i ] ), MaxAmmoCarry( m_rgiszAmmo[ i ] ) ); + + // now empty the ammo from the weaponbox since we just gave it to the player + m_rgiszAmmo[ i ] = iStringNull; + m_rgAmmo[ i ] = 0; + } + } + + // go through my weapons and try to give the usable ones to the player. + // it's important the the player be given ammo first, so the weapons code doesn't refuse + // to deploy a better weapon that the player may pick up because he has no ammo for it. + + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + if ( m_rgpPlayerItems[ i ] ) + { + CBasePlayerWeapon *pItem; + + // have at least one weapon in this slot + while ( m_rgpPlayerItems[ i ] ) + { + //ALERT ( at_console, "trying to give %s\n", STRING( m_rgpPlayerItems[ i ]->pev->classname ) ); + + pItem = m_rgpPlayerItems[ i ]; + m_rgpPlayerItems[ i ] = m_rgpPlayerItems[ i ]->m_pNext;// unlink this weapon from the box + + if ( pPlayer->AddPlayerItem( pItem ) ) pItem->AttachToPlayer( pPlayer ); + } + } + } + + EMIT_SOUND( pOther->edict(), CHAN_ITEM, "items/gunpickup2.wav", 1, ATTN_NORM ); + SetTouch(NULL); + UTIL_Remove(this); +} + +int CWeaponBox::MaxAmmoCarry( int iszName ) +{ + for ( int i = 0; i < MAX_WEAPONS; i++ ) + { + if ( CBasePlayerWeapon::ItemInfoArray[i].pszAmmo1 && !strcmp( STRING(iszName), CBasePlayerWeapon::ItemInfoArray[i].pszAmmo1 ) ) + return CBasePlayerWeapon::ItemInfoArray[i].iMaxAmmo1; + if ( CBasePlayerWeapon::ItemInfoArray[i].pszAmmo2 && !strcmp( STRING(iszName), CBasePlayerWeapon::ItemInfoArray[i].pszAmmo2 ) ) + return CBasePlayerWeapon::ItemInfoArray[i].iMaxAmmo2; + } + + Msg( "MaxAmmoCarry() doesn't recognize '%s'!\n", STRING( iszName ) ); + return -1; +} + +BOOL CWeaponBox::PackWeapon( CBasePlayerWeapon *pWeapon ) +{ + // is one of these weapons already packed in this box? + if ( HasWeapon( pWeapon ) ) return FALSE;// box can only hold one of each weapon type + + if ( pWeapon->m_pPlayer ) + { + // failed to unhook the weapon from the player! + if ( !pWeapon->m_pPlayer->RemovePlayerItem( pWeapon ) ) return FALSE; + } + + int iWeaponSlot = pWeapon->iItemSlot(); + + if ( m_rgpPlayerItems[ iWeaponSlot ] ) + { + // there's already one weapon in this slot, so link this into the slot's column + pWeapon->m_pNext = m_rgpPlayerItems[ iWeaponSlot ]; + m_rgpPlayerItems[ iWeaponSlot ] = pWeapon; + } + else + { + // first weapon we have for this slot + m_rgpPlayerItems[ iWeaponSlot ] = pWeapon; + pWeapon->m_pNext = NULL; + } + + pWeapon->pev->spawnflags |= SF_NORESPAWN;// never respawn + pWeapon->pev->movetype = MOVETYPE_NONE; + pWeapon->pev->solid = SOLID_NOT; + pWeapon->pev->effects = EF_NODRAW; + pWeapon->pev->modelindex = 0; + pWeapon->pev->model = iStringNull; + pWeapon->pev->owner = edict(); + pWeapon->SetThink( NULL );// crowbar may be trying to swing again, etc. + pWeapon->SetTouch( NULL ); + pWeapon->m_pPlayer = NULL; + + return TRUE; +} + +BOOL CWeaponBox::PackAmmo( int iszName, int iCount ) +{ + int iMaxCarry; + + if ( FStringNull( iszName ) ) + { + // error here + Msg( "NULL String in PackAmmo!\n" ); + return FALSE; + } + + iMaxCarry = MaxAmmoCarry( iszName ); + + if ( iMaxCarry != -1 && iCount > 0 ) + { + GiveAmmo( iCount, (char *)STRING( iszName ), iMaxCarry ); + return TRUE; + } + return FALSE; +} + +int CWeaponBox::GiveAmmo( int iCount, char *szName, int iMax, int *pIndex/* = NULL*/ ) +{ + int i; + + for (i = 1; i < MAX_AMMO_SLOTS && !FStringNull( m_rgiszAmmo[i] ); i++) + { + if (stricmp( szName, STRING( m_rgiszAmmo[i])) == 0) + { + if (pIndex) *pIndex = i; + + int iAdd = min( iCount, iMax - m_rgAmmo[i]); + if (iCount == 0 || iAdd > 0) + { + m_rgAmmo[i] += iAdd; + return i; + } + return -1; + } + } + if (i < MAX_AMMO_SLOTS) + { + if (pIndex) *pIndex = i; + + m_rgiszAmmo[i] = MAKE_STRING( szName ); + m_rgAmmo[i] = iCount; + return i; + } + + Msg("out of named ammo slots\n"); + return i; +} + +BOOL CWeaponBox::HasWeapon( CBasePlayerWeapon *pCheckItem ) +{ + CBasePlayerWeapon *pItem = m_rgpPlayerItems[pCheckItem->iItemSlot()]; + + while (pItem) + { + if (FClassnameIs( pItem->pev, STRING( pCheckItem->pev->classname) )) return TRUE; + pItem = pItem->m_pNext; + } + return FALSE; +} + +BOOL CWeaponBox::IsEmpty( void ) +{ + int i; + + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + if ( m_rgpPlayerItems[ i ] ) return FALSE; + } + + for ( i = 0 ; i < MAX_AMMO_SLOTS ; i++ ) + { + if ( !FStringNull( m_rgiszAmmo[ i ] )) return FALSE; // still have a bit of this type of ammo + } + return TRUE; +} + +void CWeaponBox::SetObjectCollisionBox( void ) +{ + pev->absmin = pev->origin + Vector(-16, -16, 0); + pev->absmax = pev->origin + Vector(16, 16, 16); +} \ No newline at end of file diff --git a/server/ents/baseitem.h b/server/ents/baseitem.h new file mode 100644 index 00000000..835c7781 --- /dev/null +++ b/server/ents/baseitem.h @@ -0,0 +1,68 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef ITEMS_H +#define ITEMS_H + + +class CItem : public CBaseLogic +{ +public: + void Spawn( void ); + void Precache( void ); + CBaseEntity* Respawn( void ); + void EXPORT ItemTouch( CBaseEntity *pOther ); + void EXPORT Materialize( void ); + void EXPORT ItemFall( void ); + virtual BOOL AddItem( CBaseEntity *pOther ) { return TRUE; }; + virtual void ItemOnGround( void ) {}; + float ItemRespawnTime( CItem *pItem ); + + BOOL IsItem( void ) { return !strncmp( STRING(pev->classname), "item_", 5 ); } + BOOL IsAmmo( void ) { return !strncmp( STRING(pev->classname), "ammo_", 5 ); } + + //item_generic + virtual const char *Model( void ){ return NULL; } + virtual const char *PickSound( void ){ return "common/null.wav"; } + virtual const char *FallSound( void ){ return "common/null.wav"; } +}; + +class CWeaponBox : public CBaseEntity +{ + void Precache( void ); + void Spawn( void ); + void Touch( CBaseEntity *pOther ); + void KeyValue( KeyValueData *pkvd ); + BOOL IsEmpty( void ); + int GiveAmmo( int iCount, char *szName, int iMax, int *pIndex = NULL ); + void SetObjectCollisionBox( void ); +public: + void EXPORT Kill ( void ); + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + BOOL HasWeapon( CBasePlayerWeapon *pCheckItem ); + BOOL PackWeapon( CBasePlayerWeapon *pWeapon ); + BOOL PackAmmo( int iszName, int iCount ); + + CBasePlayerWeapon *m_rgpPlayerItems[MAX_ITEM_TYPES];// one slot for each + + int MaxAmmoCarry( int iszName ); + int m_rgiszAmmo[MAX_AMMO_SLOTS];// ammo names + int m_rgAmmo[MAX_AMMO_SLOTS];// ammo quantities + int m_cAmmoTypes;// how many ammo types packed into this box (if packed by a level designer) +}; + +#endif // ITEMS_H diff --git a/server/ents/baselogic.cpp b/server/ents/baselogic.cpp new file mode 100644 index 00000000..56716908 --- /dev/null +++ b/server/ents/baselogic.cpp @@ -0,0 +1,533 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// logicentity.cpp - all entities with prefix "logic_" +// additional entities for smart system +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "defaults.h" + +// Use CBaseDelay as main function (renamed as CBaseLogic, see declaration in cbase.h). +//======================================================================= +// main functions () +//======================================================================= + +void CBaseLogic :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "delay") || FStrEq(pkvd->szKeyName, "MaxDelay")) + { + m_flDelay = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "wait") || FStrEq(pkvd->szKeyName, "MaxWait")) + { + m_flWait = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "master")) + { + m_sMaster = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "killtarget")) + { + m_iszKillTarget = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "globalstate")) + { + m_globalstate = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CBaseLogic :: FireTargets( USE_TYPE useType, float value ) +{ + //fire our targets + UTIL_FireTargets( pev->target, m_hActivator, this, useType, value ); + UTIL_FireTargets( m_iszKillTarget, m_hActivator, this, USE_REMOVE ); +} + +BOOL CBaseLogic :: IsLockedByMaster( void ) +{ + if (UTIL_IsMasterTriggered(m_sMaster, m_hActivator)) + return FALSE; + return TRUE; +} + +BOOL CBaseLogic :: IsLockedByMaster( CBaseEntity *pActivator ) +{ + if (UTIL_IsMasterTriggered(m_sMaster, pActivator)) + return FALSE; + return TRUE; +} + +BOOL CBaseLogic :: IsLockedByMaster( USE_TYPE useType ) +{ + if (UTIL_IsMasterTriggered(m_sMaster, m_hActivator)) + return FALSE; + else if (useType == USE_SHOWINFO) return FALSE;//pass to debug info + return TRUE; +} + +TYPEDESCRIPTION CBaseLogic::m_SaveData[] = +{ + DEFINE_FIELD( CBaseLogic, m_flDelay, FIELD_FLOAT ), + DEFINE_FIELD( CBaseLogic, m_hActivator, FIELD_EHANDLE ), + DEFINE_FIELD( CBaseLogic, m_hTarget, FIELD_EHANDLE ), + DEFINE_FIELD( CBaseLogic, m_iState, FIELD_INTEGER ), + DEFINE_FIELD( CBaseLogic, m_sMaster, FIELD_STRING), + DEFINE_FIELD( CBaseLogic, m_iszKillTarget, FIELD_STRING ), + DEFINE_FIELD( CBaseLogic, m_sSet, FIELD_STRING), + DEFINE_FIELD( CBaseLogic, m_sReset, FIELD_STRING), + DEFINE_FIELD( CBaseLogic, m_flWait, FIELD_FLOAT ), + DEFINE_FIELD( CBaseLogic, m_globalstate, FIELD_STRING ), +};IMPLEMENT_SAVERESTORE( CBaseLogic, CBaseEntity ); + +//======================================================================= +// Logic_generator +//======================================================================= +#define NORMAL_MODE 0 +#define RANDOM_MODE 1 +#define RANDOM_MODE_WITH_DEC 2 + +class CGenerator : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Spawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Think (void); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + +private: + int m_iFireCount; + int m_iMaxFireCount; +}; +LINK_ENTITY_TO_CLASS( logic_generator, CGenerator ); + +void CGenerator :: Spawn() +{ + if(pev->button == RANDOM_MODE || pev->button == RANDOM_MODE_WITH_DEC) + { //set delay with decceleration + if (!m_flDelay || pev->button == RANDOM_MODE_WITH_DEC) m_flDelay = 0.005; + //generate max count automaticallly, if not set on map + if (!m_iMaxFireCount) m_iMaxFireCount = RANDOM_LONG(100, 200); + } + else + { + //Smart Field System © + if (!m_iMaxFireCount) m_iMaxFireCount = -1;//disable counting for normal mode + } + if(pev->spawnflags & SF_START_ON) + { + m_iState = STATE_ON;//initialy off in random mode + SetNextThink(m_flDelay); + } +} + +TYPEDESCRIPTION CGenerator :: m_SaveData[] = +{ + DEFINE_FIELD( CGenerator, m_iMaxFireCount, FIELD_INTEGER ), + DEFINE_FIELD( CGenerator, m_iFireCount, FIELD_INTEGER ), +}; +IMPLEMENT_SAVERESTORE( CGenerator, CBaseLogic ); + +void CGenerator :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "maxcount")) + { + m_iMaxFireCount = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "mode")) + { + pev->button = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CGenerator :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + if(pev->button == RANDOM_MODE || pev->button == RANDOM_MODE_WITH_DEC) + { + if(pev->button == RANDOM_MODE_WITH_DEC) m_flDelay = 0.005; + m_iFireCount = RANDOM_LONG(0, m_iMaxFireCount/2); + } + m_iState = STATE_ON; + SetNextThink( 0 );//immediately start firing targets + } + else if (useType == USE_OFF) + { + m_iState = STATE_OFF; + DontThink(); + } + else if (useType == USE_SET) //set max count of impulses + m_iMaxFireCount = value; + + else if (useType == USE_RESET) //immediately reset + m_iFireCount = 0; + + else if (useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, Delay time %f\n", GetStringForState( GetState()), m_flDelay); + ALERT(at_console, "FireCount: %d, Max FireCount: %d\n", m_iFireCount, m_iMaxFireCount); + } +} + +void CGenerator :: Think( void ) +{ + if(m_iFireCount != -1)//if counter enabled + { + if(m_iFireCount == m_iMaxFireCount) + { + m_iFireCount = NULL; + DontThink(); + m_iState = STATE_OFF; + return; + } + else m_iFireCount++; + } + if(pev->button == RANDOM_MODE_WITH_DEC) + { //deceleration for random mode + if(m_iMaxFireCount - m_iFireCount < 40) m_flDelay += 0.005; + } + UTIL_FireTargets( pev->target, this, this, USE_TOGGLE ); + SetNextThink(m_flDelay); +} + +//======================================================================= +// Logic_switcher +//======================================================================= +#define MODE_INCREMENT 0 +#define MODE_DECREMENT 1 +#define MODE_RANDOM_VALUE 2 + +class CSwitcher : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Spawn ( void ); + void Think ( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + int m_cTargets;// the total number of targets in this manager's fire list. + int m_index; // Current target + int m_iTargetName [ MAX_MULTI_TARGETS ];// list if indexes into global string array +}; +LINK_ENTITY_TO_CLASS( logic_switcher, CSwitcher ); + +// Global Savedata for switcher +TYPEDESCRIPTION CSwitcher::m_SaveData[] = +{ DEFINE_FIELD( CSwitcher, m_cTargets, FIELD_INTEGER ), + DEFINE_FIELD( CSwitcher, m_index, FIELD_INTEGER ), + DEFINE_ARRAY( CSwitcher, m_iTargetName, FIELD_STRING, MAX_MULTI_TARGETS ), +};IMPLEMENT_SAVERESTORE(CSwitcher, CBaseLogic); + +void CSwitcher :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "mode")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if ( m_cTargets < MAX_MULTI_TARGETS ) + { + // add this field to the target list + // this assumes that additional fields are targetnames and their values are delay values. + + char tmp[128]; + UTIL_StripToken( pkvd->szKeyName, tmp ); + m_iTargetName [ m_cTargets ] = ALLOC_STRING( tmp ); + m_cTargets++; + pkvd->fHandled = TRUE; + } +} + +void CSwitcher :: Spawn( void ) +{ + int r_index = 0; + int w_index = m_cTargets - 1; + + while(r_index < w_index) + { + //we store target with right index in tempname + int name = m_iTargetName [r_index]; + + //target with right name is free, record new value from wrong name + m_iTargetName [r_index] = m_iTargetName [w_index]; + + //ok, we can swap targets + m_iTargetName [w_index] = name; + r_index++; + w_index--; + } + + m_iState = STATE_OFF; + m_index = 0; + if(pev->spawnflags & SF_START_ON) + { + m_iState = STATE_ON; + SetNextThink (m_flDelay); + } +} + +void CSwitcher :: Think ( void ) +{ + if(pev->button == MODE_INCREMENT)//increase target number + { + m_index++; + if(m_index >= m_cTargets) m_index = 0; + } + else if(pev->button == MODE_DECREMENT) + { + m_index--; + if(m_index < 0) m_index = m_cTargets - 1; + } + else if(pev->button == MODE_RANDOM_VALUE) + { + m_index = RANDOM_LONG (0, m_cTargets - 1); + } + SetNextThink (m_flDelay); +} + +void CSwitcher :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if(IsLockedByMaster( useType )) return; + + if (useType == USE_SET)//set new target for activate (direct choose or increment\decrement) + { + if(pev->spawnflags & SF_START_ON) + { + m_iState = STATE_ON; + SetNextThink (m_flDelay); + return; + } + //set maximum priority for direct choose + if(value) + { + m_index = (value - 1); + if(m_index >= m_cTargets) m_index = -1; + return; + } + if(pev->button == MODE_INCREMENT) + { + m_index++; + if(m_index >= m_cTargets) m_index = 0; + } + else if(pev->button == MODE_DECREMENT)//increase target number + { + m_index--; + if(m_index < 0) m_index = m_cTargets - 1; + } + else if(pev->button == MODE_RANDOM_VALUE) + { + m_index = RANDOM_LONG (0, m_cTargets - 1); + } + } + else if (useType == USE_RESET) + { + //reset switcher + m_iState = STATE_OFF; + DontThink(); + m_index = 0; + return; + } + else if (useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, number of targets %d\n", GetStringForState( GetState()), m_cTargets - 1); + ALERT(at_console, "Current target %s, target number %d\n", STRING(m_iTargetName[ m_index ]), m_index ); + } + else if(m_index != -1)//fire any other USE_TYPE and right index + { + UTIL_FireTargets( m_iTargetName[ m_index ], m_hActivator, this, useType, value ); + } +} + +//======================================================================= +// Logic_counter +//======================================================================= + +class CLogicCounter : public CBaseLogic +{ +public: + void Spawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData *pkvd ); +}; +LINK_ENTITY_TO_CLASS( logic_counter, CLogicCounter ); + +void CLogicCounter :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "count")) + { + pev->frags = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "maxcount")) + { + pev->body = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CLogicCounter :: Spawn( void ) +{ + //Smart Field System © + if (pev->frags == 0) pev->frags = 2; + if (pev->body == 0) pev->body = 100; + if (pev->frags == -1) pev->frags = RANDOM_LONG (1, pev->body); + + //save number of impulses + pev->impulse = pev->frags; + m_iState = STATE_OFF;//always disable +} + +void CLogicCounter :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_SET) + { + if(value) pev->impulse = pev->frags = value; //write new value + else pev->impulse = pev->frags = RANDOM_LONG (1, pev->body);//write random value + } + else if (useType == USE_RESET) pev->frags = pev->impulse; //restore counter to default + else if (useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "start count %d, current count %.f\n",pev->impulse , pev->impulse - pev->frags ); + ALERT(at_console, "left activates: %.f\n", pev->frags); + } + else //any other useType + { + pev->frags--; + if(pev->frags > 0) return; + + pev->frags = 0; //don't reset counter in negative value + UTIL_FireTargets( pev->target, pActivator, this, useType, value ); //activate target + } +} + +//======================================================================= +// Logic_usetype - sorting different usetypes +//======================================================================= + +class CLogicUseType : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( logic_usetype, CLogicUseType ); + +void CLogicUseType::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "toggle")) + { + pev->target = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "enable")) + { + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "disable")) + { + pev->netname = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "set")) + { + m_sSet = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "reset")) + { + m_sReset = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CLogicUseType::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(IsLockedByMaster( useType )) return; + if(useType == USE_TOGGLE) UTIL_FireTargets(pev->target, pActivator, this, useType, value ); + else if (useType == USE_ON) UTIL_FireTargets(pev->message, pActivator, this, useType, value ); + else if (useType == USE_OFF) UTIL_FireTargets(pev->netname, pActivator, this, useType, value ); + else if (useType == USE_SET) UTIL_FireTargets(m_sSet, pActivator, this, useType, value ); + else if (useType == USE_RESET)UTIL_FireTargets(m_sReset, pActivator, this, useType, value ); + else if (useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "This entity don't displayed settings!\n" ); + SHIFT; + } +} + +//======================================================================= +// Logic_scale - apply scale for value +//======================================================================= +class CLogicScale : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ) + { + if (FStrEq(pkvd->szKeyName, "mode")) + { + pev->impulse = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); + } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + ALERT(at_console, "Mode %s. Scale %.3f.\n", pev->impulse ? "Bool" : "Float", pev->scale); + SHIFT; + } + else + { + if(pev->impulse == 0)//bool logic + { + if(value >= 0.99)UTIL_FireTargets(pev->target, pActivator, this, USE_ON, 1 ); + if(value <= 0.01)UTIL_FireTargets(pev->target, pActivator, this, USE_OFF,0 ); + } + if(pev->impulse == 1)//direct scale + UTIL_FireTargets(pev->target, pActivator, this, USE_SET, value * pev->scale ); + if(pev->impulse == 2)//inverse sacle + UTIL_FireTargets(pev->target, pActivator, this, USE_SET, pev->scale * (1-value)); + } + } +}; +LINK_ENTITY_TO_CLASS( logic_scale, CLogicScale); + \ No newline at end of file diff --git a/server/ents/baselogic.h b/server/ents/baselogic.h new file mode 100644 index 00000000..a6dcd105 --- /dev/null +++ b/server/ents/baselogic.h @@ -0,0 +1,145 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#ifndef BASELOGIC_H +#define BASELOGIC_H + +#define MAX_MULTI_TARGETS 32 // maximum number of targets that can be added in a list + +class CBaseLogic : public CBaseEntity +{ +public: + BOOL IsLockedByMaster( void ); + BOOL IsLockedByMaster( USE_TYPE useType ); + BOOL IsLockedByMaster( CBaseEntity *pActivator ); + void FireTargets( USE_TYPE useType = USE_TOGGLE, float value = 0 ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + virtual void KeyValue( KeyValueData* pkvd); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + virtual STATE GetState( void ) { return m_iState; }; + + float m_flDelay; + float m_flWait; + EHANDLE m_hActivator; + EHANDLE m_hTarget; + STATE m_iState; + string_t m_sMaster; + string_t m_iszKillTarget; //evil stuff. agrhh + string_t m_sSet;//used for logic_usetype + string_t m_sReset;//used for logic_usetype + string_t m_globalstate; + float m_flMin, m_flMax; +}; + +#define SF_CORNER_WAITTRIG 0x001 +#define SF_CORNER_TELEPORT 0x002 +#define SF_CORNER_FIREONCE 0x004 + +class CPathCorner : public CBaseLogic +{ +public: + void Spawn( ); + void KeyValue( KeyValueData* pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + float GetDelay( void ){ return pev->spawnflags & SF_CORNER_WAITTRIG ? -1 : m_flWait; } + void GetSpeed( float *speed ) { if(pev->speed != 0) *speed = pev->speed; } + void UpdateTargets( void ); + void Link( void ); + void PostActivate( void ){ Link(); } + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + CPathCorner *Instance( edict_t *pent ) + { + if ( FClassnameIs( pent, "path_corner" ) ) + return (CPathCorner *)GET_PRIVATE(pent); + return NULL; + } + + CPathCorner *m_pNextPath1; + CPathCorner *m_pNextPath2; + CPathCorner *m_pPrevPath; + CBaseEntity *ValidPath( CBaseEntity *m_pPath ); + CBaseEntity *GetNext( void ); + CBaseEntity *GetPrev( void ){ return ValidPath( m_pPrevPath ); } + void SetPrev( CPathCorner *pPrev ) { m_pPrevPath = (CPathCorner *)ValidPath((CPathCorner *)pPrev); } +}; + +class CPathTrack : public CBaseLogic +{ +public: + void Spawn( void ); + void Activate( void ); + void KeyValue( KeyValueData* pkvd); + + void SetPrevious( CPathTrack *pprevious ); + void Link( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + CBaseEntity *ValidPath( CBaseEntity *ppath, int testFlag ); // Returns ppath if enabled, NULL otherwise + void Project( CBaseEntity *pstart, CBaseEntity *pend, Vector *origin, float dist ); + + static CPathTrack *Instance( edict_t *pent ); + + CBaseEntity *LookAhead( Vector *origin, float dist, int move ); + CBaseEntity *Nearest( Vector origin ); //notused + + CBaseEntity *GetNext( void ); + CBaseEntity *GetPrev( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static TYPEDESCRIPTION m_SaveData[]; +#if PATH_SPARKLE_DEBUG + void EXPORT Sparkle(void); +#endif + + float m_length; + string_t m_altName; + CPathTrack *m_pnext; + CPathTrack *m_pprevious; + CPathTrack *m_paltpath; +}; + +class CMultiSource : public CBaseLogic +{ +public: + void Spawn( ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + STATE GetState( void ); + void EXPORT Register( void ); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + EHANDLE m_rgEntities[MAX_MULTI_TARGETS]; + int m_rgTriggered[MAX_MULTI_TARGETS]; + + int m_iTotal; +}; + +#include "baseinfo.h" + +class CUtilRainModify : public CPointEntity +{ +public: + void Spawn( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + int drips; + RandomRange windXY; + RandomRange randXY; + float fadeTime; +}; + +#endif //BASELOGIC_H \ No newline at end of file diff --git a/server/ents/basemover.cpp b/server/ents/basemover.cpp new file mode 100644 index 00000000..908be493 --- /dev/null +++ b/server/ents/basemover.cpp @@ -0,0 +1,1834 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// basemover.cpp - base code for linear and +// angular moving brushes e.g. doors, buttons e.t.c +//======================================================================= + +#include "extdll.h" +#include "defaults.h" +#include "utils.h" +#include "cbase.h" +#include "client.h" +#include "saverestore.h" +#include "player.h" + +//======================================================================= +// main functions () +//======================================================================= +TYPEDESCRIPTION CBaseMover::m_SaveData[] = +{ + DEFINE_FIELD( CBaseMover, m_flBlockedTime, FIELD_TIME ), + DEFINE_FIELD( CBaseMover, m_iMode, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMover, m_flMoveDistance, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flLip, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flHeight, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flValue, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_vecFinalDest, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecPosition1, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecPosition2, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecFloor, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_pfnCallWhenMoveDone, FIELD_FUNCTION ), + DEFINE_FIELD( CBaseMover, m_vecAngle1, FIELD_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecAngle2, FIELD_VECTOR ), + DEFINE_FIELD( CBaseMover, m_flLinearMoveSpeed, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flAngularMoveSpeed, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_vecFinalAngle, FIELD_VECTOR ), + DEFINE_FIELD( CBaseMover, flTravelTime, FIELD_FLOAT ), +}; IMPLEMENT_SAVERESTORE( CBaseMover, CBaseBrush ); + +void CBaseMover :: AxisDir( void ) +{ + if ( pev->movedir != g_vecZero) return; + + //Don't change this! + if ( FBitSet(pev->spawnflags, SF_DOOR_ROTATE_X)) + pev->movedir = Vector ( 1, 0, 0 ); // around z-axis + else if ( FBitSet(pev->spawnflags, SF_DOOR_ROTATE_Z)) + pev->movedir = Vector ( 0, 0, 1 ); // around x-axis + else pev->movedir = Vector ( 0, 1, 0 ); // around y-axis +} + +void CBaseMover::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "lip")) + { + m_flLip = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "waveheight")) + { + //field for volume_water + pev->scale = atof(pkvd->szValue) * (1.0/8.0); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "type")) + { + m_iMode = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "locked_sound")) + { + m_iStartSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "movesound")) + { + m_iMoveSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "stopsound")) + { + m_iStopSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "distance") || FStrEq(pkvd->szKeyName, "rotation")) + { + m_flMoveDistance = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "height")) + { + m_flHeight = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "waveheight")) + { + //func_water wave height + pev->scale = atof(pkvd->szValue) * (1.0/8.0); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "contents")) + { + //func_water contents + pev->skin = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseBrush::KeyValue( pkvd ); +} + +//======================================================================= +// LinearMove +// +// calculate pev->velocity and pev->nextthink to reach vecDest from +// pev->origin traveling at flSpeed +//======================================================================= +void CBaseMover :: LinearMove( Vector vecInput, float flSpeed ) +{ + ASSERTSZ(flSpeed != 0, "LinearMove: no speed is defined!"); + + m_flLinearMoveSpeed = flSpeed; + m_vecFinalDest = vecInput; + + SetThink( LinearMoveNow ); + UTIL_SetThink( this ); +} + + +void CBaseMover :: LinearMoveNow( void ) +{ + Vector vecDest; + + if (m_pParent)vecDest = m_vecFinalDest + m_pParent->pev->origin; + else vecDest = m_vecFinalDest; + + // Already there? + if (vecDest == pev->origin) + { + LinearMoveDone(); + return; + } + + // set destdelta to the vector needed to move + Vector vecDestDelta = vecDest - pev->origin; + + // divide vector length by speed to get time to reach dest + flTravelTime = vecDestDelta.Length() / m_flLinearMoveSpeed; + + // set nextthink to trigger a call to LinearMoveDone when dest is reached + SetNextThink( flTravelTime );//, TRUE ); + SetThink( LinearMoveDone ); + + UTIL_SetVelocity( this, vecDestDelta / flTravelTime ); +} + +//======================================================================= +// After moving, set origin to exact final destination, +// call "move done" function +//======================================================================= +void CBaseMover :: LinearMoveDone( void ) +{ + SetThink(LinearMoveDoneNow); + UTIL_SetThink( this ); +} + +void CBaseMover :: LinearMoveDoneNow( void ) +{ + UTIL_SetVelocity(this, g_vecZero); + if (m_pParent) UTIL_AssignOrigin(this, m_vecFinalDest + m_pParent->pev->origin); + else UTIL_AssignOrigin(this, m_vecFinalDest); + + DontThink(); + if( m_pfnCallWhenMoveDone )(this->*m_pfnCallWhenMoveDone)(); +} + +//======================================================================= +// AngularMove +// +// calculate pev->velocity and pev->nextthink to reach vecDest from +// pev->origin traveling at flSpeed +// Just like LinearMove, but rotational. +//======================================================================= +void CBaseMover :: AngularMove( Vector vecDestAngle, float flSpeed ) +{ + ASSERTSZ(flSpeed != 0, "AngularMove: no speed is defined!"); + + m_vecFinalAngle = vecDestAngle; + m_flAngularMoveSpeed = flSpeed; + + SetThink( AngularMoveNow ); + UTIL_SetThink( this ); +} + +void CBaseMover :: AngularMoveNow() +{ + Vector vecDestAngle; + + if (m_pParent) vecDestAngle = m_vecFinalAngle + m_pParent->pev->angles; + else vecDestAngle = m_vecFinalAngle; + + // Already there? + if (vecDestAngle == pev->angles) + { + AngularMoveDone(); + return; + } + + // set destdelta to the vector needed to move + Vector vecDestDelta = vecDestAngle - pev->angles; + + // divide by speed to get time to reach dest + flTravelTime = vecDestDelta.Length() / m_flAngularMoveSpeed; + + // set nextthink to trigger a call to AngularMoveDone when dest is reached + SetNextThink( flTravelTime );//, TRUE ); + SetThink( AngularMoveDone ); + + // scale the destdelta vector by the time spent traveling to get velocity + UTIL_SetAvelocity(this, vecDestDelta / flTravelTime ); +} + +void CBaseMover :: AngularMoveDone( void ) +{ + SetThink( AngularMoveDoneNow ); + UTIL_SetThink( this ); +} + +//======================================================================= +// After rotating, set angle to exact final angle, call "move done" function +//======================================================================= +void CBaseMover :: AngularMoveDoneNow( void ) +{ + UTIL_SetAvelocity(this, g_vecZero); + if (m_pParent) UTIL_AssignAngles(this, m_vecFinalAngle + m_pParent->pev->angles); + else UTIL_AssignAngles(this, m_vecFinalAngle); + + DontThink(); + if ( m_pfnCallWhenMoveDone ) (this->*m_pfnCallWhenMoveDone)(); +} + +//======================================================================= +// ComplexMove +// +// combinate LinearMove and AngularMove +//======================================================================= +void CBaseMover :: ComplexMove( Vector vecInput, Vector vecDestAngle, float flSpeed ) +{ + ASSERTSZ(flSpeed != 0, "ComplexMove: no speed is defined!"); + + //set shared speed for moving and rotating + m_flLinearMoveSpeed = flSpeed; + m_flAngularMoveSpeed = flSpeed; + + //save local variables into global containers + m_vecFinalDest = vecInput; + m_vecFinalAngle = vecDestAngle; + + SetThink( ComplexMoveNow ); + UTIL_SetThink( this ); +} + +void CBaseMover :: ComplexMoveNow( void ) +{ + Vector vecDest, vecDestAngle; + + if (m_pParent)//calculate destination + { + vecDest = m_vecFinalDest + m_pParent->pev->origin; + vecDestAngle = m_vecFinalAngle + m_pParent->pev->angles; + } + else + { + vecDestAngle = m_vecFinalAngle; + vecDest = m_vecFinalDest; + } + + // Already there? + if (vecDest == pev->origin && vecDestAngle == pev->angles) + { + ComplexMoveDone(); + return; + } + + // Calculate TravelTime and final angles + Vector vecDestLDelta = vecDest - pev->origin; + Vector vecDestADelta = vecDestAngle - pev->angles; + + // divide vector length by speed to get time to reach dest + flTravelTime = vecDestLDelta.Length() / m_flLinearMoveSpeed; + + // set nextthink to trigger a call to LinearMoveDone when dest is reached + SetNextThink( flTravelTime ); + SetThink( ComplexMoveDone ); + + //set linear and angular velocity now + UTIL_SetVelocity( this, vecDestLDelta / flTravelTime ); + UTIL_SetAvelocity(this, vecDestADelta / flTravelTime ); +} + +void CBaseMover :: ComplexMoveDone( void ) +{ + SetThink(ComplexMoveDoneNow); + UTIL_SetThink( this ); +} + +void CBaseMover :: ComplexMoveDoneNow( void ) +{ + UTIL_SetVelocity(this, g_vecZero); + UTIL_SetAvelocity(this, g_vecZero); + + if (m_pParent) + { + UTIL_AssignOrigin(this, m_vecFinalDest + m_pParent->pev->origin); + UTIL_AssignAngles(this, m_vecFinalAngle + m_pParent->pev->angles); + } + else + { + UTIL_AssignOrigin(this, m_vecFinalDest); + UTIL_AssignAngles(this, m_vecFinalAngle); + } + + DontThink(); + if( m_pfnCallWhenMoveDone )(this->*m_pfnCallWhenMoveDone)(); +} + +//======================================================================= +// func_door - classic QUAKE door +//======================================================================= +void CBaseDoor::Spawn( ) +{ + Precache(); + if(!IsRotatingDoor())UTIL_LinearVector( this ); + CBaseBrush::Spawn(); + + //make illusionary door + if(pev->spawnflags & SF_DOOR_PASSABLE || IsWater()) + pev->solid = SOLID_NOT; + else pev->solid = SOLID_BSP; + + pev->movetype = MOVETYPE_PUSH; + + if(IsRotatingDoor()) + { + // check for clockwise rotation + AxisDir(); + + if ( FBitSet (pev->spawnflags, SF_DOOR_ROTATE_BACKWARDS))pev->movedir = pev->movedir * -1; + m_vecAngle1 = pev->angles; + m_vecAngle2 = pev->angles + pev->movedir * m_flMoveDistance; + + ASSERTSZ(m_vecAngle1 != m_vecAngle2, "rotating door start/end positions are equal"); + SetBits (pFlags, PF_ANGULAR); + } + + UTIL_SetModel( ENT(pev), pev->model ); + UTIL_SetOrigin(this, pev->origin); + + //determine work style + if ( FBitSet ( pev->spawnflags, SF_DOOR_USE_ONLY )) SetTouch ( NULL ); + else SetTouch ( DoorTouch ); + + //if waittime is -1 - button forever stay pressed + if (FBitSet(pev->spawnflags, SF_DOOR_NO_AUTO_RETURN)) pev->impulse = 1;//toggleable door + if (m_flLip == 0) m_flLip = 4;//standart offset from Quake1 + if (pev->speed == 0) pev->speed = 100;//default speed + m_iState = STATE_OFF; + + if ( IsRotatingDoor() && FBitSet (pev->spawnflags, SF_DOOR_START_OPEN)) + { + pev->angles = m_vecAngle2; + Vector vecSav = m_vecAngle1; + m_vecAngle2 = m_vecAngle1; + m_vecAngle1 = vecSav; + pev->movedir = pev->movedir * -1; + } +} + +void CBaseDoor :: PostSpawn( void ) +{ + if (m_pParent) m_vecPosition1 = pev->origin - m_pParent->pev->origin; + else m_vecPosition1 = pev->origin; + + // Subtract 2 from size because the engine expands bboxes by 1 in all directions + m_vecPosition2 = m_vecPosition1 + (pev->movedir * (fabs( pev->movedir.x * (pev->size.x-2) ) + fabs( pev->movedir.y * (pev->size.y-2) ) + fabs( pev->movedir.z * (pev->size.z-2) ) - m_flLip)); + + ASSERTSZ(m_vecPosition1 != m_vecPosition2, "door start/end positions are equal"); + if ( FBitSet (pev->spawnflags, SF_DOOR_START_OPEN) ) + { + if (m_pParent) + { + m_vecSpawnOffset = m_vecSpawnOffset + (m_vecPosition2 + m_pParent->pev->origin) - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2 + m_pParent->pev->origin); + } + else + { + m_vecSpawnOffset = m_vecSpawnOffset + m_vecPosition2 - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2); + } + Vector vecTemp = m_vecPosition2; + m_vecPosition2 = m_vecPosition1; + m_vecPosition1 = vecTemp; + } +} + +void CBaseDoor::Precache( void ) +{ + CBaseBrush::Precache();//precache damage sound + + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + if(IsWater()) m_sounds = 0; + switch (m_sounds)//load movesound sounds (sound will play when door is moving) + { + case 1: pev->noise1 = UTIL_PrecacheSound ("doors/doormove1.wav");break; + case 2: pev->noise1 = UTIL_PrecacheSound ("doors/doormove2.wav");break; + case 3: pev->noise1 = UTIL_PrecacheSound ("doors/doormove3.wav");break; + case 4: pev->noise1 = UTIL_PrecacheSound ("doors/doormove4.wav");break; + case 5: pev->noise1 = UTIL_PrecacheSound ("doors/doormove5.wav");break; + case 6: pev->noise1 = UTIL_PrecacheSound ("doors/doormove6.wav");break; + case 7: pev->noise1 = UTIL_PrecacheSound ("doors/doormove7.wav");break; + case 8: pev->noise1 = UTIL_PrecacheSound ("doors/doormove8.wav");break; + case 9: pev->noise1 = UTIL_PrecacheSound ("doors/doormove9.wav");break; + case 10: pev->noise1 = UTIL_PrecacheSound ("doors/doormove10.wav");break; + case 0: pev->noise1 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise1 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + if(IsWater()) m_sounds = 0; + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop1.wav");break; + case 2: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop2.wav");break; + case 3: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop3.wav");break; + case 4: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop4.wav");break; + case 5: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop5.wav");break; + case 6: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop6.wav");break; + case 7: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop7.wav");break; + case 8: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop8.wav");break; + case 0: pev->noise2 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise2 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStartSound); + if(IsWater()) m_sounds = 0; + switch (m_sounds)//load locked sounds + { + case 1: pev->noise3 = UTIL_PrecacheSound ("buttons/button1.wav"); break; + case 2: pev->noise3 = UTIL_PrecacheSound ("buttons/button2.wav"); break; + case 3: pev->noise3 = UTIL_PrecacheSound ("buttons/button3.wav"); break; + case 4: pev->noise3 = UTIL_PrecacheSound ("buttons/button4.wav"); break; + case 5: pev->noise3 = UTIL_PrecacheSound ("buttons/button5.wav"); break; + case 6: pev->noise3 = UTIL_PrecacheSound ("buttons/button6.wav"); break; + case 7: pev->noise3 = UTIL_PrecacheSound ("buttons/button7.wav"); break; + case 8: pev->noise3 = UTIL_PrecacheSound ("buttons/button8.wav"); break; + case 9: pev->noise3 = UTIL_PrecacheSound ("buttons/button9.wav"); break; + case 10: pev->noise3 = UTIL_PrecacheSound ("buttons/button10.wav"); break; + case 11: pev->noise3 = UTIL_PrecacheSound ("buttons/button11.wav"); break; + case 12: pev->noise3 = UTIL_PrecacheSound ("buttons/latchlocked1.wav"); break; + case 13: pev->noise3 = UTIL_PrecacheSound ("buttons/latchunlocked1.wav"); break; + case 14: pev->noise3 = UTIL_PrecacheSound ("buttons/buttons/lightswitch2.wav"); break; + case 0: pev->noise3 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise3 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } +} + +void CBaseDoor::DoorTouch( CBaseEntity *pOther ) +{ + //make delay before retouching + if ( gpGlobals->time < pev->dmgtime) return; + pev->dmgtime = gpGlobals->time + 1.0; + //m_hActivator = pOther;// remember who activated the door + + if (!FStringNull(pev->targetname)) + { + // play locked sound + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise3), m_flVolume, ATTN_NORM); + return; + } + + if(pOther->IsPlayer())Use ( pOther, this, USE_TOGGLE, 1 );//player always sending 1 +} + +void CBaseDoor::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + if(IsLockedByMaster( useType ))//passed only USE_SHOWINFO + { + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise3), m_flVolume, ATTN_NORM); + return; + } + if(useType == USE_SHOWINFO)//show info + { + DEBUGHEAD; + Msg( "State: %s, Speed %g\n", GetStringForState( GetState()), pev->speed ); + Msg( "Texture frame: %g. WaitTime: %g\n", pev->frame, m_flWait); + } + else if(m_iState != STATE_DEAD)//activate door + { + //NOTE: STATE_DEAD is better method for simulate m_flWait -1 without fucking SetThink() + if (m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF )return;//door in-moving + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if(useType == USE_ON) + { + if( m_iState == STATE_OFF )DoorGoUp(); + } + else if( useType == USE_OFF ) + { + if(m_iState == STATE_ON && pev->impulse) DoorGoDown(); + } + else if( useType == USE_SET )pev->frame = !pev->frame; //change texture + } +} + +void CBaseDoor::DoorGoUp( void ) +{ + // It could be going-down, if blocked. + ASSERT(m_iState == STATE_OFF || m_iState == STATE_TURN_OFF); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + m_iState = STATE_TURN_ON; + SetMoveDone( DoorHitTop ); + SetTouch( NULL ); + + UTIL_FireTargets( pev->target, m_hActivator, this, USE_ON ); + if(IsRotatingDoor()) + { + int sign = 1; + if ( m_hActivator != NULL && !FBitSet( pev->spawnflags, SF_DOOR_ONEWAY ) && pev->movedir.y) + { + // Y axis rotation, move away from the player + Vector vec = m_hActivator->pev->origin - pev->origin; + UTIL_MakeVectors ( m_hActivator->pev->angles ); + Vector vnext = (m_hActivator->pev->origin + (gpGlobals->v_forward * 10)) - pev->origin; + if ( (vec.x * vnext.y - vec.y * vnext.x) < 0 ) sign = -1; + } + AngularMove(m_vecAngle2 * sign, pev->speed); + } + else LinearMove(m_vecPosition2, pev->speed); +} + +void CBaseDoor::DoorHitTop( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1) ); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise2), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_ON); + m_iState = STATE_ON; + + if(pev->impulse == 0)//time base door + { + if(m_flWait == -1) m_iState = STATE_DEAD;//keep door in this position + else + { + SetThink( DoorGoDown ); + SetNextThink( m_flWait ); + } + } + else if ( !FBitSet ( pev->spawnflags, SF_DOOR_USE_ONLY )) SetTouch( DoorTouch ); + + // Fire the close target (if startopen is set, then "top" is closed) + if (pev->spawnflags & SF_DOOR_START_OPEN) + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_TOGGLE ); + UTIL_FireTargets( pev->target, m_hActivator, this, USE_TOGGLE ); +} + +void CBaseDoor::DoorGoDown( void ) +{ + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_ON); + m_iState = STATE_TURN_OFF; + SetMoveDone( DoorHitBottom ); + if(IsRotatingDoor())AngularMove( m_vecAngle1, pev->speed); + else LinearMove(m_vecPosition1, pev->speed); +} + +void CBaseDoor::DoorHitBottom( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1) ); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise2), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_OFF); + m_iState = STATE_OFF; + + if ( FBitSet ( pev->spawnflags, SF_DOOR_USE_ONLY )) SetTouch ( NULL ); + else SetTouch( DoorTouch ); + + if (!(pev->spawnflags & SF_DOOR_START_OPEN)) + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_TOGGLE ); + UTIL_FireTargets( pev->target, m_hActivator, this, USE_TOGGLE ); +} + +void CBaseDoor::Blocked( CBaseEntity *pOther ) +{ + UTIL_AssignOrigin(this, pev->origin); + //make delay before retouching + if ( gpGlobals->time < m_flBlockedTime) return; + m_flBlockedTime = gpGlobals->time + 0.5; + + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE) m_pParent->Blocked( pOther); + if ( pev->dmg ) pOther->TakeDamage( pev, pev, pev->dmg, DMG_CRUSH ); + + if (m_flWait >= 0) + { + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1) ); + if (m_iState == STATE_TURN_ON) DoorGoDown(); + else if (m_iState == STATE_TURN_OFF) DoorGoUp(); + } + + //what the hell does this ? + //UTIL_SynchDoors( this ); + SetNextThink( 0 ); +} +LINK_ENTITY_TO_CLASS( func_door, CBaseDoor ); +LINK_ENTITY_TO_CLASS( func_water, CBaseDoor ); +LINK_ENTITY_TO_CLASS( func_door_rotating, CRotDoor ); + +//======================================================================= +// func_momentary_door +//======================================================================= +void CMomentaryDoor::Precache( void ) +{ + CBaseBrush::Precache(); + + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove1.wav");break; + case 2: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove2.wav");break; + case 3: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove3.wav");break; + case 4: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove4.wav");break; + case 5: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove5.wav");break; + case 6: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove6.wav");break; + case 0: pev->noise = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop1.wav");break; + case 2: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop2.wav");break; + case 3: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop3.wav");break; + case 4: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop4.wav");break; + case 5: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop5.wav");break; + case 6: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop6.wav");break; + case 7: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop7.wav");break; + case 8: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop8.wav");break; + case 0: pev->noise2 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise2 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } +} + +void CMomentaryDoor::Spawn( void ) +{ + Precache(); + CBaseBrush::Spawn(); + UTIL_LinearVector( this );//movement direction + + if(pev->spawnflags & SF_NOTSOLID)pev->solid = SOLID_NOT; //make illusionary wall + else pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + + m_iState = STATE_OFF; + UTIL_SetOrigin(this, pev->origin); + UTIL_SetModel( ENT(pev), pev->model ); + SetTouch( NULL );//just in case +} + +void CMomentaryDoor::PostSpawn( void ) +{ + if (m_pParent) m_vecPosition1 = pev->origin - m_pParent->pev->origin; + else m_vecPosition1 = pev->origin; + + // Subtract 2 from size because the engine expands bboxes by 1 in all directions making the size too big + m_vecPosition2 = m_vecPosition1 + (pev->movedir * (fabs( pev->movedir.x * (pev->size.x-2) ) + fabs( pev->movedir.y * (pev->size.y-2) ) + fabs( pev->movedir.z * (pev->size.z-2) ) - m_flLip)); + ASSERTSZ(m_vecPosition1 != m_vecPosition2, "door start/end positions are equal"); + + if(pev->spawnflags & SF_START_ON) + { + if (m_pParent) + { + m_vecSpawnOffset = m_vecSpawnOffset + (m_vecPosition2 + m_pParent->pev->origin) - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2 + m_pParent->pev->origin); + } + else + { + m_vecSpawnOffset = m_vecSpawnOffset + m_vecPosition2 - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2); + } + Vector vecTemp = m_vecPosition2; + m_vecPosition2 = m_vecPosition1; + m_vecPosition1 = vecTemp; + } +} + +void CMomentaryDoor::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_ON) value = 1; + else if(useType == USE_OFF) value = 0; + else if(useType == USE_SET); + else if(useType == USE_SHOWINFO)//show info + { + Msg("======/Xash Debug System/======\n"); + Msg("classname: %s\n", STRING(pev->classname)); + Msg("State: %s, Lip %.2f\n", GetStringForState( GetState()), m_flLip ); + SHIFT; + } + else return; + + if ( value > 1.0 )value = 1.0; + + if (IsLockedByMaster()) return; + Vector move = m_vecPosition1 + (value * (m_vecPosition2 - m_vecPosition1)); + + float speed = 0; + Vector delta; + + if (pev->speed) speed = pev->speed; + else + { + // default: get there in 0.1 secs + delta = move - pev->origin; + speed = delta.Length() * 10; + } + + //FIXME: allow for it being told to move at the same speed in the _opposite_ direction! + if ( speed != 0 ) + { + // This entity only thinks when it moves + if ( m_iState == STATE_OFF ) + { + //ALERT(at_console,"USE: start moving to %f %f %f.\n", move.x, move.y, move.z); + m_iState = STATE_ON; + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + } + + LinearMove( move, speed ); + SetMoveDone( MomentaryMoveDone ); + } +} + +void CMomentaryDoor::MomentaryMoveDone( void ) +{ + m_iState = STATE_OFF; + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise2), m_flVolume, ATTN_NORM); +} +LINK_ENTITY_TO_CLASS( func_momentary_door, CMomentaryDoor ); + +//======================================================================= +// func_platform (a lift\elevator) +//======================================================================= +void CBasePlatform::Precache( void ) +{ + CBaseBrush::Precache();//precache damage sound + + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + switch (m_sounds)//load movesound sounds (sound will play when door is moving) + { + case 1: pev->noise = UTIL_PrecacheSound ("plats/bigmove1.wav");break; + case 2: pev->noise = UTIL_PrecacheSound ("plats/bigmove2.wav");break; + case 3: pev->noise = UTIL_PrecacheSound ("plats/elevmove1.wav");break; + case 4: pev->noise = UTIL_PrecacheSound ("plats/elevmove2.wav");break; + case 5: pev->noise = UTIL_PrecacheSound ("plats/elevmove3.wav");break; + case 6: pev->noise = UTIL_PrecacheSound ("plats/freightmove1.wav");break; + case 7: pev->noise = UTIL_PrecacheSound ("plats/freightmove2.wav");break; + case 8: pev->noise = UTIL_PrecacheSound ("plats/heavymove1.wav");break; + case 9: pev->noise = UTIL_PrecacheSound ("plats/rackmove1.wav");break; + case 10: pev->noise = UTIL_PrecacheSound ("plats/railmove1.wav");break; + case 11: pev->noise = UTIL_PrecacheSound ("plats/squeekmove1.wav");break; + case 12: pev->noise = UTIL_PrecacheSound ("plats/talkmove1.wav");break; + case 13: pev->noise = UTIL_PrecacheSound ("plats/talkmove2.wav");break; + case 0: pev->noise = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise1 = UTIL_PrecacheSound ("plats/bigstop1.wav");break; + case 2: pev->noise1 = UTIL_PrecacheSound ("plats/bigstop2.wav");break; + case 3: pev->noise1 = UTIL_PrecacheSound ("plats/freightstop1.wav");break; + case 4: pev->noise1 = UTIL_PrecacheSound ("plats/heavystop2.wav");break; + case 5: pev->noise1 = UTIL_PrecacheSound ("plats/rackstop1.wav");break; + case 6: pev->noise1 = UTIL_PrecacheSound ("plats/railstop1.wav");break; + case 7: pev->noise1 = UTIL_PrecacheSound ("plats/squeekstop1.wav");break; + case 8: pev->noise1 = UTIL_PrecacheSound ("plats/talkstop1.wav");break; + case 0: pev->noise1 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise1 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + UTIL_PrecacheSound( "buttons/button11.wav" );//error sound +} + +void CBasePlatform :: Setup( void ) +{ + Precache(); //precache moving & stop sounds + + pev->angles = g_vecZero; + + pev->solid = SOLID_BSP; + + pev->movetype = MOVETYPE_PUSH; + UTIL_SetOrigin(this, pev->origin); + UTIL_SetSize(pev, pev->mins, pev->maxs); + UTIL_SetModel(ENT(pev), pev->model ); + // vecPosition1 is the top position, vecPosition2 is the bottom + if (m_pParent) m_vecPosition1 = pev->origin - m_pParent->pev->origin; + else m_vecPosition1 = pev->origin; + m_vecPosition2 = m_vecPosition1; + + if(IsMovingPlatform() || IsComplexPlatform()) + { + m_vecPosition2.z = m_vecPosition2.z + step(); + //m_vecPosition2.z = pev->origin.z - m_flHeight; + ASSERTSZ(m_vecPosition1 != m_vecPosition2, "moving platform start/end positions are equal\n"); + } + if(IsRotatingPlatform() || IsComplexPlatform()) + { + if ( m_flMoveDistance < 0 ) pev->movedir = pev->movedir * -1; + + AxisDir(); + m_vecAngle1 = pev->angles; + m_vecAngle2 = pev->angles + pev->movedir * m_flMoveDistance; + + ASSERTSZ(m_vecAngle1 != m_vecAngle2, "rotating platform start/end positions are equal\n"); + SetBits (pFlags, PF_ANGULAR); + } + CBaseBrush::Spawn(); + + if (pev->speed == 0) pev->speed = 150; + m_iState = STATE_OFF; + +} + +void CBasePlatform :: PostSpawn( void ) +{ + if ( FBitSet( pev->spawnflags, SF_START_ON ) ) + { + if (m_pParent) UTIL_AssignOrigin (this, m_vecPosition2 + m_pParent->pev->origin); + else UTIL_AssignOrigin (this, m_vecPosition2); + UTIL_AssignAngles(this, m_vecAngle2); + m_iState = STATE_ON; + } + else + { + if (m_pParent) UTIL_AssignOrigin (this, m_vecPosition1 + m_pParent->pev->origin); + else UTIL_AssignOrigin (this, m_vecPosition1); + UTIL_AssignAngles(this, m_vecAngle1); + m_iState = STATE_OFF; + } +} + +void CBasePlatform :: Spawn( void ) +{ + Setup(); +} + +void CBasePlatform :: PostActivate( void ) +{ + if(m_iState == STATE_TURN_OFF || m_iState == STATE_TURN_ON)//platform "in-moving" ? restore sound! + { + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + } +} + +void CBasePlatform :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) GoUp(); + else if ( useType == USE_OFF ) GoDown(); + else if ( useType == USE_SET ) GoToFloor( value ); + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, floor %g\n", GetStringForState( GetState()), CalcFloor()); + Msg( "distance %g, speed %g\n", m_flMoveDistance, pev->speed); + } +} + +void CBasePlatform :: GoToFloor( float floor ) +{ + float curfloor = CalcFloor(); + m_flValue = floor; + + if(curfloor <= 0) return; + if(curfloor == floor) return; //already there? + + m_vecFloor = m_vecPosition1; + m_vecFloor.z = pev->origin.z + (floor * step()) - (curfloor * step()); + + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + + if(floor > curfloor)m_iState = STATE_TURN_ON; + else m_iState = STATE_TURN_OFF; + + SetMoveDone( HitFloor ); + + if(fabs(floor - curfloor) > 1.0) //create floor informator for prop_counter + { + CBaseEntity *pFloor = CBaseEntity::Create( "floorent", pev->origin, g_vecZero, edict() ); + pFloor->pev->target = pev->netname; + pFloor->PostActivate(); + } + LinearMove(m_vecFloor, pev->speed); +} + +void CBasePlatform :: HitFloor( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF); + UTIL_FireTargets( pev->target, m_hActivator, this, USE_TOGGLE, m_flValue ); + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_SET, m_flValue ); + m_vecPosition2 = pev->origin; //save current floor + if(m_iState == STATE_TURN_ON) m_iState = STATE_ON; + if(m_iState == STATE_TURN_OFF) m_iState = STATE_OFF; +} + +void CBasePlatform :: GoDown( void ) +{ + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_ON || m_iState == STATE_TURN_ON); + m_iState = STATE_TURN_OFF; + SetMoveDone( HitBottom ); + + if(IsRotatingPlatform()) AngularMove( m_vecAngle1, pev->speed ); + else if(IsMovingPlatform()) LinearMove( m_vecPosition1, pev->speed ); + else if(IsComplexPlatform())ComplexMove( m_vecPosition1, m_vecAngle1, pev->speed ); + else HitBottom();//don't brake platform status +} + +void CBasePlatform :: HitBottom( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_OFF); + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_SET, 1 ); + m_iState = STATE_OFF; +} + +void CBasePlatform :: GoUp( void ) +{ + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_OFF || m_iState == STATE_TURN_OFF); + m_iState = STATE_TURN_ON; + SetMoveDone( HitTop ); + + if(IsRotatingPlatform()) AngularMove( m_vecAngle2, pev->speed ); + else if(IsMovingPlatform()) LinearMove( m_vecPosition2, pev->speed ); + else if(IsComplexPlatform())ComplexMove( m_vecPosition2, m_vecAngle2, pev->speed ); + else HitTop();//don't brake platform status +} + +void CBasePlatform :: HitTop( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_ON); + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_SET, 2 ); + m_iState = STATE_ON; +} + +void CBasePlatform :: Blocked( CBaseEntity *pOther ) +{ + UTIL_AssignOrigin(this, pev->origin); + //make delay before retouching + if ( gpGlobals->time < m_flBlockedTime) return; + m_flBlockedTime = gpGlobals->time + 0.5; + + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE) m_pParent->Blocked( pOther); + pOther->TakeDamage( pev, pev, 1, DMG_CRUSH ); + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + ASSERT(m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF); + + if (m_iState == STATE_TURN_ON) GoDown(); + else if (m_iState == STATE_TURN_OFF) GoUp(); + SetNextThink( 0 ); +} +LINK_ENTITY_TO_CLASS( func_platform, CBasePlatform ); +LINK_ENTITY_TO_CLASS( func_platrot, CBasePlatform ); + + +//======================================================================= +// func_train (Classic QUAKE Train) +//======================================================================= +TYPEDESCRIPTION CFuncTrain::m_SaveData[] = +{ + DEFINE_FIELD( CFuncTrain, pCurPath, FIELD_CLASSPTR ), + DEFINE_FIELD( CFuncTrain, pNextPath, FIELD_CLASSPTR ), +}; +IMPLEMENT_SAVERESTORE( CFuncTrain, CBasePlatform ); + +void CFuncTrain :: Spawn( void ) +{ + Precache(); //precache moving & stop sounds + CBaseBrush::Spawn(); + + if(pev->spawnflags & SF_DOOR_PASSABLE || IsWater())//make illusionary train + pev->solid = SOLID_NOT; + else pev->solid = SOLID_BSP; + + pev->movetype = MOVETYPE_PUSH; + UTIL_SetOrigin(this, pev->origin); + UTIL_SetSize(pev, pev->mins, pev->maxs); + UTIL_SetModel(ENT(pev), pev->model ); + + //determine method for calculating origin + if(pev->origin != g_vecZero) pev->impulse = 1; + + if (pev->speed == 0) pev->speed = 100; + m_iState = STATE_OFF; +} + +void CFuncTrain :: PostSpawn( void ) +{ + if (!FindPath()) return; + + if (pev->impulse) + { + m_vecSpawnOffset = m_vecSpawnOffset + pCurPath->pev->origin - pev->origin; + if (m_pParent) UTIL_AssignOrigin (this, pCurPath->pev->origin - m_pParent->pev->origin ); + else UTIL_AssignOrigin (this, pCurPath->pev->origin ); + } + else + { + m_vecSpawnOffset = m_vecSpawnOffset + (pCurPath->pev->origin - TrainOrg()) - pev->origin; + if (m_pParent) UTIL_AssignOrigin (this, pCurPath->pev->origin - TrainOrg() - m_pParent->pev->origin ); + else UTIL_AssignOrigin (this, pCurPath->pev->origin - TrainOrg()); + } + pev->target = pCurPath->pev->target; +} + +void CFuncTrain :: PostActivate( void ) +{ + if(m_iState == STATE_ON)//platform "in-moving" ? restore sound! + { + EMIT_SOUND (ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + } + if ( pev->spawnflags & SF_START_ON ) + { + m_iState = STATE_OFF; //restore sound on a next level + SetThink( Next ); //evil stuff... + SetNextThink( 0.1 ); + ClearBits( pev->spawnflags, SF_START_ON );//fire once + } +} + +void CFuncTrain::ClearPointers( void ) +{ + CBaseEntity::ClearPointers(); + pCurPath = NULL; + pNextPath = NULL; +} + +void CFuncTrain::OverrideReset( void ) +{ + // Are we moving? + if ( m_iState == STATE_ON ) + { + pev->target = pev->message; + Msg("Override target %s\n", STRING( pev->target )); + if (FindPath()) SetBits( pev->spawnflags, SF_START_ON );//PostActivate member + else Stop(); + } +} + +CBaseEntity *CFuncTrain::FindPath( void ) +{ + //find start track + Msg("Find corner %s\n", STRING( pev->target )); + pCurPath = UTIL_FindEntityByTargetname (NULL, STRING(pev->target)); + if(pCurPath && pCurPath->edict()) + { + return pCurPath; + } + return NULL; +} + +CBaseEntity *CFuncTrain::FindNextPath( void ) +{ + //safe way to check patch + if(!pCurPath) return FALSE; + + // get pointer to next target + if(pev->speed > 0)pNextPath = ((CPathCorner *)pCurPath)->GetNext(); + if(pev->speed < 0)pNextPath = ((CPathCorner *)pCurPath)->GetPrev(); + + if(pNextPath && pNextPath->edict()) //validate path + { + //record new value (this will be used after changelevel) + //pev->target = pNextPath->pev->targetname; + + //pCurPath = pNextPath; + Msg("CFuncTrain::FindNextPath: pCurPath %s pNextPath %s\n", STRING( pCurPath->pev->targetname ), STRING( pNextPath->pev->targetname )); + return pNextPath; //path found + } + + Stop();//stopping platform + return NULL; +} + +void CFuncTrain :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) Next(); + else if ( useType == USE_OFF ) Stop(); + else if (useType == USE_SET) Reverse();//reverse train + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + ALERT(at_console, "State: %s, speed %g\n", GetStringForState( GetState()), pev->speed ); + if(GetPrev() && GetPrev()->edict()) Msg("Prev path %s", STRING(GetPrev()->pev->targetname) ); + if(GetNext() && GetNext()->edict()) Msg("Next path %s", STRING(GetNext()->pev->targetname) ); + SHIFT; + } +} + +void CFuncTrain :: Next( void ) +{ + CBaseEntity *pTarg = FindPath(); + + if(!pTarg) return; + + //linear move to next corner. + if(m_iState == STATE_OFF)//enable train sound + { + STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noise) ); + EMIT_SOUND (ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + } + + ClearBits(pev->effects, EF_NOINTERP); //enable interpolation + m_iState = STATE_ON; + //pev->message = ((CPathCorner *)pCurPath)->GetNext()->pev->targetname; + pev->message = pev->target; + pev->target = pTarg->pev->target; + + /*if( pev->speed < 0 && FBitSet(pNextPath->pev->spawnflags, SF_CORNER_TELEPORT)) + { + SetBits(pev->effects, EF_NOINTERP); + if (m_pParent) UTIL_AssignOrigin(this, pNextPath->pev->origin - m_pParent->pev->origin ); + else UTIL_AssignOrigin(this, pNextPath->pev->origin ); + Wait(); // Get on with doing the next path corner. + return; + }*/ + + if (m_pParent) + { + if (pev->impulse) LinearMove( pTarg->pev->origin - m_pParent->pev->origin, fabs( pev->speed)); + else LinearMove (pTarg->pev->origin - TrainOrg() - m_pParent->pev->origin, fabs(pev->speed)); + } + else + { + if (pev->impulse) LinearMove( pTarg->pev->origin, fabs(pev->speed) ); + else LinearMove (pTarg->pev->origin - TrainOrg(), fabs(pev->speed) ); + } + pCurPath = pTarg; + SetMoveDone( Wait ); +} + +void CFuncTrain :: Wait( void ) +{ + UTIL_FireTargets(pev->netname, this, this, USE_TOGGLE ); + ((CPathCorner *)pCurPath)->UpdateTargets(); + ((CPathCorner *)pCurPath)->GetSpeed( &pev->speed ); + if(!Stop(((CPathCorner *)pCurPath)->GetDelay())) Next();// go to next corner +} + +BOOL CFuncTrain :: Teleport( void ) +{ + if( FBitSet(pNextPath->pev->spawnflags, SF_CORNER_TELEPORT)) + { + SetBits(pev->effects, EF_NOINTERP); + + //determine teleportation point + if(pev->speed > 0) + { + pNextPath = pNextPath->GetNext(); + ((CPathCorner *)pCurPath)->UpdateTargets(); + UTIL_FireTargets(pev->netname, this, this, USE_TOGGLE ); + } + if (m_pParent) UTIL_AssignOrigin(this, pNextPath->pev->origin - m_pParent->pev->origin ); + else UTIL_AssignOrigin(this, pNextPath->pev->origin ); + + pCurPath = pNextPath; + Next(); // Get on with doing the next path corner. + return TRUE; + } + return FALSE; +} + +BOOL CFuncTrain :: Stop( float flWait ) +{ + if( flWait == 0 ) return FALSE; + m_iState = STATE_OFF; + + // clear the sound channel. + STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noise) ); + EMIT_SOUND (ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + UTIL_SetVelocity(this, g_vecZero); + UTIL_SetAvelocity(this, g_vecZero); + if( flWait > 0) + { + SetNextThink( flWait ); + SetThink( Next ); + } + + //wait for retrigger + if(flWait == -1) DontThink(); + return TRUE; +} + +void CFuncTrain::Reverse( void ) +{ + //update path if dir changed + if(pev->speed != 0 && pNextPath && pNextPath->edict()) pCurPath = pNextPath; + pev->speed = -pev->speed; + if(m_iState == STATE_ON) Next(); //reverse now! +} + +void CFuncTrain :: Blocked( CBaseEntity *pOther ) +{ + // Keep "movewith" entities in line + UTIL_AssignOrigin(this, pev->origin); + + if ( gpGlobals->time < m_flBlockedTime) return; + m_flBlockedTime = gpGlobals->time + 0.5; + + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE) m_pParent->Blocked( pOther); + pOther->TakeDamage( pev, pev, 1, DMG_CRUSH ); + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + ASSERT(m_iState == STATE_ON); + + Stop( 0.5 ); +} +LINK_ENTITY_TO_CLASS( func_train, CFuncTrain ); + +//======================================================================= +// func_tracktrain (controllable train) +//======================================================================= +void CFuncTrackTrain :: Precache( void ) +{ + CBaseBrush::Precache();//precache damage sound + + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + switch (m_sounds) + { + case 1: pev->noise = UTIL_PrecacheSound("plats/ttrain1.wav");break; + case 2: pev->noise = UTIL_PrecacheSound("plats/ttrain2.wav");break; + case 3: pev->noise = UTIL_PrecacheSound("plats/ttrain3.wav");break; + case 4: pev->noise = UTIL_PrecacheSound("plats/ttrain4.wav");break; + case 5: pev->noise = UTIL_PrecacheSound("plats/ttrain6.wav");break; + case 6: pev->noise = UTIL_PrecacheSound("plats/ttrain7.wav");break; + default: pev->noise = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + //always constant + pev->noise1 = UTIL_PrecacheSound("plats/ttrain_brake1.wav"); + pev->noise2 = UTIL_PrecacheSound("plats/ttrain_start1.wav"); +} + +void CFuncTrackTrain :: Spawn( void ) +{ + if ( pev->speed == 0 ) m_speed = 100; + else m_speed = pev->speed; + pev->speed = 0; + pev->velocity = g_vecZero; + m_vecBaseAvel = pev->avelocity; //LRC - save it for later + pev->avelocity = g_vecZero; + pev->impulse = m_speed; + m_dir = 1; + + if ( FStringNull(pev->target) ) Msg("Warning: %s with no target!\n", STRING(pev->classname)); + + //if ( pev->spawnflags & SF_NOTSOLID ) pev->solid = SOLID_NOT; + if ( pev->spawnflags & 8 ) pev->solid = SOLID_NOT; //temp solution + else pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + SetBits (pFlags, PF_ANGULAR); + UTIL_SetModel( ENT(pev), pev->model ); + + UTIL_SetSize( pev, pev->mins, pev->maxs ); + UTIL_SetOrigin( this, pev->origin ); + pev->oldorigin = pev->origin; // Cache off placed origin for train controls + + m_controlMins = pev->mins; + m_controlMaxs = pev->maxs; + m_controlMaxs.z += 72; + + NextThink( 0.1, FALSE ); + SetThink( Find ); + Precache(); +} + +void CFuncTrackTrain :: NextThink( float thinkTime, BOOL alwaysThink ) +{ + if ( alwaysThink ) pev->flags |= FL_ALWAYSTHINK; + else pev->flags &= ~FL_ALWAYSTHINK; + SetNextThink( thinkTime, TRUE ); +} + +void CFuncTrackTrain :: Blocked( CBaseEntity *pOther ) +{ + // Blocker is on-ground on the train + if ( FBitSet( pOther->pev->flags, FL_ONGROUND ) && VARS(pOther->pev->groundentity) == pev ) + { + float deltaSpeed = fabs(pev->speed); + if ( deltaSpeed > 50 ) deltaSpeed = 50; + if ( !pOther->pev->velocity.z ) pOther->pev->velocity.z += deltaSpeed; + return; + } + else pOther->pev->velocity = (pOther->pev->origin - pev->origin ).Normalize() * pev->dmg; + if ( pev->dmg <= 0 ) return;// we can't hurt this thing, so we're not concerned with it + pOther->TakeDamage( pev, pev, pev->dmg, DMG_CRUSH ); +} + +void CFuncTrackTrain::OverrideReset( void ) +{ + NextThink( 0.1, FALSE ); + SetThink( NearestPath ); +} + +void CFuncTrackTrain :: Find( void ) +{ + pPath = (CPathTrack*)UTIL_FindEntityByTargetname( NULL, STRING(pev->target) ); + if ( !pPath ) return; + + entvars_t *pevTarget = pPath->pev; + if ( !FClassnameIs( pevTarget, "path_track" ) ) + { + ALERT( at_error, "func_track_train must be on a path of path_track\n" ); + pPath = NULL; + return; + } + + Vector nextPos = pevTarget->origin; + nextPos.z += m_height; + + Vector look = nextPos; + look.z -= m_height; + ((CPathTrack *)pPath)->LookAhead( &look, m_length, 0 ); + look.z += m_height; + + Vector vTemp = UTIL_VecToAngles( look - nextPos ); + vTemp.y += 180; + + if ( pev->spawnflags & SF_TRACKTRAIN_NOPITCH ) + { + vTemp.x = 0; + //pev->angles.x = 0; + } + + UTIL_AssignAngles(this, vTemp); + UTIL_AssignOrigin ( this, nextPos ); + + NextThink( 0.1, FALSE ); + SetThink( PostponeNext ); + pev->speed = m_startSpeed; + + UpdateSound(); +} + +TYPEDESCRIPTION CFuncTrackTrain::m_SaveData[] = +{ + DEFINE_FIELD( CFuncTrackTrain, pPath, FIELD_CLASSPTR ), + DEFINE_FIELD( CFuncTrackTrain, m_length, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTrackTrain, m_height, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTrackTrain, m_speed, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTrackTrain, m_dir, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTrackTrain, m_startSpeed, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTrackTrain, m_controlMins, FIELD_VECTOR ), + DEFINE_FIELD( CFuncTrackTrain, m_controlMaxs, FIELD_VECTOR ), + DEFINE_FIELD( CFuncTrackTrain, m_flVolume, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTrackTrain, m_flBank, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTrackTrain, m_oldSpeed, FIELD_FLOAT ), +}; +IMPLEMENT_SAVERESTORE( CFuncTrackTrain, CBaseMover ); + + +void CFuncTrackTrain :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "wheels")) + { + m_length = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "height")) + { + m_height = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "startspeed")) + { + m_startSpeed = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "sounds")) + { + m_iMoveSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "bank")) + { + m_flBank = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseMover::KeyValue( pkvd ); +} + + + + + +void CFuncTrackTrain :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ +// ALERT(at_debug, "TRAIN: use\n"); + + m_hActivator = pActivator; //AJH + + if (useType == USE_TOGGLE) + { + if(pev->speed != 0) useType = USE_OFF;//temp solution + else useType = USE_ON; + } + if (useType == USE_ON) + { + pev->speed = m_speed * m_dir; + PostponeNext(); + } + else if ( useType == USE_OFF ) + { + pev->speed = 0; + UTIL_SetVelocity(this, g_vecZero); //LRC + UTIL_SetAvelocity(this, g_vecZero); //LRC + StopSound(); + SetThink( NULL ); + } + else if ( useType == USE_SET ) + { + float delta = value; + + delta = ((int)(pev->speed * 4) / (int)m_speed)*0.25 + 0.25 * delta; + if ( delta > 1 ) + delta = 1; + else if ( delta < -1 ) + delta = -1; + if ( pev->spawnflags & SF_TRACKTRAIN_FORWARDONLY ) + { + if ( delta < 0 )delta = 0; + } + pev->speed = m_speed * delta; + + if(pev->speed == 0) + { + UTIL_SetVelocity(this, g_vecZero); + UTIL_SetAvelocity(this, g_vecZero); + StopSound(); + SetThink( NULL ); + } + if(pPath == NULL) + { + delta = 0; //G-Cont. Set speed to 0, and don't controls, if tracktrain on trackchange + return; + } + PostponeNext(); + ALERT( at_aiconsole, "TRAIN(%s), speed to %.2f\n", STRING(pev->targetname), pev->speed ); + } +} + +#define TRAIN_STARTPITCH 60 +#define TRAIN_MAXPITCH 200 +#define TRAIN_MAXSPEED 1000 // approx max speed for sound pitch calculation + +void CFuncTrackTrain :: StopSound( void ) +{ + // if sound playing, stop it + if (m_soundPlaying && pev->noise) + { + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND_DYN(ENT(pev), CHAN_ITEM, "plats/ttrain_brake1.wav", m_flVolume, ATTN_NORM, 0, 100); + } + + m_soundPlaying = 0; +} + +void CFuncTrackTrain :: UpdateSound( void ) +{ + float flpitch; + + if (!pev->noise) + return; + + flpitch = TRAIN_STARTPITCH + (abs(pev->speed) * (TRAIN_MAXPITCH - TRAIN_STARTPITCH) / TRAIN_MAXSPEED); + + if (!m_soundPlaying) + { + // play startup sound for train + EMIT_SOUND_DYN(ENT(pev), CHAN_ITEM, "plats/ttrain_start1.wav", m_flVolume, ATTN_NORM, 0, 100); + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM, 0, (int) flpitch); + m_soundPlaying = 1; + } + else + { + // update pitch + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM, SND_CHANGE_PITCH, (int) flpitch); + } +} + +void CFuncTrackTrain::ClearPointers( void ) +{ + CBaseEntity::ClearPointers(); + pPath = NULL; +} + +void CFuncTrackTrain :: PostActivate( void ) +{ +} +void CFuncTrackTrain :: PostponeNext( void ) +{ + DesiredAction();//temp fix + //UTIL_SetAction( this ); +} + +void CFuncTrackTrain :: DesiredAction( void ) // Next( void ) +{ + float time = 0.5; + + if ( !pev->speed ) + { + UTIL_SetVelocity(this, g_vecZero); + DontThink(); + ALERT( at_aiconsole, "TRAIN(%s): Speed is 0\n", STRING(pev->targetname) ); + StopSound(); + return; + } + + if ( !pPath ) + { + UTIL_SetVelocity(this, g_vecZero); + DontThink(); + ALERT( at_aiconsole, "TRAIN(%s): Lost path\n", STRING(pev->targetname) ); + StopSound(); + return; + } + + UpdateSound(); + + Vector nextPos = pev->origin; + + nextPos.z -= m_height; + CBaseEntity *pnext = ((CPathTrack *)pPath)->LookAhead( &nextPos, pev->speed * 0.1, 1 ); + nextPos.z += m_height; + + UTIL_SetVelocity( this, (nextPos - pev->origin) * 10 ); //LRC + Vector nextFront = pev->origin; + + nextFront.z -= m_height; + if ( m_length > 0 ) + ((CPathTrack *)pPath)->LookAhead( &nextFront, m_length, 0 ); + else + ((CPathTrack *)pPath)->LookAhead( &nextFront, 100, 0 ); + nextFront.z += m_height; + + Vector delta = nextFront - pev->origin; + + Vector angles = UTIL_VecToAngles( delta ); + // The train actually points west + angles.y += 180; //LRC, FIXME: add a 'built facing' field. + + angles.fixangle(); + pev->angles.fixangle(); + + if ( !pnext || (delta.x == 0 && delta.y == 0) ) angles = pev->angles; + + float vy, vx, vz; + if ( !(pev->spawnflags & SF_TRACKTRAIN_NOPITCH) ) vx = 10*UTIL_AngleDistance( angles.x, pev->angles.x ); + else vx = m_vecBaseAvel.x; + + if ( !(pev->spawnflags & SF_TRACKTRAIN_NOYAW) ) vy = 10*UTIL_AngleDistance( angles.y, pev->angles.y ); + else vy = m_vecBaseAvel.y; + + if ( m_flBank != 0 ) + { + if ( pev->avelocity.y < -5 ) vz = UTIL_AngleDistance( UTIL_ApproachAngle( -m_flBank, pev->angles.z, m_flBank*2 ), pev->angles.z); + else if ( pev->avelocity.y > 5 ) vz = UTIL_AngleDistance( UTIL_ApproachAngle( m_flBank, pev->angles.z, m_flBank*2 ), pev->angles.z); + else vz = UTIL_AngleDistance( UTIL_ApproachAngle( 0, pev->angles.z, m_flBank*4 ), pev->angles.z) * 4; + } + else vz = m_vecBaseAvel.z; + UTIL_SetAvelocity(this, Vector(vx, vy, vz)); + + if ( pnext ) + { + if ( pnext != pPath ) + { + CBaseEntity *pFire; + if ( pev->speed >= 0 ) // check whether we're going forwards or backwards + pFire = pnext; + else + pFire = pPath; + + pPath = pnext; + // Fire the pass target if there is one + if ( pFire->pev->message ) + { + UTIL_FireTargets( pFire->pev->message, this, this, USE_TOGGLE ); + if ( FBitSet( pFire->pev->spawnflags, SF_PATH_FIREONCE ) ) + pFire->pev->message = 0; + } + + if ( pFire->pev->spawnflags & SF_PATH_DISABLE_TRAIN ) + pev->spawnflags |= SF_TRACKTRAIN_NOCONTROL; + + float setting = ((int)(pev->speed*4) / (int)m_speed) / 4.0; //LRC - one of { 1, 0.75, 0.5, 0.25, 0, ... -1 } + + CBaseEntity* pDest; //LRC - the path_track we're heading for, after pFire. + if (pev->speed > 0) + pDest = pFire->GetNext(); + else + pDest = pFire->GetPrev(); + + if ( pFire->pev->speed != 0) + { + //ALERT( at_console, "TrackTrain setting is %d / %d = %.2f\n", (int)(pev->speed*4), (int)m_speed, setting ); + + switch ( (int)(pFire->pev->armortype) ) + { + case PATHSPEED_SET: + // Don't override speed if under user control + if (pev->spawnflags & SF_TRACKTRAIN_NOCONTROL) + pev->speed = pFire->pev->speed; + ALERT( at_aiconsole, "TrackTrain %s speed set to %4.2f\n", STRING(pev->targetname), pev->speed ); + break; + case PATHSPEED_SET_MASTER: + m_speed = pFire->pev->speed; + pev->impulse = m_speed; + pev->speed = setting * m_speed; + ALERT( at_aiconsole, "TrackTrain %s master speed set to %4.2f\n", STRING(pev->targetname), pev->speed ); + break; + case PATHSPEED_ACCEL: + m_speed += pFire->pev->speed; + pev->impulse = m_speed; + pev->speed = setting * m_speed; + ALERT( at_aiconsole, "TrackTrain %s speed accel to %4.2f\n", STRING(pev->targetname), pev->speed ); + break; + case PATHSPEED_TIME: + float distance = (pev->origin - pDest->pev->origin).Length(); + //ALERT(at_debug, "pFire=%s, distance=%.2f, ospeed=%.2f, nspeed=%.2f\n", STRING(pFire->pev->targetname), distance, pev->speed, distance / pFire->pev->speed); + m_speed = distance / pFire->pev->speed; + pev->impulse = m_speed; + pev->speed = setting * m_speed; + ALERT( at_aiconsole, "TrackTrain %s speed to %4.2f (timed)\n", STRING(pev->targetname), pev->speed ); + break; + } + } + //LRC- FIXME: add support, here, for a Teleport flag. + } + SetThink( PostponeNext ); + NextThink( time, TRUE ); + } + else // end of path, stop + { + Vector vecTemp; //LRC + StopSound(); + vecTemp = (nextPos - pev->origin); //LRC + + UTIL_SetAvelocity(this, g_vecZero); + float distance = vecTemp.Length(); //LRC + m_oldSpeed = pev->speed; + + + pev->speed = 0; + + // Move to the dead end + + // Are we there yet? + if ( distance > 0 ) + { + // no, how long to get there? + time = distance / m_oldSpeed; + UTIL_SetVelocity( this, vecTemp * (m_oldSpeed / distance) ); + SetThink( DeadEnd ); + NextThink( time, FALSE ); + } + else + { + UTIL_SetVelocity( this, vecTemp ); + DeadEnd(); + } + } +} + + +void CFuncTrackTrain::DeadEnd( void ) +{ + // Fire the dead-end target if there is one + CBaseEntity *pTrack, *pNext; + + pTrack = pPath; + + ALERT( at_aiconsole, "TRAIN(%s): Dead end ", STRING(pev->targetname) ); + // Find the dead end path node + // HACKHACK -- This is bugly, but the train can actually stop moving at a different node depending on it's speed + // so we have to traverse the list to it's end. + if ( pTrack ) + { + if ( m_oldSpeed < 0 ) + { + do + { + pNext = ((CPathTrack *)pTrack)->ValidPath( pTrack->GetPrev(), TRUE ); + if ( pNext ) + pTrack = pNext; + } while ( pNext ); + } + else + { + do + { + pNext = ((CPathTrack *)pTrack)->ValidPath( pTrack->GetNext(), TRUE ); + if ( pNext ) + pTrack = pNext; + } while ( pNext ); + } + } + + UTIL_SetVelocity( this, g_vecZero ); + UTIL_SetAvelocity(this, g_vecZero ); + + if ( pTrack ) + { + ALERT( at_aiconsole, "at %s\n", STRING(pTrack->pev->targetname) ); + if ( pTrack->pev->netname ) + UTIL_FireTargets( pTrack->pev->netname, this, this, USE_TOGGLE ); + } +} + + +void CFuncTrackTrain :: SetControls( entvars_t *pevControls ) +{ + Vector offset = pevControls->origin - pev->oldorigin; + + m_controlMins = pevControls->mins + offset; + m_controlMaxs = pevControls->maxs + offset; +} + + +BOOL CFuncTrackTrain :: OnControls( entvars_t *pevTest ) +{ + Vector offset = pevTest->origin - pev->origin; + + if ( pev->spawnflags & SF_TRACKTRAIN_NOCONTROL ) + return FALSE; + + // Transform offset into local coordinates + UTIL_MakeVectors( pev->angles ); + Vector local; + local.x = DotProduct( offset, gpGlobals->v_forward ); + local.y = -DotProduct( offset, gpGlobals->v_right ); + local.z = DotProduct( offset, gpGlobals->v_up ); + + if ( local.x >= m_controlMins.x && local.y >= m_controlMins.y && local.z >= m_controlMins.z && + local.x <= m_controlMaxs.x && local.y <= m_controlMaxs.y && local.z <= m_controlMaxs.z ) + return TRUE; + + return FALSE; +} + +void CFuncTrackTrain :: NearestPath( void ) +{ + CBaseEntity *pTrack = NULL; + CBaseEntity *pNearest = NULL; + float dist, closest; + + closest = 1024; + + while ((pTrack = UTIL_FindEntityInSphere( pTrack, pev->origin, 1024 )) != NULL) + { + // filter out non-tracks + if ( !(pTrack->pev->flags & (FL_CLIENT|FL_MONSTER)) && FClassnameIs( pTrack->pev, "path_track" ) ) + { + dist = (pev->origin - pTrack->pev->origin).Length(); + if ( dist < closest ) + { + closest = dist; + pNearest = pTrack; + } + } + } + + if ( !pNearest ) + { + ALERT( at_debug, "Can't find a nearby track !!!\n" ); + SetThink(NULL); + return; + } + + ALERT( at_aiconsole, "TRAIN: %s, Nearest track is %s\n", STRING(pev->targetname), STRING(pNearest->pev->targetname) ); + // If I'm closer to the next path_track on this path, then it's my real path + pTrack = ((CPathTrack *)pNearest)->GetNext(); + if ( pTrack ) + { + if ( (pev->origin - pTrack->pev->origin).Length() < (pev->origin - pNearest->pev->origin).Length() ) + pNearest = pTrack; + } + + pPath = (CPathTrack *)pNearest; + + if ( pev->speed != 0 ) + { + NextThink( 0.1, FALSE ); + SetThink( PostponeNext ); + } +} +LINK_ENTITY_TO_CLASS( func_tracktrain, CFuncTrackTrain ); \ No newline at end of file diff --git a/server/ents/basemover.cpp.save b/server/ents/basemover.cpp.save new file mode 100644 index 00000000..4b9f8765 --- /dev/null +++ b/server/ents/basemover.cpp.save @@ -0,0 +1,1352 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// basemover.cpp - base code for linear and +// angular moving brushes e.g. doors, buttons e.t.c +//======================================================================= + +#include "extdll.h" +#include "defaults.h" +#include "utils.h" +#include "cbase.h" +#include "client.h" +#include "saverestore.h" +#include "player.h" + +//======================================================================= +// main functions () +//======================================================================= +TYPEDESCRIPTION CBaseMover::m_SaveData[] = +{ + DEFINE_FIELD( CBaseMover, m_flBlockedTime, FIELD_TIME ), + DEFINE_FIELD( CBaseMover, m_iMode, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMover, m_flMoveDistance, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flLip, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flHeight, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flValue, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_vecFinalDest, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecPosition1, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecPosition2, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecFloor, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMover, m_pfnCallWhenMoveDone, FIELD_FUNCTION ), + DEFINE_FIELD( CBaseMover, m_vecAngle1, FIELD_VECTOR ), + DEFINE_FIELD( CBaseMover, m_vecAngle2, FIELD_VECTOR ), + DEFINE_FIELD( CBaseMover, m_flLinearMoveSpeed, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_flAngularMoveSpeed, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMover, m_vecFinalAngle, FIELD_VECTOR ), + DEFINE_FIELD( CBaseMover, flTravelTime, FIELD_FLOAT ), +}; IMPLEMENT_SAVERESTORE( CBaseMover, CBaseBrush ); + +void CBaseMover :: AxisDir( void ) +{ + //make backward compatibility + if ( pev->movedir != g_vecZero) return; + if ( FBitSet(pev->spawnflags, 128)) + pev->movedir = Vector ( 0, 0, 1 ); // around z-axis + else if ( FBitSet(pev->spawnflags, 64)) + pev->movedir = Vector ( 1, 0, 0 ); // around x-axis + else pev->movedir = Vector ( 0, 1, 0 ); // around y-axis +} + +void CBaseMover::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "lip")) + { + m_flLip = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "waveheight")) + { + //field for volume_water + pev->scale = atof(pkvd->szValue) * (1.0/8.0); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "type")) + { + m_iMode = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "locksound")) + { + m_iStartSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "movesound")) + { + m_iMoveSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "stopsound")) + { + m_iStopSound = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "distance")) + { + m_flMoveDistance = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "height")) + { + m_flHeight = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "waveheight")) + { + //func_water wave height + pev->scale = atof(pkvd->szValue) * (1.0/8.0); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "contents")) + { + //func_water contents + pev->skin = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseBrush::KeyValue( pkvd ); +} + +//======================================================================= +// LinearMove +// +// calculate pev->velocity and pev->nextthink to reach vecDest from +// pev->origin traveling at flSpeed +//======================================================================= +void CBaseMover :: LinearMove( Vector vecInput, float flSpeed ) +{ + ASSERTSZ(flSpeed != 0, "LinearMove: no speed is defined!"); + + m_flLinearMoveSpeed = flSpeed; + m_vecFinalDest = vecInput; + + SetThink( LinearMoveNow ); + UTIL_SetThink( this ); +} + + +void CBaseMover :: LinearMoveNow( void ) +{ + Vector vecDest; + + if (m_pParent)vecDest = m_vecFinalDest + m_pParent->pev->origin; + else vecDest = m_vecFinalDest; + + // Already there? + if (vecDest == pev->origin) + { + LinearMoveDone(); + return; + } + + // set destdelta to the vector needed to move + Vector vecDestDelta = vecDest - pev->origin; + + // divide vector length by speed to get time to reach dest + flTravelTime = vecDestDelta.Length() / m_flLinearMoveSpeed; + + // set nextthink to trigger a call to LinearMoveDone when dest is reached + SetNextThink( flTravelTime ); + SetThink( LinearMoveDone ); + + UTIL_SetVelocity( this, vecDestDelta / flTravelTime ); +} + +//======================================================================= +// After moving, set origin to exact final destination, +// call "move done" function +//======================================================================= +void CBaseMover :: LinearMoveDone( void ) +{ + SetThink(LinearMoveDoneNow); + UTIL_SetThink( this ); +} + +void CBaseMover :: LinearMoveDoneNow( void ) +{ + UTIL_SetVelocity(this, g_vecZero); + if (m_pParent) UTIL_AssignOrigin(this, m_vecFinalDest + m_pParent->pev->origin); + else UTIL_AssignOrigin(this, m_vecFinalDest); + + DontThink(); + if( m_pfnCallWhenMoveDone )(this->*m_pfnCallWhenMoveDone)(); +} + +//======================================================================= +// AngularMove +// +// calculate pev->velocity and pev->nextthink to reach vecDest from +// pev->origin traveling at flSpeed +// Just like LinearMove, but rotational. +//======================================================================= +void CBaseMover :: AngularMove( Vector vecDestAngle, float flSpeed ) +{ + ASSERTSZ(flSpeed != 0, "AngularMove: no speed is defined!"); + + m_vecFinalAngle = vecDestAngle; + m_flAngularMoveSpeed = flSpeed; + + SetThink( AngularMoveNow ); + UTIL_SetThink( this ); +} + +void CBaseMover :: AngularMoveNow() +{ + Vector vecDestAngle; + + if (m_pParent) vecDestAngle = m_vecFinalAngle + m_pParent->pev->angles; + else vecDestAngle = m_vecFinalAngle; + + // Already there? + if (vecDestAngle == pev->angles) + { + AngularMoveDone(); + return; + } + + // set destdelta to the vector needed to move + Vector vecDestDelta = vecDestAngle - pev->angles; + + // divide by speed to get time to reach dest + flTravelTime = vecDestDelta.Length() / m_flAngularMoveSpeed; + + // set nextthink to trigger a call to AngularMoveDone when dest is reached + SetNextThink( flTravelTime ); + SetThink( AngularMoveDone ); + + // scale the destdelta vector by the time spent traveling to get velocity + UTIL_SetAvelocity(this, vecDestDelta / flTravelTime ); +} + +void CBaseMover :: AngularMoveDone( void ) +{ + SetThink( AngularMoveDoneNow ); + UTIL_SetThink( this ); +} + +//======================================================================= +// After rotating, set angle to exact final angle, call "move done" function +//======================================================================= +void CBaseMover :: AngularMoveDoneNow( void ) +{ + UTIL_SetAvelocity(this, g_vecZero); + if (m_pParent) UTIL_AssignAngles(this, m_vecFinalAngle + m_pParent->pev->angles); + else UTIL_AssignAngles(this, m_vecFinalAngle); + + DontThink(); + if ( m_pfnCallWhenMoveDone ) (this->*m_pfnCallWhenMoveDone)(); +} + +//======================================================================= +// ComplexMove +// +// combinate LinearMove and AngularMove +//======================================================================= +void CBaseMover :: ComplexMove( Vector vecInput, Vector vecDestAngle, float flSpeed ) +{ + ASSERTSZ(flSpeed != 0, "ComplexMove: no speed is defined!"); + + //set shared speed for moving and rotating + m_flLinearMoveSpeed = flSpeed; + m_flAngularMoveSpeed = flSpeed; + + //save local variables into global containers + m_vecFinalDest = vecInput; + m_vecFinalAngle = vecDestAngle; + + SetThink( ComplexMoveNow ); + UTIL_SetThink( this ); +} + +void CBaseMover :: ComplexMoveNow( void ) +{ + Vector vecDest, vecDestAngle; + + if (m_pParent)//calculate destination + { + vecDest = m_vecFinalDest + m_pParent->pev->origin; + vecDestAngle = m_vecFinalAngle + m_pParent->pev->angles; + } + else + { + vecDestAngle = m_vecFinalAngle; + vecDest = m_vecFinalDest; + } + + // Already there? + if (vecDest == pev->origin && vecDestAngle == pev->angles) + { + ComplexMoveDone(); + return; + } + + // Calculate TravelTime and final angles + Vector vecDestLDelta = vecDest - pev->origin; + Vector vecDestADelta = vecDestAngle - pev->angles; + + // divide vector length by speed to get time to reach dest + flTravelTime = vecDestLDelta.Length() / m_flLinearMoveSpeed; + + // set nextthink to trigger a call to LinearMoveDone when dest is reached + SetNextThink( flTravelTime ); + SetThink( ComplexMoveDone ); + + //set linear and angular velocity now + UTIL_SetVelocity( this, vecDestLDelta / flTravelTime ); + UTIL_SetAvelocity(this, vecDestADelta / flTravelTime ); +} + +void CBaseMover :: ComplexMoveDone( void ) +{ + SetThink(ComplexMoveDoneNow); + UTIL_SetThink( this ); +} + +void CBaseMover :: ComplexMoveDoneNow( void ) +{ + UTIL_SetVelocity(this, g_vecZero); + UTIL_SetAvelocity(this, g_vecZero); + + if (m_pParent) + { + UTIL_AssignOrigin(this, m_vecFinalDest + m_pParent->pev->origin); + UTIL_AssignAngles(this, m_vecFinalAngle + m_pParent->pev->angles); + } + else + { + UTIL_AssignOrigin(this, m_vecFinalDest); + UTIL_AssignAngles(this, m_vecFinalAngle); + } + + DontThink(); + if( m_pfnCallWhenMoveDone )(this->*m_pfnCallWhenMoveDone)(); +} + +//======================================================================= +// func_door - classic QUAKE door +//======================================================================= +void CBaseDoor::Spawn( ) +{ + Precache(); + if(!IsRotatingDoor())UTIL_LinearVector( this ); + CBaseBrush::Spawn(); + + if(pev->spawnflags & SF_NOTSOLID)//make illusionary door + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + + if(IsRotatingDoor()) + { + // check for clockwise rotation + if ( m_flMoveDistance < 0 ) pev->movedir = pev->movedir * -1; + + AxisDir(); + m_vecAngle1 = pev->angles; + m_vecAngle2 = pev->angles + pev->movedir * m_flMoveDistance; + + ASSERTSZ(m_vecAngle1 != m_vecAngle2, "rotating door start/end positions are equal"); + + SetBits (pFlags, PF_ANGULAR); + } + + UTIL_SetModel( ENT(pev), pev->model ); + UTIL_SetOrigin(this, pev->origin); + + //determine work style + if(m_iMode == 0)//normal door - only USE + { + SetUse ( DoorUse ); + SetTouch ( NULL ); + pev->team = 1;//info_node identifier. Do not edit!!! + } + if(m_iMode == 1)//classic QUAKE & HL door - only TOUCH + { + SetUse ( ShowInfo );//show info only + SetTouch ( DoorTouch ); + } + if(m_iMode == 2)//combo door - USE and TOUCH + { + SetUse ( DoorUse ); + SetTouch ( DoorTouch ); + } + + //as default any door is toggleable, but if mapmaker set waittime > 0 + //door will de transformed into timebased door + //if waittime is -1 - button forever stay pressed + if(m_flWait == 0) pev->impulse = 1;//toggleable door + if (m_flLip == 0) m_flLip = 4;//standart offset from Quake1 + + if (pev->speed == 0) pev->speed = 100;//default speed + m_iState = STATE_OFF; +} + +void CBaseDoor :: PostSpawn( void ) +{ + if (m_pParent) m_vecPosition1 = pev->origin - m_pParent->pev->origin; + else m_vecPosition1 = pev->origin; + + // Subtract 2 from size because the engine expands bboxes by 1 in all directions + m_vecPosition2 = m_vecPosition1 + (pev->movedir * (fabs( pev->movedir.x * (pev->size.x-2) ) + fabs( pev->movedir.y * (pev->size.y-2) ) + fabs( pev->movedir.z * (pev->size.z-2) ) - m_flLip)); + + ASSERTSZ(m_vecPosition1 != m_vecPosition2, "door start/end positions are equal"); + if ( FBitSet (pev->spawnflags, SF_START_ON) ) + { + if (m_pParent) + { + m_vecSpawnOffset = m_vecSpawnOffset + (m_vecPosition2 + m_pParent->pev->origin) - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2 + m_pParent->pev->origin); + } + else + { + m_vecSpawnOffset = m_vecSpawnOffset + m_vecPosition2 - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2); + } + Vector vecTemp = m_vecPosition2; + m_vecPosition2 = m_vecPosition1; + m_vecPosition1 = vecTemp; + } +} + +void CBaseDoor :: SetToggleState( int state ) +{ + if ( m_iState == STATE_ON ) + { + if (m_pParent) UTIL_AssignOrigin( this, m_vecPosition2 + m_pParent->pev->origin); + else UTIL_AssignOrigin( this, m_vecPosition2 ); + } + else + { + if (m_pParent) UTIL_AssignOrigin( this, m_vecPosition1 + m_pParent->pev->origin); + else UTIL_AssignOrigin( this, m_vecPosition1 ); + } +} + +void CBaseDoor::Precache( void ) +{ + CBaseBrush::Precache();//precache damage sound + + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + switch (m_sounds)//load movesound sounds (sound will play when door is moving) + { + case 1: pev->noise1 = UTIL_PrecacheSound ("doors/doormove1.wav");break; + case 2: pev->noise1 = UTIL_PrecacheSound ("doors/doormove2.wav");break; + case 3: pev->noise1 = UTIL_PrecacheSound ("doors/doormove3.wav");break; + case 4: pev->noise1 = UTIL_PrecacheSound ("doors/doormove4.wav");break; + case 5: pev->noise1 = UTIL_PrecacheSound ("doors/doormove5.wav");break; + case 6: pev->noise1 = UTIL_PrecacheSound ("doors/doormove6.wav");break; + case 7: pev->noise1 = UTIL_PrecacheSound ("doors/doormove7.wav");break; + case 8: pev->noise1 = UTIL_PrecacheSound ("doors/doormove8.wav");break; + case 9: pev->noise1 = UTIL_PrecacheSound ("doors/doormove9.wav");break; + case 10: pev->noise1 = UTIL_PrecacheSound ("doors/doormove10.wav");break; + case 0: pev->noise1 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise1 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop1.wav");break; + case 2: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop2.wav");break; + case 3: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop3.wav");break; + case 4: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop4.wav");break; + case 5: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop5.wav");break; + case 6: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop6.wav");break; + case 7: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop7.wav");break; + case 8: pev->noise2 = UTIL_PrecacheSound ("doors/doorstop8.wav");break; + case 0: pev->noise2 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise2 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + if (!FStringNull(m_sMaster))//door has master + { + m_sounds = UTIL_LoadSoundPreset(m_iStartSound); + switch (m_sounds)//load locked sounds + { + case 1: pev->noise3 = UTIL_PrecacheSound ("!NA"); break; + case 2: pev->noise3 = UTIL_PrecacheSound ("!ND"); break; + case 3: pev->noise3 = UTIL_PrecacheSound ("!NF"); break; + case 4: pev->noise3 = UTIL_PrecacheSound ("!NFIRE"); break; + case 5: pev->noise3 = UTIL_PrecacheSound ("!NCHEM"); break; + case 6: pev->noise3 = UTIL_PrecacheSound ("!NRAD"); break; + case 7: pev->noise3 = UTIL_PrecacheSound ("!NCON"); break; + case 8: pev->noise3 = UTIL_PrecacheSound ("!NH"); break; + case 9: pev->noise3 = UTIL_PrecacheSound ("!NG"); break; + case 0: pev->noise3 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise3 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + } +} + +void CBaseDoor::DoorTouch( CBaseEntity *pOther ) +{ + //make delay before retouching + if ( gpGlobals->time < pev->dmgtime) return; + pev->dmgtime = gpGlobals->time + 1.0; + m_hActivator = pOther;// remember who activated the door + + if(pOther->IsPlayer())DoorUse ( pOther, this, USE_TOGGLE, 1 );//player always sending 1 +} + +void CBaseDoor::DoorUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + if(IsLockedByMaster( useType ))//passed only USE_SHOWINFO + { + EMIT_SOUND(ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise3), 1, ATTN_NORM); + return; + } + if(useType == USE_SHOWINFO)//show info + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, Speed %.2f\n", GetStringForState( GetState()), pev->speed ); + ALERT(at_console, "Texture frame: %.f. WaitTime: %.2f\n", pev->frame, m_flWait); + } + else if(m_iState != STATE_DEAD)//activate door + { + //NOTE: STATE_DEAD is better method for simulate m_flWait -1 without fucking SetThink() + if (m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF )return;//door in-moving + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if(useType == USE_ON) + { + if( m_iState == STATE_OFF )DoorGoUp(); + } + else if( useType == USE_OFF ) + { + if(m_iState == STATE_ON && pev->impulse) DoorGoDown(); + } + else if( useType == USE_SET ) + { + if(value) + { + m_flWait = value; + pev->impulse = 0; + } + } + else if( useType == USE_RESET ) + { + m_flWait = 0; + pev->impulse = 1; + } + } +} + +void CBaseDoor :: ShowInfo ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(useType == USE_SHOWINFO)//show info + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, Speed %.2f\n", GetStringForState( GetState()), pev->speed ); + ALERT(at_console, "Texture frame: %.f. WaitTime: %.2f\n", pev->frame, m_flWait); + } +} + +void CBaseDoor::DoorGoUp( void ) +{ + // It could be going-down, if blocked. + ASSERT(m_iState == STATE_OFF || m_iState == STATE_TURN_OFF); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1), 1, ATTN_NORM); + m_iState = STATE_TURN_ON; + SetMoveDone( DoorHitTop ); + + UTIL_FireTargets( pev->target, m_hActivator, this, USE_ON ); + if(IsRotatingDoor()) AngularMove(m_vecAngle2, pev->speed); + else LinearMove(m_vecPosition2, pev->speed); +} + +void CBaseDoor::DoorHitTop( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1) ); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise2), 1, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_ON); + m_iState = STATE_ON; + + if(m_flWait == -1) + { + m_iState = STATE_DEAD;//keep door in this position + return; + } + + if(pev->impulse == 0)//time base door + { + SetThink( DoorGoDown ); + SetNextThink( m_flWait ); + } + + // Fire the close target (if startopen is set, then "top" is closed) + if (pev->spawnflags & SF_START_ON) + UTIL_FireTargets( pev->target, m_hActivator, this, USE_OFF ); + else UTIL_FireTargets( pev->target, m_hActivator, this, USE_ON ); +} + +void CBaseDoor::DoorGoDown( void ) +{ + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1), 1, ATTN_NORM); + + ASSERT(m_iState == STATE_ON); + m_iState = STATE_TURN_OFF; + SetMoveDone( DoorHitBottom ); + if(IsRotatingDoor())AngularMove( m_vecAngle1, pev->speed); + else LinearMove(m_vecPosition1, pev->speed); +} + +void CBaseDoor::DoorHitBottom( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1) ); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise2), 1, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_OFF); + m_iState = STATE_OFF; + + if (pev->spawnflags & SF_START_ON) + UTIL_FireTargets( pev->target, m_hActivator, this, USE_ON ); + else UTIL_FireTargets( pev->target, m_hActivator, this, USE_OFF ); + + UTIL_FireTargets( pev->target, m_hActivator, this, USE_ON ); +} + +void CBaseDoor::Blocked( CBaseEntity *pOther ) +{ + CBaseEntity *pTarget = NULL; + CBaseDoor *pDoor = NULL; + + UTIL_AssignOrigin(this, pev->origin); + //make delay before retouching + if ( gpGlobals->time < m_flBlockedTime) return; + m_flBlockedTime = gpGlobals->time + 0.5; + + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE) m_pParent->Blocked( pOther); + if ( pev->dmg ) pOther->TakeDamage( pev, pev, pev->dmg, DMG_CRUSH ); + + if (m_flWait >= 0) + { + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise1) ); + if(IsRotatingDoor()) + { + if (m_iState == STATE_TURN_ON) DoorGoUp(); + else if (m_iState == STATE_TURN_OFF) DoorGoDown(); + } + else + { + if (m_iState == STATE_TURN_ON) DoorGoDown(); + else if (m_iState == STATE_TURN_OFF) DoorGoUp(); + } + } + SetNextThink( 0 ); +} +LINK_ENTITY_TO_CLASS( func_door, CBaseDoor ); + +//======================================================================= +// func_door_rotating - classic rotating door +//======================================================================= +void CRotDoor::Spawn( void ) +{ + CBaseDoor::Spawn(); + + if ( FBitSet (pev->spawnflags, SF_START_ON) ) + { + pev->angles = m_vecAngle2; + Vector vecSav = m_vecAngle1; + m_vecAngle2 = m_vecAngle1; + m_vecAngle1 = vecSav; + pev->movedir = pev->movedir * -1; + } +} + +void CRotDoor :: SetToggleState( int state ) +{ + if ( state == STATE_ON ) pev->angles = m_vecAngle2; + else pev->angles = m_vecAngle1; + UTIL_SetOrigin( this, pev->origin ); +} +LINK_ENTITY_TO_CLASS( func_door_rotating, CRotDoor ); + +//======================================================================= +// func_momentary_door +//======================================================================= +void CMomentaryDoor::Precache( void ) +{ + CBaseBrush::Precache(); + + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove1.wav");break; + case 2: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove2.wav");break; + case 3: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove3.wav");break; + case 4: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove4.wav");break; + case 5: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove5.wav");break; + case 6: pev->noise = UTIL_PrecacheSound ("materials/doors/doormove6.wav");break; + case 0: pev->noise = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop1.wav");break; + case 2: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop2.wav");break; + case 3: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop3.wav");break; + case 4: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop4.wav");break; + case 5: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop5.wav");break; + case 6: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop6.wav");break; + case 7: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop7.wav");break; + case 8: pev->noise2 = UTIL_PrecacheSound ("materials/doors/doorstop8.wav");break; + case 0: pev->noise2 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise2 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } +} + +void CMomentaryDoor::Spawn( void ) +{ + Precache(); + CBaseBrush::Spawn(); + UTIL_LinearVector( this );//movement direction + + if(pev->spawnflags & SF_NOTSOLID)pev->solid = SOLID_NOT; //make illusionary wall + else pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + + m_iState = STATE_OFF; + UTIL_SetOrigin(this, pev->origin); + UTIL_SetModel( ENT(pev), pev->model ); + SetTouch( NULL );//just in case +} + +void CMomentaryDoor::PostSpawn( void ) +{ + if (m_pParent) m_vecPosition1 = pev->origin - m_pParent->pev->origin; + else m_vecPosition1 = pev->origin; + + // Subtract 2 from size because the engine expands bboxes by 1 in all directions making the size too big + m_vecPosition2 = m_vecPosition1 + (pev->movedir * (fabs( pev->movedir.x * (pev->size.x-2) ) + fabs( pev->movedir.y * (pev->size.y-2) ) + fabs( pev->movedir.z * (pev->size.z-2) ) - m_flLip)); + ASSERTSZ(m_vecPosition1 != m_vecPosition2, "door start/end positions are equal"); + + if(pev->spawnflags & SF_START_ON) + { + if (m_pParent) + { + m_vecSpawnOffset = m_vecSpawnOffset + (m_vecPosition2 + m_pParent->pev->origin) - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2 + m_pParent->pev->origin); + } + else + { + m_vecSpawnOffset = m_vecSpawnOffset + m_vecPosition2 - pev->origin; + UTIL_AssignOrigin(this, m_vecPosition2); + } + Vector vecTemp = m_vecPosition2; + m_vecPosition2 = m_vecPosition1; + m_vecPosition1 = vecTemp; + } +} + +void CMomentaryDoor::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_ON) value = 1; + else if(useType == USE_OFF) value = 0; + else if(useType == USE_SET); + else if(useType == USE_SHOWINFO)//show info + { + Msg("======/Xash Debug System/======\n"); + Msg("classname: %s\n", STRING(pev->classname)); + Msg("State: %s, Lip %.2f\n", GetStringForState( GetState()), m_flLip ); + SHIFT; + } + else return; + + if ( value > 1.0 )value = 1.0; + + if (IsLockedByMaster()) return; + Vector move = m_vecPosition1 + (value * (m_vecPosition2 - m_vecPosition1)); + + float speed = 0; + Vector delta; + + if (pev->speed) speed = pev->speed; + else + { + // default: get there in 0.1 secs + delta = move - pev->origin; + speed = delta.Length() * 10; + } + + //FIXME: allow for it being told to move at the same speed in the _opposite_ direction! + if ( speed != 0 ) + { + // This entity only thinks when it moves + if ( m_iState == STATE_OFF ) + { + //ALERT(at_console,"USE: start moving to %f %f %f.\n", move.x, move.y, move.z); + m_iState = STATE_ON; + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), 1, ATTN_NORM); + } + + LinearMove( move, speed ); + SetMoveDone( MomentaryMoveDone ); + } +} + +void CMomentaryDoor::MomentaryMoveDone( void ) +{ + m_iState = STATE_OFF; + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise2), 1, ATTN_NORM); +} +LINK_ENTITY_TO_CLASS( func_momentary_door, CMomentaryDoor ); + +//======================================================================= +// func_platform (a lift\elevator) +//======================================================================= +void CBasePlatform::Precache( void ) +{ + CBaseBrush::Precache();//precache damage sound + + int m_sounds = UTIL_LoadSoundPreset(m_iMoveSound); + switch (m_sounds)//load movesound sounds (sound will play when door is moving) + { + case 1: pev->noise = UTIL_PrecacheSound ("plats/bigmove1.wav");break; + case 2: pev->noise = UTIL_PrecacheSound ("plats/bigmove2.wav");break; + case 3: pev->noise = UTIL_PrecacheSound ("plats/elevmove1.wav");break; + case 4: pev->noise = UTIL_PrecacheSound ("plats/elevmove2.wav");break; + case 5: pev->noise = UTIL_PrecacheSound ("plats/elevmove3.wav");break; + case 6: pev->noise = UTIL_PrecacheSound ("plats/freightmove1.wav");break; + case 7: pev->noise = UTIL_PrecacheSound ("plats/freightmove2.wav");break; + case 8: pev->noise = UTIL_PrecacheSound ("plats/heavymove1.wav");break; + case 9: pev->noise = UTIL_PrecacheSound ("plats/rackmove1.wav");break; + case 10: pev->noise = UTIL_PrecacheSound ("plats/railmove1.wav");break; + case 11: pev->noise = UTIL_PrecacheSound ("plats/squeekmove1.wav");break; + case 12: pev->noise = UTIL_PrecacheSound ("plats/talkmove1.wav");break; + case 13: pev->noise = UTIL_PrecacheSound ("plats/talkmove2.wav");break; + case 0: pev->noise = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + m_sounds = UTIL_LoadSoundPreset(m_iStopSound); + switch (m_sounds)//load pushed sounds (sound will play at activate or pushed button) + { + case 1: pev->noise1 = UTIL_PrecacheSound ("plats/bigstop1.wav");break; + case 2: pev->noise1 = UTIL_PrecacheSound ("plats/bigstop2.wav");break; + case 3: pev->noise1 = UTIL_PrecacheSound ("plats/freightstop1.wav");break; + case 4: pev->noise1 = UTIL_PrecacheSound ("plats/heavystop2.wav");break; + case 5: pev->noise1 = UTIL_PrecacheSound ("plats/rackstop1.wav");break; + case 6: pev->noise1 = UTIL_PrecacheSound ("plats/railstop1.wav");break; + case 7: pev->noise1 = UTIL_PrecacheSound ("plats/squeekstop1.wav");break; + case 8: pev->noise1 = UTIL_PrecacheSound ("plats/talkstop1.wav");break; + case 0: pev->noise1 = UTIL_PrecacheSound ("common/null.wav"); break; + default: pev->noise1 = UTIL_PrecacheSound(m_sounds); break;//custom sound or sentence + } + + UTIL_PrecacheSound( "buttons/button11.wav" );//error sound +} + +void CBasePlatform :: Setup( void ) +{ + Precache(); //precache moving & stop sounds + + pev->angles = g_vecZero; + + if (IsWater()) pev->solid = SOLID_NOT; // special contents (water, slime, e.t.c. ) + else pev->solid = SOLID_BSP; + + pev->movetype = MOVETYPE_PUSH; + UTIL_SetOrigin(this, pev->origin); + UTIL_SetSize(pev, pev->mins, pev->maxs); + UTIL_SetModel(ENT(pev), pev->model ); + // vecPosition1 is the top position, vecPosition2 is the bottom + if (m_pParent) m_vecPosition1 = pev->origin - m_pParent->pev->origin; + else m_vecPosition1 = pev->origin; + m_vecPosition2 = m_vecPosition1; + + if(IsMovingPlatform() || IsComplexPlatform()) + { + m_vecPosition2.z = m_vecPosition2.z + step(); + ASSERTSZ(m_vecPosition1 != m_vecPosition2, "moving platform start/end positions are equal\n"); + } + if(IsRotatingPlatform() || IsComplexPlatform()) + { + if ( m_flMoveDistance < 0 ) pev->movedir = pev->movedir * -1; + + AxisDir(); + m_vecAngle1 = pev->angles; + m_vecAngle2 = pev->angles + pev->movedir * m_flMoveDistance; + + ASSERTSZ(m_vecAngle1 != m_vecAngle2, "rotating platform start/end positions are equal\n"); + SetBits (pFlags, PF_ANGULAR); + } + if (!IsWater())CBaseBrush::Spawn(); + + if (pev->speed == 0) pev->speed = 150; + m_iState = STATE_OFF; + +} + +void CBasePlatform :: StartMessage( CBasePlayer *pPlayer ) +{ + if(IsWater()) + { + MESSAGE_BEGIN( MSG_ONE, gmsgAddMirror, NULL, pPlayer->pev ); + WRITE_SHORT( entindex() ); + MESSAGE_END(); + pev->body = 1; //enable water + } +} + +void CBasePlatform :: PostSpawn( void ) +{ + if ( FBitSet( pev->spawnflags, SF_START_ON ) ) + { + if (m_pParent) UTIL_AssignOrigin (this, m_vecPosition2 + m_pParent->pev->origin); + else UTIL_AssignOrigin (this, m_vecPosition2); + UTIL_AssignAngles(this, m_vecAngle2); + m_iState = STATE_ON; + } + else + { + if (m_pParent) UTIL_AssignOrigin (this, m_vecPosition1 + m_pParent->pev->origin); + else UTIL_AssignOrigin (this, m_vecPosition1); + UTIL_AssignAngles(this, m_vecAngle1); + m_iState = STATE_OFF; + } +} + +void CBasePlatform :: Spawn( void ) +{ + Setup(); +} + +void CBasePlatform :: PostActivate( void ) +{ + if(m_iState == STATE_TURN_OFF || m_iState == STATE_TURN_ON)//platform "in-moving" ? restore sound! + { + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + } +} + +void CBasePlatform :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + GoUp(); + } + else if ( useType == USE_OFF ) + { + GoDown(); + } + else if ( useType == USE_SET ) + { + GoToFloor( value ); + } + else if ( useType == USE_RESET ) + { + GoToFloor( 1 ); + } + else if (useType == USE_SHOWINFO) + { + Msg( "======/Xash Debug System/======\n"); + Msg( "classname: %s\n", STRING(pev->classname)); + if(IsWater())Msg( "Contents: %s, WaveHeight %g\n", GetContentsString( pev->skin ), pev->scale); + else Msg( "State: %s, floor %g\n", GetStringForState( GetState()), CalcFloor()); + Msg( "distance %g, speed %g\n", m_flMoveDistance, pev->speed); + + } +} + +void CBasePlatform :: GoToFloor( float floor ) +{ + float curfloor = CalcFloor(); + m_flValue = floor; + + if(curfloor <= 0) return; + if(curfloor == floor) //already there? + { + //pass trough + UTIL_FireTargets( pev->target, m_hActivator, this, USE_RESET, m_flValue ); + return; + } + + m_vecFloor = m_vecPosition1; + m_vecFloor.z = pev->origin.z + (floor * step()) - (curfloor * step()); + + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + + if(floor > curfloor)m_iState = STATE_TURN_ON; + else m_iState = STATE_TURN_OFF; + + SetMoveDone( HitFloor ); + + if(fabs(floor - curfloor) > 1.0) //create floor informator for prop_counter + { + CBaseEntity *pFloor = CBaseEntity::Create( "floorent", pev->origin, g_vecZero, edict() ); + pFloor->pev->target = pev->netname; + pFloor->PostActivate(); + } + LinearMove(m_vecFloor, pev->speed); +} + +void CBasePlatform :: HitFloor( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF); + UTIL_FireTargets( pev->target, m_hActivator, this, USE_TOGGLE, m_flValue ); + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_SET, m_flValue ); + m_vecPosition2 = pev->origin; //save current floor + if(m_iState == STATE_TURN_ON) m_iState = STATE_ON; + if(m_iState == STATE_TURN_OFF) m_iState = STATE_OFF; +} + +void CBasePlatform :: GoDown( void ) +{ + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_ON || m_iState == STATE_TURN_ON); + m_iState = STATE_TURN_OFF; + SetMoveDone( HitBottom ); + + if(IsRotatingPlatform()) AngularMove( m_vecAngle1, pev->speed ); + else if(IsMovingPlatform()) LinearMove( m_vecPosition1, pev->speed ); + else if(IsComplexPlatform())ComplexMove( m_vecPosition1, m_vecAngle1, pev->speed ); + else HitBottom();//don't brake platform status +} + +void CBasePlatform :: HitBottom( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_OFF); + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_SET, 1 ); + m_iState = STATE_OFF; +} + +void CBasePlatform :: GoUp( void ) +{ + EMIT_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_OFF || m_iState == STATE_TURN_OFF); + m_iState = STATE_TURN_ON; + SetMoveDone( HitTop ); + + if(IsRotatingPlatform()) AngularMove( m_vecAngle2, pev->speed ); + else if(IsMovingPlatform()) LinearMove( m_vecPosition2, pev->speed ); + else if(IsComplexPlatform())ComplexMove( m_vecPosition2, m_vecAngle2, pev->speed ); + else HitTop();//don't brake platform status +} + +void CBasePlatform :: HitTop( void ) +{ + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + ASSERT(m_iState == STATE_TURN_ON); + UTIL_FireTargets( pev->netname, m_hActivator, this, USE_SET, 2 ); + m_iState = STATE_ON; +} + +void CBasePlatform :: Blocked( CBaseEntity *pOther ) +{ + UTIL_AssignOrigin(this, pev->origin); + //make delay before retouching + if ( gpGlobals->time < m_flBlockedTime) return; + m_flBlockedTime = gpGlobals->time + 0.5; + + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE) m_pParent->Blocked( pOther); + pOther->TakeDamage( pev, pev, 1, DMG_CRUSH ); + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + ASSERT(m_iState == STATE_TURN_ON || m_iState == STATE_TURN_OFF); + + if (m_iState == STATE_TURN_ON) GoDown(); + else if (m_iState == STATE_TURN_OFF) GoUp(); + SetNextThink( 0 ); +} + +LINK_ENTITY_TO_CLASS( func_water, CBasePlatform ); +LINK_ENTITY_TO_CLASS( func_platform, CBasePlatform ); +LINK_ENTITY_TO_CLASS( func_platform_rotating, CBasePlatform ); + + +//======================================================================= +// func_train (Classic QUAKE Train) +//======================================================================= +TYPEDESCRIPTION CFuncTrain::m_SaveData[] = +{ + DEFINE_FIELD( CFuncTrain, pCurPath, FIELD_CLASSPTR ), + DEFINE_FIELD( CFuncTrain, pNextPath, FIELD_CLASSPTR ), +}; +IMPLEMENT_SAVERESTORE( CFuncTrain, CBasePlatform ); + +void CFuncTrain :: Spawn( void ) +{ + Precache(); //precache moving & stop sounds + + if(pev->spawnflags & SF_NOTSOLID)//make illusionary train + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + } + else + { + pev->solid = SOLID_BSP; + pev->movetype = MOVETYPE_PUSH; + } + + UTIL_SetOrigin(this, pev->origin); + UTIL_SetSize(pev, pev->mins, pev->maxs); + UTIL_SetModel(ENT(pev), pev->model ); + CBaseBrush::Spawn(); + + //determine method for calculating origin + if(pev->origin != g_vecZero) pev->impulse = 1; + + if (pev->speed == 0) pev->speed = 100; + m_iState = STATE_OFF; +} + +void CFuncTrain :: PostSpawn( void ) +{ + if (!FindPath()) return; + + if (pev->impulse) + { + m_vecSpawnOffset = m_vecSpawnOffset + pCurPath->pev->origin - pev->origin; + if (m_pParent) UTIL_AssignOrigin (this, pCurPath->pev->origin - m_pParent->pev->origin ); + else UTIL_AssignOrigin (this, pCurPath->pev->origin ); + } + else + { + m_vecSpawnOffset = m_vecSpawnOffset + (pCurPath->pev->origin - TrainOrg()) - pev->origin; + if (m_pParent) UTIL_AssignOrigin (this, pCurPath->pev->origin - TrainOrg() - m_pParent->pev->origin ); + else UTIL_AssignOrigin (this, pCurPath->pev->origin - TrainOrg()); + } + pev->target = pCurPath->pev->target; +} + +void CFuncTrain :: PostActivate( void ) +{ + if(m_iState == STATE_ON)//platform "in-moving" ? restore sound! + { + EMIT_SOUND (ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + } + if ( pev->spawnflags & SF_START_ON ) + { + m_iState = STATE_OFF; //restore sound on a next level + SetThink( Next ); //evil stuff... + SetNextThink( 0.1 ); + ClearBits( pev->spawnflags, SF_START_ON );//fire once + } +} + +void CFuncTrain::ClearPointers( void ) +{ + CBaseEntity::ClearPointers(); + pCurPath = NULL; + pNextPath = NULL; +} + +void CFuncTrain::OverrideReset( void ) +{ + // Are we moving? + if ( m_iState == STATE_ON ) + { + pev->target = pev->message; + Msg("Override target %s\n", STRING( pev->target )); + if (FindPath()) SetBits( pev->spawnflags, SF_START_ON );//PostActivate member + else Stop(); + } +} + +CBaseEntity *CFuncTrain::FindPath( void ) +{ + //find start track + Msg("Find corner %s\n", STRING( pev->target )); + pCurPath = UTIL_FindEntityByTargetname (NULL, STRING(pev->target)); + if(pCurPath && pCurPath->edict()) + { + return pCurPath; + } + return NULL; +} + +CBaseEntity *CFuncTrain::FindNextPath( void ) +{ + //safe way to check patch + if(!pCurPath) return FALSE; + + // get pointer to next target + if(pev->speed > 0)pNextPath = ((CPathCorner *)pCurPath)->GetNext(); + if(pev->speed < 0)pNextPath = ((CPathCorner *)pCurPath)->GetPrev(); + + if(pNextPath && pNextPath->edict()) //validate path + { + //record new value (this will be used after changelevel) + //pev->target = pNextPath->pev->targetname; + + //pCurPath = pNextPath; + Msg("CFuncTrain::FindNextPath: pCurPath %s pNextPath %s\n", STRING( pCurPath->pev->targetname ), STRING( pNextPath->pev->targetname )); + return pNextPath; //path found + } + + Stop();//stopping platform + return NULL; +} + +void CFuncTrain :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) Next(); + else if ( useType == USE_OFF ) Stop(); + else if (useType == USE_SET) Reverse();//reverse train + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + ALERT(at_console, "State: %s, speed %g\n", GetStringForState( GetState()), pev->speed ); + if(GetPrev() && GetPrev()->edict()) Msg("Prev path %s", STRING(GetPrev()->pev->targetname) ); + if(GetNext() && GetNext()->edict()) Msg("Next path %s", STRING(GetNext()->pev->targetname) ); + SHIFT; + } +} + +void CFuncTrain :: Next( void ) +{ + CBaseEntity *pTarg = FindPath(); + + if(!pTarg) return; + + //linear move to next corner. + if(m_iState == STATE_OFF)//enable train sound + { + STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noise) ); + EMIT_SOUND (ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise), m_flVolume, ATTN_NORM); + } + + ClearBits(pev->effects, EF_NOINTERP); //enable interpolation + m_iState = STATE_ON; + //pev->message = ((CPathCorner *)pCurPath)->GetNext()->pev->targetname; + pev->message = pev->target; + pev->target = pTarg->pev->target; + + /*if( pev->speed < 0 && FBitSet(pNextPath->pev->spawnflags, SF_CORNER_TELEPORT)) + { + SetBits(pev->effects, EF_NOINTERP); + if (m_pParent) UTIL_AssignOrigin(this, pNextPath->pev->origin - m_pParent->pev->origin ); + else UTIL_AssignOrigin(this, pNextPath->pev->origin ); + Wait(); // Get on with doing the next path corner. + return; + }*/ + + if (m_pParent) + { + if (pev->impulse) LinearMove( pTarg->pev->origin - m_pParent->pev->origin, fabs( pev->speed)); + else LinearMove (pTarg->pev->origin - TrainOrg() - m_pParent->pev->origin, fabs(pev->speed)); + } + else + { + if (pev->impulse) LinearMove( pTarg->pev->origin, fabs(pev->speed) ); + else LinearMove (pTarg->pev->origin - TrainOrg(), fabs(pev->speed) ); + } + pCurPath = pTarg; + SetMoveDone( Wait ); +} + +void CFuncTrain :: Wait( void ) +{ + UTIL_FireTargets(pev->netname, this, this, USE_TOGGLE ); + ((CPathCorner *)pCurPath)->UpdateTargets(); + ((CPathCorner *)pCurPath)->GetSpeed( &pev->speed ); + if(!Stop(((CPathCorner *)pCurPath)->GetDelay())) Next();// go to next corner +} + +BOOL CFuncTrain :: Teleport( void ) +{ + if( FBitSet(pNextPath->pev->spawnflags, SF_CORNER_TELEPORT)) + { + SetBits(pev->effects, EF_NOINTERP); + + //determine teleportation point + if(pev->speed > 0) + { + pNextPath = pNextPath->GetNext(); + ((CPathCorner *)pCurPath)->UpdateTargets(); + UTIL_FireTargets(pev->netname, this, this, USE_TOGGLE ); + } + if (m_pParent) UTIL_AssignOrigin(this, pNextPath->pev->origin - m_pParent->pev->origin ); + else UTIL_AssignOrigin(this, pNextPath->pev->origin ); + + pCurPath = pNextPath; + Next(); // Get on with doing the next path corner. + return TRUE; + } + return FALSE; +} + +BOOL CFuncTrain :: Stop( float flWait ) +{ + if( flWait == 0 ) return FALSE; + m_iState = STATE_OFF; + + // clear the sound channel. + STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noise) ); + EMIT_SOUND (ENT(pev), CHAN_VOICE, (char*)STRING(pev->noise1), m_flVolume, ATTN_NORM); + + UTIL_SetVelocity(this, g_vecZero); + UTIL_SetAvelocity(this, g_vecZero); + if( flWait > 0) + { + SetNextThink( flWait ); + SetThink( Next ); + } + + //wait for retrigger + if(flWait == -1) DontThink(); + return TRUE; +} + +void CFuncTrain::Reverse( void ) +{ + //update path if dir changed + if(pev->speed != 0 && pNextPath && pNextPath->edict()) pCurPath = pNextPath; + pev->speed = -pev->speed; + if(m_iState == STATE_ON) Next(); //reverse now! +} + +void CFuncTrain :: Blocked( CBaseEntity *pOther ) +{ + // Keep "movewith" entities in line + UTIL_AssignOrigin(this, pev->origin); + + if ( gpGlobals->time < m_flBlockedTime) return; + m_flBlockedTime = gpGlobals->time + 0.5; + + if(m_pParent && m_pParent->edict() && pFlags & PF_PARENTMOVE) m_pParent->Blocked( pOther); + pOther->TakeDamage( pev, pev, 1, DMG_CRUSH ); + STOP_SOUND(ENT(pev), CHAN_STATIC, (char*)STRING(pev->noise)); + ASSERT(m_iState == STATE_ON); + + Stop( 0.5 ); +} +LINK_ENTITY_TO_CLASS( func_train, CFuncTrain ); \ No newline at end of file diff --git a/server/ents/basemover.h b/server/ents/basemover.h new file mode 100644 index 00000000..ff2e4c7d --- /dev/null +++ b/server/ents/basemover.h @@ -0,0 +1,252 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= +#ifndef BASEMOVER_H +#define BASEMOVER_H + +//rotating brush flags +#define SF_BRUSH_ROTATE_Z_AXIS 4 +#define SF_BRUSH_ROTATE_X_AXIS 8 + +//door flags +#define SF_DOOR_START_OPEN 1 +#define SF_DOOR_ROTATE_BACKWARDS 2 +#define SF_DOOR_PASSABLE 8 +#define SF_DOOR_ONEWAY 16 +#define SF_DOOR_NO_AUTO_RETURN 32 +#define SF_DOOR_ROTATE_Z 64 +#define SF_DOOR_ROTATE_X 128 +#define SF_DOOR_USE_ONLY 256 +#define SF_DOOR_NOMONSTERS 512 + +// Tracktrain spawn flags +#define SF_TRACKTRAIN_NOPITCH 0x0001 +#define SF_TRACKTRAIN_NOCONTROL 0x0002 +#define SF_TRACKTRAIN_FORWARDONLY 0x0004 +#define SF_TRACKTRAIN_PASSABLE 0x0008 +#define SF_TRACKTRAIN_NOYAW 0x0010 //LRC +#define SF_TRACKTRAIN_AVELOCITY 0x800000 //LRC - avelocity has been set manually, don't turn. +#define SF_TRACKTRAIN_AVEL_GEARS 0x400000 //LRC - avelocity should be scaled up/down when the train changes gear. + +// Spawnflag for CPathTrack +#define SF_PATH_DISABLED 0x00000001 +#define SF_PATH_FIREONCE 0x00000002 +#define SF_PATH_ALTREVERSE 0x00000004 +#define SF_PATH_DISABLE_TRAIN 0x00000008 +#define SF_PATH_ALTERNATE 0x00008000 +#define SF_PATH_AVELOCITY 0x00080000 //LRC + +//LRC - values in 'armortype' +#define PATHSPEED_SET 0 +#define PATHSPEED_ACCEL 1 +#define PATHSPEED_TIME 2 +#define PATHSPEED_SET_MASTER 3 + +class CBaseMover : public CBaseBrush +{ +public: + void KeyValue( KeyValueData *pkvd ); + virtual void AxisDir( void ); + void (CBaseMover::*m_pfnCallWhenMoveDone)(void); //custom movedone function + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + float m_flMoveDistance;//rotating distance + float m_flBlockedTime; //set blocked refresh time + int m_iMode;//style of working + float m_flValue;//value to send + float m_flHeight; + float m_flLip; + + Vector m_vecPosition1; //startpos + Vector m_vecPosition2; //endpos + Vector m_vecAngle1; //startangle + Vector m_vecAngle2; //endangle + Vector m_vecFinalDest; //basemover finalpos + Vector m_vecFinalAngle; //basemover finalangle + Vector m_vecFloor; //basemover dest floor + float m_flLinearMoveSpeed;//member linear speed + float m_flAngularMoveSpeed;//member angular speed + + // common member functions + void LinearMove ( Vector vecInput, float flSpeed ); + void AngularMove( Vector vecDestAngle, float flSpeed ); + void ComplexMove( Vector vecInput, Vector vecDestAngle, float flSpeed ); + void EXPORT LinearMoveNow( void ); + void EXPORT AngularMoveNow( void ); + void EXPORT ComplexMoveNow( void ); + void EXPORT LinearMoveDone( void ); + void EXPORT AngularMoveDone( void ); + void EXPORT ComplexMoveDone( void ); + void EXPORT LinearMoveDoneNow( void ); + void EXPORT AngularMoveDoneNow( void ); + void EXPORT ComplexMoveDoneNow( void ); +}; + +class CBaseDoor : public CBaseMover +{ +public: + void Spawn( void ); + void Precache( void ); + virtual void PostSpawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + BOOL IsWater( void ){ return pev->skin != 0; }; + void EXPORT DoorTouch( CBaseEntity *pOther ); + virtual void Blocked( CBaseEntity *pOther ); + virtual int ObjectCaps( void ) + { + if ( FBitSet ( pev->spawnflags, SF_DOOR_USE_ONLY ))//door without name player can direct using + return (CBaseMover::ObjectCaps() & ~FCAP_ACROSS_TRANSITION | FCAP_IMPULSE_USE ); + else return (CBaseMover::ObjectCaps() & ~FCAP_ACROSS_TRANSITION); + }; + + // local functions + void EXPORT DoorGoUp( void ); + void EXPORT DoorGoDown( void ); + void EXPORT DoorHitTop( void ); + void EXPORT DoorHitBottom( void ); + virtual BOOL IsRotatingDoor( void ){ return FALSE; }; +}; + +class CRotDoor : public CBaseDoor +{ +public: + virtual void PostSpawn( void ) {} + virtual BOOL IsRotatingDoor( void ){ return TRUE; }; +}; + +class CMomentaryDoor : public CBaseMover +{ +public: + void Spawn( void ); + void Precache( void ); + void PostSpawn( void ); + void EXPORT MomentaryMoveDone( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual int ObjectCaps( void ) { return CBaseMover :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } +}; + +class CBasePlatform : public CBaseMover +{ +public: + void Precache( void ); + void Spawn( void ); + void PostSpawn( void ); + void Setup( void ); + void PostActivate( void ); + virtual void Blocked( CBaseEntity *pOther ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + float CalcFloor( void ) + { + float curfloor = ((pev->origin.z - m_vecPosition1.z) / step()) + 1; + if (curfloor - floor(curfloor) > 0.5) return ceil(curfloor); + else return floor(curfloor); + } + float step( void ) { return pev->size.z + m_flHeight - 2; } + float ftime( void ) { return step() / pev->speed; } //time to moving between floors + + void EXPORT GoUp( void ); + void EXPORT GoDown( void ); + void GoToFloor( float floor ); + void EXPORT HitTop( void ); + void EXPORT HitBottom( void ); + void EXPORT HitFloor( void ); + virtual BOOL IsMovingPlatform( void ) { return m_flHeight != 0 && m_flMoveDistance == 0; } + virtual BOOL IsRotatingPlatform( void ){ return m_flHeight == 0 && m_flMoveDistance != 0; } + virtual BOOL IsComplexPlatform( void ) { return m_flHeight != 0 && m_flMoveDistance != 0; } +}; + +class CFuncTrain : public CBasePlatform +{ +public: + void Spawn( void ); + void PostSpawn( void ); + void OverrideReset( void ); + void PostActivate( void ); + void ClearPointers( void ); + + BOOL Stop( float flWait = -1 ); + BOOL Teleport( void ); + void Blocked( CBaseEntity *pOther ); + BOOL IsWater( void ){ return pev->skin != 0; }; + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + Vector TrainOrg( void ) { return (pev->mins + pev->maxs) * 0.5; } + + void EXPORT Wait( void ); + void EXPORT Next( void ); + + //path operations + CBaseEntity *FindPath( void ); + CBaseEntity *FindNextPath( void ); + void Reverse( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + CBaseEntity *pCurPath, *pNextPath; +}; + +class CFuncTrackTrain : public CBaseMover +{ +public: + void Spawn( void ); + void Precache( void ); + + void Blocked( CBaseEntity *pOther ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData* pkvd ); + + void EXPORT DesiredAction( void ); //LRC - used to be called Next! + void PostActivate( void ); + void ClearPointers( void ); + +// void EXPORT Next( void ); + void EXPORT PostponeNext( void ); + void EXPORT Find( void ); + void EXPORT NearestPath( void ); + void EXPORT DeadEnd( void ); + + void NextThink( float thinkTime, BOOL alwaysThink ); + + void SetTrack( CBaseEntity *track ) { pPath = ((CPathTrack *)track)->Nearest(pev->origin); } + void SetControls( entvars_t *pevControls ); + BOOL OnControls( entvars_t *pev ); + + void StopSound ( void ); + void UpdateSound ( void ); + + static CFuncTrackTrain *Instance( edict_t *pent ) + { + if ( FClassnameIs( pent, "func_tracktrain" ) ) + return (CFuncTrackTrain *)GET_PRIVATE(pent); + return NULL; + } + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static TYPEDESCRIPTION m_SaveData[]; + virtual int ObjectCaps( void ) { return CBaseMover :: ObjectCaps() | FCAP_DIRECTIONAL_USE; } + + virtual void OverrideReset( void ); + + CBaseEntity *pPath; + float m_length; + float m_height; + // I get it... this records the train's max speed (as set by the level designer), whereas + // pev->speed records the current speed (as set by the player). --LRC + // m_speed is also stored, as an int, in pev->impulse. + float m_speed; + float m_dir; + float m_startSpeed; + Vector m_controlMins; + Vector m_controlMaxs; + int m_soundPlaying; + float m_flBank; + float m_oldSpeed; + Vector m_vecBaseAvel; // LRC - the underlying avelocity, superceded by normal turning behaviour where applicable +}; + +#endif //BASEMOVER_H \ No newline at end of file diff --git a/server/ents/baseother.cpp b/server/ents/baseother.cpp new file mode 100644 index 00000000..afca77ef --- /dev/null +++ b/server/ents/baseother.cpp @@ -0,0 +1,270 @@ +//======================================================================= +// Copyright (C) XashXT Group 2007 +// misc temporary entities +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" + +//======================================================================= +// sparkleent - explode post sparks +//======================================================================= +class CEnvShower : public CBaseEntity +{ + void Spawn( void ); + void Think( void ); + void Touch( CBaseEntity *pOther ); + int ObjectCaps( void ) { return FCAP_DONT_SAVE; } +}; +LINK_ENTITY_TO_CLASS( sparkleent, CEnvShower ); + +void CEnvShower::Spawn( void ) +{ + pev->velocity = RANDOM_FLOAT( 200, 300 ) * pev->angles; + pev->velocity.x += RANDOM_FLOAT(-100.f,100.f); + pev->velocity.y += RANDOM_FLOAT(-100.f,100.f); + if ( pev->velocity.z >= 0 ) pev->velocity.z += 200; + else pev->velocity.z -= 200; + pev->movetype = MOVETYPE_BOUNCE; + pev->gravity = 0.5; + SetNextThink( 0.1 ); + pev->solid = SOLID_NOT; + UTIL_SetModel( edict(), "models/common/null.mdl"); + UTIL_SetSize(pev, g_vecZero, g_vecZero ); + pev->effects |= EF_NODRAW; + pev->speed = RANDOM_FLOAT( 0.5, 1.5 ); + pev->angles = g_vecZero; +} + +void CEnvShower::Think( void ) +{ + UTIL_Sparks( pev->origin ); + + pev->speed -= 0.1; + if ( pev->speed > 0 ) SetNextThink( 0.1 ); + else UTIL_Remove( this ); + pev->flags &= ~FL_ONGROUND; +} + +void CEnvShower::Touch( CBaseEntity *pOther ) +{ + if ( pev->flags & FL_ONGROUND ) + pev->velocity = pev->velocity * 0.1; + else pev->velocity = pev->velocity * 0.6; + if ( (pev->velocity.x*pev->velocity.x+pev->velocity.y*pev->velocity.y) < 10.0 ) pev->speed = 0; +} + +//==================================================================== +// ChangeLevelFire +//==================================================================== +class ChangeLevelFire : public CBaseLogic +{ +public: + void PostActivate( void ) { FireTargets(); UTIL_Remove( this ); } + int ObjectCaps( void ) { return CBaseEntity::ObjectCaps() | FCAP_FORCE_TRANSITION; } +}; +LINK_ENTITY_TO_CLASS( fireent, ChangeLevelFire ); +//======================================================================= +// faderent - rendering time effects +//======================================================================= +class CRenderFxFader : public CBaseLogic +{ +public: + void Spawn ( void ) { m_hTarget = CBaseEntity::Instance( pev->owner ); } + void Think ( void ) + { + if (((CBaseEntity*)m_hTarget) == NULL) + { + UTIL_Remove( this ); + return; + } + + float flDegree = (gpGlobals->time - pev->dmgtime)/pev->speed; + + if (flDegree >= 1) + { + m_hTarget->pev->renderamt = pev->renderamt + pev->health; + m_hTarget->pev->rendercolor = pev->rendercolor + pev->movedir; + m_hTarget->pev->scale = pev->scale + pev->frags; + m_hTarget->pev->framerate = pev->framerate + pev->max_health; + UTIL_FireTargets( pev->target, m_hTarget, this, USE_TOGGLE ); + m_hTarget = NULL; + + SetNextThink( 0.1 ); + SetThink(Remove); + } + else + { + m_hTarget->pev->renderamt = pev->renderamt + pev->health * flDegree; + m_hTarget->pev->rendercolor.x = pev->rendercolor.x + pev->movedir.x * flDegree; + m_hTarget->pev->rendercolor.y = pev->rendercolor.y + pev->movedir.y * flDegree; + m_hTarget->pev->rendercolor.z = pev->rendercolor.z + pev->movedir.z * flDegree; + m_hTarget->pev->scale = pev->scale + pev->frags * flDegree; + m_hTarget->pev->framerate = pev->framerate + pev->max_health * flDegree; + SetNextThink( 0.01 );//fader step + } + } +}; +LINK_ENTITY_TO_CLASS( faderent, CRenderFxFader ); + +//======================================================================= +// smokeent - temporary smoke +//======================================================================= +class CSmokeEnt : public CBaseAnimating +{ + void Spawn( void ); + void Think( void ); +}; +LINK_ENTITY_TO_CLASS( smokeent, CSmokeEnt ); + +void CSmokeEnt::Spawn( void ) +{ + pev->solid = SOLID_BBOX; + pev->movetype = MOVETYPE_NONE; + pev->armorvalue = gpGlobals->time; + + if(!pev->team) pev->team = 50; + if(pev->team > 250)pev->team = 250;//normalize and remember about random value 0 - 5 +} + +void CSmokeEnt::Think( void ) +{ + if (pev->dmgtime) + { + if (pev->dmgtime < gpGlobals->time) + { + UTIL_Remove( this ); + return; + } + else if (RANDOM_FLOAT( 0, pev->dmgtime - pev->armorvalue ) > pev->dmgtime - gpGlobals->time) + { + return; + } + } + + Vector VecSrc = UTIL_RandomVector( pev->absmin, pev->absmax ); + + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, VecSrc ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( VecSrc.x ); + WRITE_COORD( VecSrc.y ); + WRITE_COORD( VecSrc.z ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( RANDOM_LONG(0,5) + pev->impulse ); // scale * 10 + WRITE_BYTE( RANDOM_LONG(0, 3) + 8 ); // framerate + MESSAGE_END(); + + StudioFrameAdvance( );//animate model if present + SetNextThink( 0.2 ); +} + +//========================================================= +// func_platform floor indicator +//========================================================= +class CFloorEnt:public CPointEntity +{ + void Think( void ); + void PostActivate( void ); + CBasePlatform *pElevator; //no need to save - PostActivate will be restore this +}; + +void CFloorEnt::PostActivate( void ) +{ + pElevator = (CBasePlatform*)CBasePlatform::Instance( pev->owner ); + SetNextThink( 0 ); +} + +void CFloorEnt::Think ( void ) +{ + if(pElevator && (pElevator->GetState() == STATE_TURN_ON || pElevator->GetState() == STATE_TURN_OFF) ) + { + UTIL_FireTargets( pev->target, pElevator, this, USE_SET, pElevator->CalcFloor()); + SetNextThink( 0.01 ); + } + else UTIL_Remove( this ); +} +LINK_ENTITY_TO_CLASS( floorent, CFloorEnt ); + + +//==================================================================== +// Laser pointer target +//==================================================================== +CLaserSpot *CLaserSpot::CreateSpot( void ) +{ + CLaserSpot *pSpot = GetClassPtr( (CLaserSpot *)NULL ); + pSpot->Spawn(); + pSpot->pev->classname = MAKE_STRING("laserspotent"); + + return pSpot; +} + +void CLaserSpot::Precache( void ) +{ + UTIL_PrecacheModel("sprites/glow02.spr"); + UTIL_PrecacheSound("weapons/spot_on.wav"); + UTIL_PrecacheSound("weapons/spot_off.wav"); +} + +void CLaserSpot::Spawn( void ) +{ + Precache( ); + + //laser dot settings + pev->movetype = MOVETYPE_FLY; + pev->solid = SOLID_NOT; + pev->scale = 1.0; + pev->rendermode = kRenderGlow; + pev->renderfx = kRenderFxNoDissipation; + pev->renderamt = 255; + pev->rendercolor = Vector( 200, 12, 12 ); + UTIL_SetModel(ENT(pev), "sprites/glow02.spr" ); + UTIL_SetOrigin( this, pev->origin ); +} + +void CLaserSpot::Suspend( float flSuspendTime ) +{ + pev->effects |= EF_NODRAW; + + // -1 means suspend indefinitely + if (flSuspendTime == -1) SetThink( NULL ); + else + { + SetThink( Revive ); + SetNextThink( flSuspendTime ); + } +} + +void CLaserSpot::Revive( void ) +{ + pev->effects &= ~EF_NODRAW; + SetThink( NULL ); +} + +void CLaserSpot::Update( CBasePlayer *m_pPlayer ) +{ + TraceResult tr; + + UTIL_MakeVectors( m_pPlayer->pev->v_angle ); + UTIL_TraceLine( m_pPlayer->GetGunPosition(), m_pPlayer->GetGunPosition() + gpGlobals->v_forward * 8192, dont_ignore_monsters, ENT(m_pPlayer->pev), &tr ); + UTIL_SetOrigin( this, tr.vecEndPos ); + + if( UTIL_PointContents( tr.vecEndPos ) & CONTENTS_SKY && VARS(tr.pHit)->solid == SOLID_BSP ) + { + pev->renderamt = 33; + pev->effects |= EF_NODRAW; + } + else + { + pev->effects &= ~EF_NODRAW; + + float SpotDistance = (tr.vecEndPos - m_pPlayer->GetGunPosition()).Length(); + int brightness = (1 / log(SpotDistance / 0.3)) * 1300; + pev->scale = SpotDistance / 2500 + RANDOM_FLOAT(0.01, SpotDistance/2750); + + if(pev->renderamt >= 255) pev->renderamt = brightness + RANDOM_LONG(1, SpotDistance/400); + else pev->renderamt += 5; + } +} +LINK_ENTITY_TO_CLASS( laserspotent, CLaserSpot ); \ No newline at end of file diff --git a/server/ents/basepath.cpp b/server/ents/basepath.cpp new file mode 100644 index 00000000..174a5719 --- /dev/null +++ b/server/ents/basepath.cpp @@ -0,0 +1,427 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +//atoi(g_engfuncs.pfnInfoKeyValue(g_engfuncs.pfnGetInfoKeyBuffer(edict()), "skin")); +//use this for custom change speed + +//========================================================= +// path_corner - train node path. +//========================================================= +TYPEDESCRIPTION CPathCorner::m_SaveData[] = +{ DEFINE_FIELD( CPathCorner, m_pNextPath1, FIELD_CLASSPTR ), + DEFINE_FIELD( CPathCorner, m_pNextPath2, FIELD_CLASSPTR ), + DEFINE_FIELD( CPathCorner, m_pPrevPath, FIELD_CLASSPTR ), +};IMPLEMENT_SAVERESTORE(CPathCorner, CBaseLogic); + +void CPathCorner :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "altpath")) + { + pev->netname = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} +LINK_ENTITY_TO_CLASS( path_corner, CPathCorner ); + +void CPathCorner :: Spawn( void ) +{ + m_iState = STATE_ON; + SetBits( pev->flags, FL_POINTENTITY ); +} + +void CPathCorner :: Link( void ) +{ + CBaseEntity *pTarget; + + if ( FStringNull( pev->targetname ) ) return; + + if ( !FStringNull(pev->target) ) + { + pTarget = UTIL_FindEntityByTargetname( NULL, STRING(pev->target)); + if(pTarget) + { + m_pNextPath1 = (CPathCorner*)pTarget;//valid path + m_pNextPath1->SetPrev( this ); + } + else DevMsg( "Dead end link %s\n", STRING(pev->target) ); + } + if ( !FStringNull(pev->netname) ) + { + pTarget = UTIL_FindEntityByTargetname( NULL, STRING(pev->netname)); + if(pTarget) + { + m_pNextPath2 = (CPathCorner*)pTarget;//valid path + m_pNextPath2->SetPrev( this ); + } + else DevMsg( "Dead end link %s\n", STRING(pev->netname) ); + } +} + +void CPathCorner::UpdateTargets( void ) +{ + UTIL_FireTargets(pev->message, this, this, USE_TOGGLE ); + + if(pev->spawnflags & SF_CORNER_FIREONCE) + { + pev->message = iStringNull; + ClearBits( pev->spawnflags, SF_CORNER_FIREONCE ); + } +} + +CBaseEntity *CPathCorner::ValidPath( CBaseEntity *m_pPath ) +{ + ASSERTSZ( m_pPath != this, "ValidPath: self path!\n"); + + if(m_pPath && m_pPath->edict() && m_pPath != this && ((CPathCorner *)m_pPath)->m_iState == STATE_ON) + return m_pPath; + return this; +} + +CBaseEntity *CPathCorner::GetNext( void ) +{ + if(pev->team) return ValidPath( m_pNextPath2 ); + return ValidPath( m_pNextPath1 ); +} + +void CPathCorner :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) m_iState = STATE_ON; + else if(useType == USE_OFF) m_iState = STATE_OFF; + else if(useType == USE_SET) pev->team = !pev->team; //change path + else if (useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, wait %g\n", GetStringForState( GetState()), m_flWait); + if(ValidPath(m_pPrevPath)) Msg("Prev path %s", STRING(m_pPrevPath->pev->targetname)); + if(ValidPath(m_pNextPath1))Msg("Next path %s", STRING(m_pNextPath1->pev->targetname)); + if(ValidPath(m_pNextPath2))Msg("Alt path %s", STRING(m_pNextPath2->pev->targetname)); + SHIFT; + } +} + + +//========================================================= +// path_track - tracktrain node path. +//========================================================= +TYPEDESCRIPTION CPathTrack::m_SaveData[] = +{ + DEFINE_FIELD( CPathTrack, m_length, FIELD_FLOAT ), + DEFINE_FIELD( CPathTrack, m_pnext, FIELD_CLASSPTR ), + DEFINE_FIELD( CPathTrack, m_paltpath, FIELD_CLASSPTR ), + DEFINE_FIELD( CPathTrack, m_pprevious, FIELD_CLASSPTR ), + DEFINE_FIELD( CPathTrack, m_altName, FIELD_STRING ), +}; + +IMPLEMENT_SAVERESTORE( CPathTrack, CBaseEntity ); +LINK_ENTITY_TO_CLASS( path_track, CPathTrack ); + +// +// Cache user-entity-field values until spawn is called. +// +void CPathTrack :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "altpath")) + { + m_altName = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "turnspeed")) //LRC + { + if (pkvd->szValue[0]) // if the field is blank, don't set the spawnflag. + { + pev->spawnflags |= SF_PATH_AVELOCITY; + UTIL_StringToVector( (float*)pev->avelocity, pkvd->szValue); + } + pkvd->fHandled = TRUE; + } + else + CBaseLogic::KeyValue( pkvd ); +} + +void CPathTrack :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + int on; + + // Use toggles between two paths + if ( m_paltpath ) + { + on = !FBitSet( pev->spawnflags, SF_PATH_ALTERNATE ); + if (useType == USE_TOGGLE) + { + if(on) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON)ClearBits( pev->spawnflags, SF_PATH_ALTERNATE ); + else if(useType == USE_OFF)SetBits( pev->spawnflags, SF_PATH_ALTERNATE ); + } + else // Use toggles between enabled/disabled + { + on = !FBitSet( pev->spawnflags, SF_PATH_DISABLED ); + if (useType == USE_TOGGLE) + { + if(on) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON)ClearBits( pev->spawnflags, SF_PATH_DISABLED ); + else if(useType == USE_OFF)SetBits( pev->spawnflags, SF_PATH_DISABLED ); + } +} + + +void CPathTrack :: Link( void ) +{ + CBaseEntity *pTarget; + + if ( !FStringNull(pev->target) ) + { + pTarget = UTIL_FindEntityByTargetname( NULL, STRING(pev->target) ); + if ( pTarget ) + { + m_pnext = (CPathTrack*)pTarget; + m_pnext->SetPrevious( this ); + } + else + ALERT( at_debug, "Dead end link %s\n", STRING(pev->target) ); + } + + // Find "alternate" path + if ( m_altName ) + { + pTarget = UTIL_FindEntityByTargetname( NULL, STRING(m_altName) ); + if ( pTarget ) // If no next pointer, this is the end of a path + { + m_paltpath = (CPathTrack*)pTarget; + m_paltpath->SetPrevious( this ); + } + } +} + + +void CPathTrack :: Spawn( void ) +{ + pev->solid = SOLID_TRIGGER; + UTIL_SetSize(pev, Vector(-8, -8, -8), Vector(8, 8, 8)); + + m_pnext = NULL; + m_pprevious = NULL; +// DEBUGGING CODE +#if PATH_SPARKLE_DEBUG + SetThink( Sparkle ); + SetNextThink( 0.5 ); +#endif +} + + +void CPathTrack::Activate( void ) +{ + if ( !FStringNull( pev->targetname ) ) // Link to next, and back-link + Link(); + + CBaseLogic::Activate(); +} + +CBaseEntity *CPathTrack :: ValidPath( CBaseEntity *ppath, int testFlag ) +{ + if ( !ppath ) + return NULL; + + if ( testFlag && FBitSet( ppath->pev->spawnflags, SF_PATH_DISABLED ) ) + return NULL; + + return ppath; +} + + +void CPathTrack :: Project( CBaseEntity *pstart, CBaseEntity *pend, Vector *origin, float dist ) +{ + if ( pstart && pend ) + { + Vector dir = (pend->pev->origin - pstart->pev->origin); + dir = dir.Normalize(); + *origin = pend->pev->origin + dir * dist; + } +} + +CBaseEntity *CPathTrack::GetNext( void ) +{ + if ( m_paltpath && FBitSet( pev->spawnflags, SF_PATH_ALTERNATE ) && !FBitSet( pev->spawnflags, SF_PATH_ALTREVERSE ) ) + return m_paltpath; + + return m_pnext; +} + + + +CBaseEntity *CPathTrack::GetPrev( void ) +{ + if ( m_paltpath && FBitSet( pev->spawnflags, SF_PATH_ALTERNATE ) && FBitSet( pev->spawnflags, SF_PATH_ALTREVERSE ) ) + return m_paltpath; + + return m_pprevious; +} + + + +void CPathTrack::SetPrevious( CPathTrack *pprev ) +{ + // Only set previous if this isn't my alternate path + if ( pprev && !FStrEq( STRING(pprev->pev->targetname), STRING(m_altName) ) ) + m_pprevious = pprev; +} + + +// Assumes this is ALWAYS enabled +CBaseEntity *CPathTrack :: LookAhead( Vector *origin, float dist, int move ) +{ + CBaseEntity *pcurrent; + float originalDist = dist; + + pcurrent = this; + Vector currentPos = *origin; + + if ( dist < 0 ) // Travelling backwards through path + { + dist = -dist; + while ( dist > 0 ) + { + Vector dir = pcurrent->pev->origin - currentPos; + float length = dir.Length(); + if ( !length ) + { + if ( !ValidPath(pcurrent->GetPrev(), move) ) // If there is no previous node, or it's disabled, return now. + { + if ( !move ) + Project( pcurrent->GetNext(), pcurrent, origin, dist ); + return NULL; + } + pcurrent = pcurrent->GetPrev(); + } + else if ( length > dist ) // enough left in this path to move + { + *origin = currentPos + (dir * (dist / length)); + return pcurrent; + } + else + { + dist -= length; + currentPos = pcurrent->pev->origin; + *origin = currentPos; + if ( !ValidPath(pcurrent->GetPrev(), move) ) // If there is no previous node, or it's disabled, return now. + return NULL; + + pcurrent = pcurrent->GetPrev(); + } + } + *origin = currentPos; + return pcurrent; + } + else + { + while ( dist > 0 ) + { + if ( !ValidPath(pcurrent->GetNext(), move) ) // If there is no next node, or it's disabled, return now. + { + if ( !move ) + Project( pcurrent->GetPrev(), pcurrent, origin, dist ); + return NULL; + } + Vector dir = pcurrent->GetNext()->pev->origin - currentPos; + float length = dir.Length(); + if ( !length && !ValidPath( pcurrent->GetNext()->GetNext(), move ) ) + { + if ( dist == originalDist ) // HACK -- up against a dead end + return NULL; + return pcurrent; + } + if ( length > dist ) // enough left in this path to move + { + *origin = currentPos + (dir * (dist / length)); + return pcurrent; + } + else + { + dist -= length; + currentPos = pcurrent->GetNext()->pev->origin; + pcurrent = pcurrent->GetNext(); + *origin = currentPos; + } + } + *origin = currentPos; + } + + return pcurrent; +} + + +// Assumes this is ALWAYS enabled +CBaseEntity *CPathTrack :: Nearest( Vector origin ) +{ + int deadCount; + float minDist, dist; + Vector delta; + CBaseEntity *ppath, *pnearest; + + + delta = origin - pev->origin; + delta.z = 0; + minDist = delta.Length(); + pnearest = this; + ppath = GetNext(); + + // Hey, I could use the old 2 racing pointers solution to this, but I'm lazy :) + deadCount = 0; + while ( ppath && ppath != this ) + { + deadCount++; + if ( deadCount > 9999 ) + { + ALERT( at_error, "Bad sequence of path_tracks from %s", STRING(pev->targetname) ); + return NULL; + } + delta = origin - ppath->pev->origin; + delta.z = 0; + dist = delta.Length(); + if ( dist < minDist ) + { + minDist = dist; + pnearest = ppath; + } + ppath = ppath->GetNext(); + } + return pnearest; +} + + +CPathTrack *CPathTrack::Instance( edict_t *pent ) +{ + if ( FClassnameIs( pent, "path_track" ) ) + return (CPathTrack *)GET_PRIVATE(pent); + return NULL; +} + + + // DEBUGGING CODE +#if PATH_SPARKLE_DEBUG +void CPathTrack :: Sparkle( void ) +{ + + SetNextThink( 0.2 ); + if ( FBitSet( pev->spawnflags, SF_PATH_DISABLED ) ) + UTIL_ParticleEffect(pev->origin, Vector(0,0,100), 210, 10); + else + UTIL_ParticleEffect(pev->origin, Vector(0,0,100), 84, 10); +} +#endif \ No newline at end of file diff --git a/server/ents/baserockets.cpp b/server/ents/baserockets.cpp new file mode 100644 index 00000000..5275333f --- /dev/null +++ b/server/ents/baserockets.cpp @@ -0,0 +1,961 @@ +//======================================================================= +// Copyright (C) Shambler Team 2006 +// rockets.cpp - projectiles code +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "baseweapon.h" +#include "nodes.h" +#include "soundent.h" +#include "decals.h" +#include "defaults.h" + + +//=========================== +// +// Grenade code +// +//=========================== +void CGrenade::Explode( TraceResult *pTrace, int bitsDamageType ) +{ + float flRndSound;// sound randomizer + + pev->model = iStringNull;//invisible + pev->solid = SOLID_NOT;// intangible + pev->takedamage = DAMAGE_NO; + + UTIL_Explode( pTrace->vecEndPos, pev->owner, pev->impulse, pev->classname ); + + flRndSound = RANDOM_FLOAT( 0 , 1 ); + switch ( RANDOM_LONG( 0, 2 ) ) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "weapons/debris1.wav", 0.55, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "weapons/debris2.wav", 0.55, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_VOICE, "weapons/debris3.wav", 0.55, ATTN_NORM); break; + } + + pev->effects |= EF_NODRAW; + SetThink( Remove ); + pev->velocity = g_vecZero; + SetNextThink( 0.3 ); +} + +void CGrenade::Killed( entvars_t *pevAttacker, int iGib ) +{ + Detonate( ); +} + +// Timed grenade, this think is called when time runs out. +void CGrenade::DetonateUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + SetThink( Detonate ); + SetNextThink( 0 ); +} + +void CGrenade::PreDetonate( void ) +{ + CSoundEnt::InsertSound ( bits_SOUND_DANGER, pev->origin, 400, 0.3 ); + + SetThink( Detonate ); + SetNextThink( 1 ); +} + +void CGrenade::Detonate( void ) +{ + TraceResult tr; + Vector vecSpot;// trace starts here! + + vecSpot = pev->origin + Vector ( 0 , 0 , 8 ); + UTIL_TraceLine ( vecSpot, vecSpot + Vector ( 0, 0, -40 ), ignore_monsters, ENT(pev), & tr); + + Explode( &tr, DMG_BLAST ); +} + +void CGrenade::ExplodeTouch( CBaseEntity *pOther ) +{ + TraceResult tr; + Vector vecSpot;// trace starts here! + + pev->enemy = pOther->edict(); + + vecSpot = pev->origin - pev->velocity.Normalize() * 32; + UTIL_TraceLine( vecSpot, vecSpot + pev->velocity.Normalize() * 64, ignore_monsters, ENT(pev), &tr ); + + Explode( &tr, DMG_BLAST ); +} + +void CGrenade::DangerSoundThink( void ) +{ + if (!IsInWorld()) + { + UTIL_Remove( this ); + return; + } + + CSoundEnt::InsertSound ( bits_SOUND_DANGER, pev->origin + pev->velocity * 0.5, pev->velocity.Length( ), 0.2 ); + SetNextThink( 0.2 ); + + if( pev->waterlevel != 0 && pev->watertype & MASK_WATER ) + { + pev->velocity = pev->velocity * 0.5; + } +} + +void CGrenade::BounceTouch( CBaseEntity *pOther ) +{ + // don't hit the guy that launched this grenade + if ( pOther->edict() == pev->owner ) + return; + + // only do damage if we're moving fairly fast + if (m_flNextAttack < gpGlobals->time && pev->velocity.Length() > 100) + { + entvars_t *pevOwner = VARS( pev->owner ); + if (pevOwner) + { + TraceResult tr = UTIL_GetGlobalTrace( ); + ClearMultiDamage( ); + pOther->TraceAttack(pevOwner, 1, gpGlobals->v_forward, &tr, DMG_CLUB ); + ApplyMultiDamage( pev, pevOwner); + } + m_flNextAttack = gpGlobals->time + 1.0; // debounce + } + + Vector vecTestVelocity; + // pev->avelocity = Vector (300, 300, 300); + + // this is my heuristic for modulating the grenade velocity because grenades dropped purely vertical + // or thrown very far tend to slow down too quickly for me to always catch just by testing velocity. + // trimming the Z velocity a bit seems to help quite a bit. + vecTestVelocity = pev->velocity; + vecTestVelocity.z *= 0.45; + + if ( !m_fRegisteredSound && vecTestVelocity.Length() <= 60 ) + { + //ALERT( at_console, "Grenade Registered!: %f\n", vecTestVelocity.Length() ); + + // grenade is moving really slow. It's probably very close to where it will ultimately stop moving. + // go ahead and emit the danger sound. + + // register a radius louder than the explosion, so we make sure everyone gets out of the way + CSoundEnt::InsertSound ( bits_SOUND_DANGER, pev->origin, pev->dmg / 0.4, 0.3 ); + m_fRegisteredSound = TRUE; + } + + if (pev->flags & FL_ONGROUND) + { + // add a bit of static friction + pev->velocity = pev->velocity * 0.8; + pev->sequence = RANDOM_LONG( 2, 2 ); + } + else + { + // play bounce sound + BounceSound(); + } + pev->framerate = pev->velocity.Length() / 200.0; + if (pev->framerate > 1.0) pev->framerate = 1; + else if (pev->framerate < 0.5) pev->framerate = 0; + +} + +void CGrenade::SlideTouch( CBaseEntity *pOther ) +{ + // don't hit the guy that launched this grenade + if ( pOther->edict() == pev->owner ) + return; + + // pev->avelocity = Vector (300, 300, 300); + + if (pev->flags & FL_ONGROUND) + { + // add a bit of static friction + pev->velocity = pev->velocity * 0.95; + + if (pev->velocity.x != 0 || pev->velocity.y != 0) + { + // maintain sliding sound + } + } + else BounceSound(); +} + +void CGrenade :: BounceSound( void ) +{ + switch ( RANDOM_LONG( 0, 2 ) ) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "weapons/grenade_hit1.wav", 0.25, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "weapons/grenade_hit2.wav", 0.25, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_VOICE, "weapons/grenade_hit3.wav", 0.25, ATTN_NORM); break; + } +} + +void CGrenade :: TumbleThink( void ) +{ + if (!IsInWorld()) + { + UTIL_Remove( this ); + return; + } + + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + if (pev->dmgtime - 1 < gpGlobals->time) + { + CSoundEnt::InsertSound ( bits_SOUND_DANGER, pev->origin + pev->velocity * (pev->dmgtime - gpGlobals->time), 400, 0.1 ); + } + + if (pev->dmgtime <= gpGlobals->time) + { + SetThink( Detonate ); + } + if( pev->waterlevel != 0 && pev->watertype & MASK_WATER ) + { + pev->velocity = pev->velocity * 0.5; + pev->framerate = 0.2; + } +} + + +void CGrenade:: Spawn( void ) +{ + pev->movetype = MOVETYPE_BOUNCE; + pev->classname = MAKE_STRING( "grenade" ); + + pev->solid = SOLID_BBOX; + + UTIL_SetModel(ENT(pev), "models/grenade.mdl"); + UTIL_SetSize(pev, Vector( 0, 0, 0), Vector(0, 0, 0)); + + pev->dmg = 100; + m_fRegisteredSound = FALSE; +} + + +CGrenade *CGrenade::ShootContact( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity ) +{ + CGrenade *pGrenade = GetClassPtr( (CGrenade *)NULL ); + pGrenade->Spawn(); + // contact grenades arc lower + pGrenade->pev->gravity = 0.5;// lower gravity since grenade is aerodynamic and engine doesn't know it. + UTIL_SetOrigin( pGrenade, vecStart ); + pGrenade->pev->velocity = vecVelocity; + pGrenade->pev->angles = UTIL_VecToAngles (pGrenade->pev->velocity); + pGrenade->pev->owner = ENT(pevOwner); + pGrenade->pev->flags |= FL_PROJECTILE; + + // make monsters afaid of it while in the air + pGrenade->SetThink( DangerSoundThink ); + pGrenade->SetNextThink( 0 ); + + // Tumble in air + pGrenade->pev->avelocity.x = RANDOM_FLOAT ( -100, -500 ); + + // Explode on contact + pGrenade->SetTouch( ExplodeTouch ); + + pGrenade->pev->dmg = M203_DMG; + + return pGrenade; +} + + +CGrenade * CGrenade:: ShootTimed( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity, float time ) +{ + CGrenade *pGrenade = GetClassPtr( (CGrenade *)NULL ); + pGrenade->Spawn(); + UTIL_SetOrigin( pGrenade, vecStart ); + pGrenade->pev->velocity = vecVelocity; + pGrenade->pev->angles = UTIL_VecToAngles(pGrenade->pev->velocity); + pGrenade->pev->owner = ENT(pevOwner); + + pGrenade->SetTouch(&CGrenade:: BounceTouch ); // Bounce if touched + + // Take one second off of the desired detonation time and set the think to PreDetonate. PreDetonate + // will insert a DANGER sound into the world sound list and delay detonation for one second so that + // the grenade explodes after the exact amount of time specified in the call to ShootTimed(). + + pGrenade->pev->dmgtime = gpGlobals->time + time; + pGrenade->SetThink( TumbleThink ); + pGrenade->SetNextThink( 0.1 ); + if (time < 0.1) + { + pGrenade->SetNextThink( 0 ); + pGrenade->pev->velocity = Vector( 0, 0, 0 ); + } + + pGrenade->pev->sequence = RANDOM_LONG( 4, 7 ); + pGrenade->pev->framerate = 1.0; + pGrenade->pev->flags |= FL_PROJECTILE; + + // Tumble through the air + // pGrenade->pev->avelocity.x = -400; + + pGrenade->pev->gravity = 0.5; + pGrenade->pev->friction = 0.8; + + UTIL_SetModel(ENT(pGrenade->pev), "models/weapons/w_grenade.mdl"); + pGrenade->pev->dmg = 100; + + return pGrenade; +} + +LINK_ENTITY_TO_CLASS( grenade, CGrenade ); + +//=========================== +// +// Rocket code +// +//=========================== +CRpgRocket *CRpgRocket::Create ( Vector vecOrigin, Vector vecAngles, CBaseEntity *pOwner, CBasePlayerWeapon *pLauncher ) +{ + CRpgRocket *pRocket = GetClassPtr( (CRpgRocket *)NULL ); + + UTIL_SetOrigin( pRocket, vecOrigin ); + pRocket->pev->angles = vecAngles; + pRocket->Spawn(); + pRocket->SetTouch( RocketTouch ); + pRocket->m_pLauncher = pLauncher;// remember what RPG fired me. + pRocket->m_pLauncher->m_cActiveRocket++;// register this missile as active for the launcher + pRocket->pev->owner = pOwner->edict(); + pRocket->pev->flags |= FL_PROJECTILE; + + return pRocket; +} + +TYPEDESCRIPTION CRpgRocket::m_SaveData[] = +{ + DEFINE_FIELD( CRpgRocket, m_flIgniteTime, FIELD_TIME ), + DEFINE_FIELD( CRpgRocket, m_pLauncher, FIELD_CLASSPTR ), +}; +IMPLEMENT_SAVERESTORE( CRpgRocket, CGrenade ); + +void CRpgRocket :: Spawn( void ) +{ + Precache( ); + // motor + pev->movetype = MOVETYPE_BOUNCE; + pev->solid = SOLID_BBOX; + UTIL_SetModel(ENT(pev), "models/rpgrocket.mdl"); + UTIL_SetSize(pev, Vector( 0, 0, 0), Vector(0, 0, 0)); + UTIL_SetOrigin( this, pev->origin ); + + pev->classname = MAKE_STRING("rpg_rocket"); + + SetThink( IgniteThink ); + SetTouch( ExplodeTouch ); + + pev->angles.x -= 30; + UTIL_MakeVectors( pev->angles ); + pev->angles.x = -(pev->angles.x + 30); + pev->velocity = gpGlobals->v_forward * 250; + pev->gravity = 0.5; + + SetNextThink( 0.4 ); + pev->dmg = RPG_ROCKET_DMG; +} + +void CRpgRocket :: RocketTouch ( CBaseEntity *pOther ) +{ + CBaseEntity *pPlayer = CBaseEntity::Instance(pev->owner); + if ( m_pLauncher ) m_pLauncher->m_cActiveRocket--; + STOP_SOUND( edict(), CHAN_VOICE, "weapons/rpg/rocket1.wav" ); + ExplodeTouch( pOther ); +} + +void CRpgRocket::Detonate( void ) +{ + TraceResult tr; + Vector vecSpot;// trace starts here! + CBaseEntity *pPlayer = CBaseEntity::Instance(pev->owner); + if ( m_pLauncher ) m_pLauncher->m_cActiveRocket--; + STOP_SOUND( edict(), CHAN_VOICE, "weapons/rpg/rocket1.wav" ); + vecSpot = pev->origin + Vector ( 0 , 0 , 8 ); + UTIL_TraceLine ( vecSpot, vecSpot + Vector ( 0, 0, -40 ), ignore_monsters, ENT(pev), & tr); + + Explode( &tr, DMG_BLAST ); +} + +void CRpgRocket :: Precache( void ) +{ + UTIL_PrecacheModel("models/rpgrocket.mdl"); + UTIL_PrecacheSound ("weapons/rpg/rocket1.wav"); + UTIL_PrecacheSound ("weapons/rpg/beep.wav"); + UTIL_PrecacheSound ("weapons/rpg/beep2.wav"); + m_iTrail = UTIL_PrecacheModel("sprites/smoke.spr"); +} + +void CRpgRocket :: IgniteThink( void ) +{ + pev->movetype = MOVETYPE_FLY; + pev->effects |= EF_LIGHT; + + CreateTrail(); + m_flIgniteTime = gpGlobals->time; + + // set to follow laser spot + SetThink( FollowThink ); + SetNextThink( 0.1 ); +} + +void CRpgRocket :: CreateTrail( void ) +{ + if(b_setup) return; + + // make rocket sound after save\load + EMIT_SOUND( ENT(pev), CHAN_VOICE, "weapons/rpg/rocket1.wav", 1, 0.5 ); + + // restore rocket trail + SFX_Trail(entindex(), m_iTrail); + b_setup = TRUE; +} + +void CRpgRocket :: FollowThink( void ) +{ + CBaseEntity *pOther = NULL; + Vector vecTarget; + Vector vecDir; + float flDist, flMax, flDot; + TraceResult tr; + UTIL_MakeAimVectors( pev->angles ); + + CreateTrail(); + + vecTarget = gpGlobals->v_forward; + flMax = 4096; + // Examine all entities within a reasonable radius + while ((pOther = UTIL_FindEntityByClassname( pOther, "laserspotent" )) != NULL) + { + UTIL_TraceLine ( pev->origin, pOther->pev->origin, dont_ignore_monsters, ENT(pev), &tr ); + // Msg( "%f\n", tr.flFraction ); + if (tr.flFraction >= 0.90) + { + vecDir = pOther->pev->origin - pev->origin; + flDist = vecDir.Length( ); + vecDir = vecDir.Normalize( ); + flDot = DotProduct( gpGlobals->v_forward, vecDir ); + if ((flDot > 0) && (flDist * (1 - flDot) < flMax)) + { + flMax = flDist * (1 - flDot); + vecTarget = vecDir; + } + } + } + + pev->angles = UTIL_VecToAngles( vecTarget ); + + // this acceleration and turning math is totally wrong, but it seems to respond well so don't change it. + float flSpeed = pev->velocity.Length(); + if (gpGlobals->time - m_flIgniteTime < 1.0) + { + pev->velocity = pev->velocity * 0.2 + vecTarget * (flSpeed * 0.8 + 400); + if( pev->waterlevel == 3 && pev->watertype & MASK_WATER ) + { + // go slow underwater + if (pev->velocity.Length() > 300) + { + pev->velocity = pev->velocity.Normalize() * 300; + } + UTIL_BubbleTrail( pev->origin - pev->velocity * 0.1, pev->origin, 4 ); + } + else + { + if (pev->velocity.Length() > 2000) + { + pev->velocity = pev->velocity.Normalize() * 2000; + } + } + } + else + { + if (pev->effects & EF_LIGHT) + { + pev->effects = 0; + STOP_SOUND( ENT(pev), CHAN_VOICE, "weapons/rpg/rocket1.wav" ); + } + pev->velocity = pev->velocity * 0.2 + vecTarget * flSpeed * 0.798; + if ((pev->waterlevel == 0 || pev->watertype == CONTENTS_FOG) && pev->velocity.Length() < 1500) + { + Detonate( ); + } + } + //Msg( "%.0f\n", flSpeed ); + + SetNextThink( 0.05 ); +} +LINK_ENTITY_TO_CLASS( rpg_rocket, CRpgRocket ); + +//=========================== +// apache HVR rocket +//=========================== +void CApacheHVR :: Spawn( void ) +{ + Precache( ); + // motor + pev->movetype = MOVETYPE_FLY; + pev->solid = SOLID_BBOX; + + UTIL_SetModel(ENT(pev), "models/HVR.mdl"); + UTIL_SetSize(pev, Vector( 0, 0, 0), Vector(0, 0, 0)); + UTIL_SetOrigin( this, pev->origin ); + + SetThink( IgniteThink ); + SetTouch( ExplodeTouch ); + + UTIL_MakeAimVectors( pev->angles ); + pev->movedir = gpGlobals->v_forward; + pev->gravity = 0.5; + + SetNextThink( 0.1 ); + + pev->dmg = 150; +} + +void CApacheHVR :: Precache( void ) +{ + UTIL_PrecacheModel("models/HVR.mdl"); + m_iTrail = UTIL_PrecacheModel("sprites/smoke.spr"); + UTIL_PrecacheSound("weapons/rocket1.wav"); +} + +void CApacheHVR :: IgniteThink( void ) +{ + // pev->movetype = MOVETYPE_TOSS; + + // pev->movetype = MOVETYPE_FLY; + pev->effects |= EF_LIGHT; + + // make rocket sound + EMIT_SOUND( ENT(pev), CHAN_VOICE, "weapons/rocket1.wav", 1, 0.5 ); + + // rocket trail + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BEAMFOLLOW ); + WRITE_SHORT(entindex()); // entity + WRITE_SHORT(m_iTrail ); // model + WRITE_BYTE( 15 ); // life + WRITE_BYTE( 5 ); // width + WRITE_BYTE( 224 ); // r, g, b + WRITE_BYTE( 224 ); // r, g, b + WRITE_BYTE( 255 ); // r, g, b + WRITE_BYTE( 255 ); // brightness + MESSAGE_END(); // move PHS/PVS data sending into here (SEND_ALL, SEND_PVS, SEND_PHS) + + // set to accelerate + SetThink( AccelerateThink ); + SetNextThink( 0.1 ); +} + + +void CApacheHVR :: AccelerateThink( void ) +{ + // check world boundaries + if(!IsInWorld()) + { + UTIL_Remove( this ); + return; + } + + // accelerate + float flSpeed = pev->velocity.Length(); + if (flSpeed < 1800) + { + pev->velocity = pev->velocity + pev->movedir * 200; + } + + // re-aim + pev->angles = UTIL_VecToAngles( pev->velocity ); + + SetNextThink( 0.1 ); +} +LINK_ENTITY_TO_CLASS( hvr_rocket, CApacheHVR ); + +//=========================== +// Nuclear explode code +//=========================== +CNukeExplode *CNukeExplode::Create ( Vector vecOrigin, CBaseEntity *pOwner ) +{ + CNukeExplode *pNuke = GetClassPtr( (CNukeExplode *)NULL ); + UTIL_SetOrigin( pNuke, vecOrigin ); + pNuke->Spawn(); + pNuke->pev->classname = MAKE_STRING("nuclear_explode"); + pNuke->pev->owner = pOwner->edict(); + + return pNuke; +} + +void CNukeExplode :: Spawn( void ) +{ + Precache(); + UTIL_SetModel(ENT(pev), "models/nexplode.mdl"); + TraceResult tr; + UTIL_TraceLine ( pev->origin, pev->origin + Vector ( 0, 0, -32 ), ignore_monsters, ENT(pev), &tr); + UTIL_ScreenShake( pev->origin, 16, 1, 2, 1700 ); + pev->oldorigin = tr.vecEndPos + (tr.vecPlaneNormal * 30);//save normalized position + + //create first explode sprite + SFX_Explode( m_usExplodeSprite, pev->oldorigin, 100, TE_EXPLFLAG_NOPARTICLES | TE_EXPLFLAG_NOSOUND ); + EMIT_SOUND( ENT(pev), CHAN_VOICE, "weapons/warhead/whexplode.wav", 1, ATTN_NONE ); + + pev->movetype = MOVETYPE_NONE; + pev->solid = SOLID_NOT; + pev->rendermode = kRenderTransAdd; + pev->renderamt = 190; + pev->scale = 0.5; + UTIL_SetOrigin( this, pev->origin ); + + SetThink( ExplodeThink ); + + pev->dmg = REDEEMER_ROCKET_DMG; + SetNextThink( 0 ); +} + +void CNukeExplode :: Precache( void ) +{ + m_usExplodeSprite = UTIL_PrecacheModel("sprites/warhead01.spr"); + m_usExplodeSprite2 = UTIL_PrecacheModel("sprites/warhead02.spr"); + UTIL_PrecacheModel("models/nexplode.mdl"); + UTIL_PrecacheSound("weapons/warhead/whexplode.wav"); +} + +void CNukeExplode :: ExplodeThink( void ) +{ + pev->renderamt -= 1.5; + pev->scale += .2; + + if(pev->scale >= 8 && pev->scale < 8.2)//create second explode sprite + SFX_Explode( m_usExplodeSprite2, pev->oldorigin, 100, TE_EXPLFLAG_NOPARTICLES | TE_EXPLFLAG_NOSOUND ); + + entvars_t *pevOwner; + if ( pev->owner ) pevOwner = VARS( pev->owner ); + else pevOwner = NULL; + pev->owner = NULL; //can't traceline attack owner if this is set + + ::RadiusDamage( pev->origin, pev, pevOwner, pev->renderamt/2, pev->scale * 30, CLASS_NONE, DMG_BLAST | DMG_NUCLEAR ); + if (pev->scale > 25 )UTIL_Remove( this ); + + SetNextThink( 0.01 ); +} +LINK_ENTITY_TO_CLASS( nuclear_explode, CNukeExplode ); + +//=========================== +// Nuke rocket code +//=========================== +CWHRocket *CWHRocket::Create ( Vector vecOrigin, Vector vecAngles, CBaseEntity *pOwner, CBasePlayerWeapon *pLauncher, BOOL Control ) +{ + CWHRocket *pRocket = GetClassPtr( (CWHRocket *)NULL ); + + UTIL_SetOrigin( pRocket, vecOrigin ); + pRocket->pev->angles = vecAngles; + pRocket->m_pLauncher = pLauncher;// remember what RPG fired me. + pRocket->pev->owner = pOwner->edict(); + pRocket->pev->button = Control;//memeber rocket type + pRocket->pev->classname = MAKE_STRING("nuke_rocket"); + pRocket->m_pLauncher->m_cActiveRocket++;//register rocket + pRocket->Spawn(); + pRocket->pev->flags |= FL_PROJECTILE; + + return pRocket; +} + +TYPEDESCRIPTION CWHRocket::m_SaveData[] = +{ + DEFINE_FIELD( CWHRocket, m_pLauncher, FIELD_CLASSPTR ), + DEFINE_FIELD( CWHRocket, m_pPlayer, FIELD_CLASSPTR ), +}; +IMPLEMENT_SAVERESTORE( CWHRocket, CBaseAnimating ); + + +void CWHRocket :: Spawn( void ) +{ + Precache( ); + + m_pPlayer = (CBasePlayer*)CBasePlayer::Instance( pev->owner ); + if(!m_pPlayer)//leveldesigner may put rocket on a map + { + if(!IsMultiplayer()) + { + Msg("Warning! Player pointer is not valid\n"); + m_pPlayer = (CBasePlayer*)UTIL_PlayerByIndex( 1 ); + } + else + { + UTIL_Remove( this ); + return; + } + } + pev->movetype = MOVETYPE_FLY; + pev->solid = SOLID_SLIDEBOX; + pev->takedamage = DAMAGE_YES; + pev->health = 10; + pev->speed = WARHEAD_SPEED;//set initial speed + + UTIL_SetModel(ENT(pev), "models/whrocket.mdl"); + EMIT_SOUND( ENT(pev), CHAN_WEAPON, "weapons/warhead/launch.wav", 1, 0.5 ); + UTIL_SetOrigin( this, pev->origin ); + pev->colormap = ENTINDEX(edict());//manually save our index into colormap + + SetThink( FollowThink ); + SetTouch( NukeTouch ); + + UTIL_MakeVectors( pev->angles ); + pev->angles.x = -(pev->angles.x); + pev->velocity = gpGlobals->v_forward * pev->speed; + m_Center = pev->angles; + + if(pev->button) + { + UTIL_SetView( m_pPlayer, this, CAMERA_ON | INVERSE_X ); + m_pLauncher->m_iOnControl = 1;//start controlling + m_pPlayer->m_iWarHUD = 1;//enable warhead HUD + } + SetNextThink( 0.1 ); +} + +void CWHRocket :: Precache( void ) +{ + UTIL_PrecacheModel("models/whrocket.mdl"); + UTIL_PrecacheSound("weapons/warhead/launch.wav"); + UTIL_PrecacheSound("weapons/warhead/whfly.wav"); + UTIL_PrecacheEntity( "nuclear_explode" );//explode + m_iTrail = UTIL_PrecacheAurora("whtrail"); + m_iBurst = UTIL_PrecacheAurora("whburst"); + b_setup = FALSE; +} + +void CWHRocket :: CreateTrail( void ) +{ + if(b_setup)return;//restore function + EMIT_SOUND( ENT(pev), CHAN_VOICE, "weapons/warhead/whfly.wav", 1, 0.5 ); + UTIL_SetAurora( this, m_iTrail); + UTIL_SetAurora( this, m_iBurst); + pev->renderfx = kRenderFxAurora; + b_setup = TRUE; +} + +void CWHRocket :: FollowThink( void ) +{ + Vector angles, velocity;//private angles & velocity to transform + CreateTrail(); + + if(pev->button)//controllable rocket + { + UTIL_MakeVectorsPrivate( m_pPlayer->pev->v_angle, forward, NULL, NULL ); + + angles = m_pPlayer->pev->v_angle; + angles[0] = 0 - angles[0]; + float steer = WARHEAD_MAX_SPEED / pev->speed;//steer factor + + angles.x = m_Center.x + UTIL_AngleDistance( angles.x, m_Center.x ); + angles.y = m_Center.y + UTIL_AngleDistance( angles.y, m_Center.y ); + angles.z = m_Center.z + UTIL_AngleDistance( angles.z, m_Center.z ); + + float distX = UTIL_AngleDistance( angles.x, pev->angles.x ); + pev->avelocity.x = distX * steer; + + float distY = UTIL_AngleDistance( angles.y, pev->angles.y ); + pev->avelocity.y = distY * steer; + + float distZ = UTIL_AngleDistance( angles.z, pev->angles.z ); + pev->avelocity.z = distY * -steer + distZ * steer; + + pev->velocity = forward * pev->speed + pev->avelocity; + if(m_pLauncher && m_pLauncher->m_iOnControl == 2) + { + Detonate();//check for himself destroy + return; + } + } + CalculateVelocity(); + + //Msg("Speed %.f\n", pev->velocity.Length()); + SetNextThink( 0.1 ); +} + +void CWHRocket::CalculateVelocity ( void ) +{ + if(pev->waterlevel == 3)//go slow underwater + { + if (pev->speed > WARHEAD_SPEED_UNDERWATER) pev->speed -= 30; + GetAttachment( 0, pev->oldorigin, pev->movedir ); + UTIL_BubbleTrail( pev->oldorigin - pev->velocity * 0.1, pev->oldorigin, 4 ); + if( pev->button ) m_pPlayer->m_iWarHUD = 2; + } + else + { + pev->speed += 5;//accelerate rocket + if( pev->button ) m_pPlayer->m_iWarHUD = 1; + } + if(pev->speed > WARHEAD_SPEED)pev->velocity = pev->velocity.Normalize() * WARHEAD_SPEED; +} + +int CWHRocket::TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ) +{ + pev->health -= flDamage;//calculate health + if (pev->health <= 0) Detonate(); + return 1; +} + +void CWHRocket :: NukeTouch ( CBaseEntity *pOther ) +{ + pev->enemy = pOther->edict(); //save enemy + + // check for sky + if( UTIL_PointContents( pev->origin ) & CONTENTS_SKY ) + Detonate( FALSE ); // silent remove in sky + else Detonate(); + + SetNextThink( 0.7f ); +} + +void CWHRocket :: Detonate ( bool explode ) +{ + //NOTE: Player can controlled one rocket at moment + //but non controlled rocket don't reset this indicator + if(pev->button) m_pPlayer->m_iWarHUD = 3;//make static noise + + //launcher callback + if( m_pLauncher ) m_pLauncher->m_cActiveRocket--; + + pev->takedamage = DAMAGE_NO; + pev->velocity = g_vecZero; + pev->solid = SOLID_NOT; + pev->effects |= EF_NODRAW; + pev->model = iStringNull;//invisible + + STOP_SOUND( edict(), CHAN_VOICE, "weapons/warhead/whfly.wav" );//stop flying sound + + if(explode)//make nuclear explosion if needed + { + CNukeExplode::Create( pev->origin, m_pPlayer ); + SetThink( RemoveRocket ); + SetNextThink( 0.7 ); + } + else + { + SetThink( RemoveRocket ); + SetNextThink( 0.7 ); + } +} +void CWHRocket :: RemoveRocket ( void ) +{ + if( m_pLauncher ) m_pLauncher->m_iOnControl = 0; //stop controlling + if(pev->button) + { + m_pPlayer->m_iWarHUD = 0;//reset HUD + UTIL_SetView(m_pPlayer, iStringNull, 0 );//reset view + } + + SetThink( Remove ); + SetNextThink( 0.1 ); +} +LINK_ENTITY_TO_CLASS( nuke_rocket, CWHRocket ); + + +//=========================== +// crossbow bolt +//=========================== +CBolt *CBolt::Create( Vector vecOrigin, Vector vecAngles, CBaseEntity *pOwner ) +{ + // Create a new entity with CBolt private data + CBolt *pBolt = GetClassPtr( (CBolt *)NULL ); + pBolt->pev->classname = MAKE_STRING("bolt"); + pBolt->Spawn(); + pBolt->pev->origin = vecOrigin; + pBolt->pev->angles = vecAngles; + pBolt->pev->owner = pOwner->edict(); + pBolt->pev->avelocity.z = 10; + + return pBolt; +} + +void CBolt::Spawn( ) +{ + Precache( ); + pev->movetype = MOVETYPE_FLY; + pev->solid = SOLID_BBOX; + + pev->gravity = 0.5; + UTIL_SetModel(ENT(pev), "models/crossbow_bolt.mdl"); + + UTIL_SetOrigin( this, pev->origin ); + UTIL_SetSize(pev, Vector(0, 0, 0), Vector(0, 0, 0)); + + EMIT_SOUND(ENT(pev), CHAN_BODY, "weapons/xbow_fire1.wav", 1, ATTN_NORM); + + SetThink( BubbleThink ); + SetNextThink( 0.2 ); +} + +void CBolt::Precache( ) +{ + UTIL_PrecacheModel("models/crossbow_bolt.mdl"); + UTIL_PrecacheSound("weapons/xbow_hitbod1.wav"); + UTIL_PrecacheSound("weapons/xbow_hitbod2.wav"); + UTIL_PrecacheSound("weapons/xbow_hit1.wav"); + UTIL_PrecacheSound("weapons/xbow_fire1.wav"); +} + +void CBolt::Touch( CBaseEntity *pOther ) +{ + SetTouch( NULL ); + + if (pOther->IsMonster() || pOther->IsPlayer()) + { + TraceResult tr = UTIL_GetGlobalTrace( ); + entvars_t *pevOwner; + + pevOwner = VARS( pev->owner ); + ClearMultiDamage( ); + pOther->TraceAttack(pevOwner, BOLT_DMG, pev->velocity.Normalize(), &tr, DMG_NEVERGIB ); + ApplyMultiDamage( pev, pevOwner ); + + pev->velocity = Vector( 0, 0, 0 ); + + // play body "thwack" sound + switch( RANDOM_LONG(0,1) ) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_BODY, "weapons/xbow_hitbod1.wav", 1, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_BODY, "weapons/xbow_hitbod2.wav", 1, ATTN_NORM); break; + } + + Killed( pev, GIB_NEVER ); + } + else + { + EMIT_SOUND_DYN(ENT(pev), CHAN_BODY, "weapons/xbow_hit1.wav", RANDOM_FLOAT(0.95, 1.0), ATTN_NORM, 0, 98 + RANDOM_LONG(0,7)); + + SetNextThink( 0 ); + + Vector vecDir = pev->velocity.Normalize( ); + UTIL_SetOrigin( this, pev->origin - vecDir * 12 ); + pev->angles = UTIL_VecToAngles( vecDir ); + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_FLY; + pev->velocity = Vector( 0, 0, 0 ); + pev->avelocity.z = 0; + pev->angles.z = RANDOM_LONG(0, 360); + + if ( pOther == g_pWorld ) SetThink( PVSRemove ); + else if( pOther->IsBSPModel()) + { + SetParent( pOther );//glue bolt with parent system + SetThink( PVSRemove ); + } + else SetThink( Remove ); + if( !( UTIL_PointContents( pev->origin ) & MASK_WATER )) + UTIL_Sparks( pev->origin ); + } +} + +void CBolt::BubbleThink( void ) +{ + if (pev->waterlevel == 3) UTIL_BubbleTrail( pev->origin - pev->velocity * 0.1, pev->origin, 1 ); + SetNextThink( 0.1 ); +} + +LINK_ENTITY_TO_CLASS( bolt, CBolt ); \ No newline at end of file diff --git a/server/ents/baserockets.h b/server/ents/baserockets.h new file mode 100644 index 00000000..b777c3c6 --- /dev/null +++ b/server/ents/baserockets.h @@ -0,0 +1,110 @@ +//======================================================================= +// Copyright (C) Shambler Team 2006 +// rockets.h - projectiles code +//======================================================================= +#include "baseweapon.h" + +class CGrenade : public CBaseMonster +{ +public: + void Spawn( void ); + + static CGrenade *ShootTimed( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity, float time ); + static CGrenade *ShootContact( entvars_t *pevOwner, Vector vecStart, Vector vecVelocity ); + + void Explode( TraceResult *pTrace, int bitsDamageType ); + + void EXPORT BounceTouch( CBaseEntity *pOther ); + void EXPORT SlideTouch( CBaseEntity *pOther ); + void EXPORT ExplodeTouch( CBaseEntity *pOther ); + void EXPORT DangerSoundThink( void ); + void EXPORT PreDetonate( void ); + void EXPORT Detonate( void ); + void EXPORT DetonateUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT TumbleThink( void ); + + virtual void BounceSound( void ); + virtual int BloodColor( void ) { return DONT_BLEED; } + virtual void Killed( entvars_t *pevAttacker, int iGib ); + + BOOL m_fRegisteredSound;// whether or not this grenade has issued its DANGER sound to the world sound list yet. +}; + +class CRpgRocket : public CGrenade +{ +public: + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + void Spawn( void ); + void Precache( void ); + void EXPORT FollowThink( void ); + void EXPORT IgniteThink( void ); + void EXPORT RocketTouch( CBaseEntity *pOther ); + void Detonate( void ); + void CreateTrail( void ); + static CRpgRocket *Create ( Vector vecOrigin, Vector vecAngles, CBaseEntity *pOwner, CBasePlayerWeapon *pLauncher ); + + int m_iTrail; + float m_flIgniteTime; + CBasePlayerWeapon *m_pLauncher;// pointer back to the launcher that fired me. + BOOL b_setup; +}; + +class CApacheHVR : public CGrenade +{ + void Spawn( void ); + void Precache( void ); + void EXPORT IgniteThink( void ); + void EXPORT AccelerateThink( void ); + + int m_iTrail; +}; + +class CNukeExplode : public CBaseLogic +{ +public: + void Spawn( void ); + void Precache( void ); + void EXPORT ExplodeThink( void ); + static CNukeExplode *Create ( Vector vecOrigin, CBaseEntity *pOwner ); + short m_usExplodeSprite; + short m_usExplodeSprite2; +}; + +class CWHRocket : public CBaseAnimating +{ +public: + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + void Spawn( void ); + void Precache( void ); + void CreateTrail( void ); + void EXPORT FollowThink( void ); + void EXPORT NukeTouch( CBaseEntity *pOther ); + void EXPORT RemoveRocket ( void ); + void CalculateVelocity ( void ); + void Detonate ( bool explode = 1 ); + static CWHRocket *Create ( Vector vecOrigin, Vector vecAngles, CBaseEntity *pOwner, CBasePlayerWeapon *pLauncher, BOOL Control ); + int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ); + + int m_iTrail, m_iBurst;// rocket trail + CBasePlayerWeapon *m_pLauncher;// pointer back to the launcher that fired me. + CBasePlayer *m_pPlayer; + Vector m_Center, forward; + BOOL b_setup; +}; + +class CBolt : public CBaseAnimating +{ +public: + void Spawn( void ); + void Precache( void ); + int ObjectCaps( void ) { return FCAP_DONT_SAVE; } + void EXPORT BubbleThink( void ); + void Touch( CBaseEntity *pOther ); + static CBolt *Create( Vector vecOrigin, Vector vecAngles, CBaseEntity *pOwner ); + + int m_iTrail; +}; \ No newline at end of file diff --git a/server/ents/basesprite.h b/server/ents/basesprite.h new file mode 100644 index 00000000..2607df5b --- /dev/null +++ b/server/ents/basesprite.h @@ -0,0 +1,141 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#ifndef BASESPRITE_H +#define BASESPRITE_H + +#define SF_TEMPSPRITE 0x8000 //play sequence and die +#include "baseworld.h" + +class CSprite : public CBaseLogic +{ +public: + void Spawn( void ); + void Precache( void ) { UTIL_PrecacheModel( pev->model ); } + void PostActivate( void ) + { + //env_glow always enabled + if (FClassnameIs( pev, "env_glow" ) || FStringNull(pev->targetname) || pev->spawnflags & SF_START_ON) + { + TurnOn(); + ClearBits( pev->spawnflags, SF_START_ON ); + } + } + + void Think( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + void Move( void ); + void PostSpawn( void ); + void Animate( float frames ) + { + if(Frames() > 1) + { + pev->frame += frames; + if ( pev->frame > Frames() ) + { + if( pev->spawnflags & SF_FIREONCE ) TurnOff(); + else if ( Frames() > 0 ) pev->frame = fmod( pev->frame, Frames() ); + } + } + } + void TurnOff( void ); + void TurnOn( void ); + float Frames( void ) { return MODEL_FRAMES( pev->modelindex ) - 1; } + inline void SetTransparency( int rendermode, int r, int g, int b, int a, int fx ) + { + pev->rendermode = rendermode; + pev->rendercolor.x = r; + pev->rendercolor.y = g; + pev->rendercolor.z = b; + pev->renderamt = a; + pev->renderfx = fx; + } + inline void SetTexture( int spriteIndex ) { pev->modelindex = spriteIndex; } + inline void SetScale( float scale ) { pev->scale = scale; } + inline void SetBrightness( int brightness ) { pev->renderamt = brightness; } + + inline void AnimateAndDie( float framerate ) + { + SetBits( pev->spawnflags, SF_TEMPSPRITE ); + pev->framerate = framerate; + pev->pain_finished = gpGlobals->time + (Frames() / framerate); + SetNextThink( 0 ); + } + static CSprite *SpriteCreate( const char *pSpriteName, const Vector &origin, BOOL animate ) + { + CSprite *pSprite = GetClassPtr( (CSprite *)NULL ); + pSprite->pev->classname = MAKE_STRING("env_sprite"); + pSprite->pev->model = MAKE_STRING(pSpriteName); + pSprite->pev->origin = origin; + pSprite->Spawn(); + if ( animate ) pSprite->TurnOn(); + else ClearBits( pSprite->pev->effects, EF_NODRAW ); + + return pSprite; + } +}; + +class CShot : public CSprite +{ +public: + void Touch ( CBaseEntity *pOther ) + { + if (pev->teleport_time > gpGlobals->time) return; + pev->teleport_time = gpGlobals->time + 0.1; + if ( pOther != g_pWorld) UTIL_FireTargets( pev->target, pOther, this, USE_ON ); + else UTIL_FireTargets( pev->target, pOther, this, USE_SET ); //world touch + } + static CShot *CreateShot ( const char *szGibModel, Vector size ) + { + CShot *pShot = GetClassPtr( (CShot*)NULL ); + pShot->pev->classname = MAKE_STRING("shot"); + pShot->pev->solid = SOLID_SLIDEBOX; + UTIL_SetModel(ENT(pShot->pev), szGibModel ); + UTIL_SetSize(pShot->pev, -size, size ); + return pShot; + } +}; + +class CGib : public CBaseEntity +{ +public: + void Spawn( const char *szGibModel ); + void EXPORT BounceGibTouch ( CBaseEntity *pOther ); + void EXPORT StickyGibTouch ( CBaseEntity *pOther ); + void EXPORT WaitTillLand( void ); + + virtual int ObjectCaps( void ) { return (CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION) | FCAP_DONT_SAVE; } + static CGib *CreateGib ( CBaseEntity *pVictim, const char *szGibModel, int gibtype ); + + //spawn gibs + static void SpawnHeadGib( CBaseEntity *pVictim, string_t m_iGibModel = NULL ) + { + if(FStringNull( m_iGibModel )) CreateGib( pVictim, "models/hgibs.mdl", 1 ); + else CreateGib( pVictim, STRING( m_iGibModel ), 1 ); + } + static void SpawnRandomGibs( CBaseEntity *pVictim, int cGibs, int human, string_t m_iGibModel = NULL ) + { + const char *model; + if(FStringNull( m_iGibModel )) + { + if(human) model = "models/hgibs.mdl"; + else model = "models/agibs.mdl"; + } + else model = (char *)STRING( m_iGibModel ); + for(int i = 0; i < cGibs; i++) CreateGib( pVictim, model, 0 ); + } + static void SpawnStickyGibs( CBaseEntity *pVictim, int cGibs, string_t m_iGibModel = NULL ) + { + if(!FStringNull( m_iGibModel )) for(int i = 0; i < cGibs; i++) CreateGib( pVictim, STRING( m_iGibModel ), 2 ); + else for(int i = 0; i < cGibs; i++) CreateGib( pVictim, "models/stickygib.mdl", 2 ); + } + + int m_bloodColor; + int m_cBloodDecals; + int m_material; + float m_lifeTime; +}; + +#endif //BASESPRITE_H \ No newline at end of file diff --git a/server/ents/basetank.cpp b/server/ents/basetank.cpp new file mode 100644 index 00000000..dcb7ddf4 --- /dev/null +++ b/server/ents/basetank.cpp @@ -0,0 +1,1154 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "sfx.h" +#include "cbase.h" +#include "basebeams.h" +#include "baseweapon.h" +#include "basetank.h" +#include "monsters.h" + +TYPEDESCRIPTION CFuncTank::m_SaveData[] = +{ + DEFINE_FIELD( CFuncTank, m_yawCenter, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_yawRate, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_yawRange, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_yawTolerance, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_pitchCenter, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_pitchRate, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_pitchRange, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_pitchTolerance, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_fireLast, FIELD_TIME ), + DEFINE_FIELD( CFuncTank, m_fireRate, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_lastSightTime, FIELD_TIME ), + DEFINE_FIELD( CFuncTank, m_persist, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_Range, FIELD_RANGE ), + DEFINE_FIELD( CFuncTank, m_barrelPos, FIELD_VECTOR ), + DEFINE_FIELD( CFuncTank, m_spriteScale, FIELD_FLOAT ), + DEFINE_FIELD( CFuncTank, m_iszSpriteSmoke, FIELD_STRING ), + DEFINE_FIELD( CFuncTank, m_iszSpriteFlash, FIELD_STRING ), + DEFINE_FIELD( CFuncTank, m_bulletType, FIELD_INTEGER ), + DEFINE_FIELD( CFuncTank, m_sightOrigin, FIELD_VECTOR ), + DEFINE_FIELD( CFuncTank, m_spread, FIELD_INTEGER ), + DEFINE_FIELD( CFuncTank, m_pControls, FIELD_CLASSPTR ), //LRC + DEFINE_FIELD( CFuncTank, m_pSpot, FIELD_CLASSPTR ), //LRC + DEFINE_FIELD( CFuncTank, m_flNextAttack, FIELD_TIME ), + DEFINE_FIELD( CFuncTank, m_iBulletDamage, FIELD_INTEGER ), + DEFINE_FIELD( CFuncTank, m_iszMaster, FIELD_STRING ), + DEFINE_FIELD( CFuncTank, m_iszFireMaster, FIELD_STRING ), //LRC +}; +IMPLEMENT_SAVERESTORE( CFuncTank, CBaseEntity ); + +void CFuncTank :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "yawrate")) + { + m_yawRate = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "yawrange")) + { + m_yawRange = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "yawtolerance")) + { + m_yawTolerance = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "pitchrange")) + { + m_pitchRange = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "pitchrate")) + { + m_pitchRate = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "pitchtolerance")) + { + m_pitchTolerance = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "firerate")) + { + m_fireRate = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "barrelend")) + { + pev->netname = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "spritescale")) + { + m_spriteScale = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "spritesmoke")) + { + m_iszSpriteSmoke = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "spriteflash")) + { + m_iszSpriteFlash = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "rotatesound")) + { + pev->noise = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "persistence")) + { + m_persist = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "bullet")) + { + m_bulletType = (TANKBULLET)atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if ( FStrEq(pkvd->szKeyName, "bullet_damage" )) + { + m_iBulletDamage = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "firespread")) + { + m_spread = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "Range")) + { + m_Range = RandomRange(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "master")) + { + m_iszMaster = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "firemaster")) + { + m_iszFireMaster = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iClass")) + { + m_iTankClass = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CFuncTank :: Spawn( void ) +{ + Precache(); + + pev->movetype = MOVETYPE_PUSH; // so it doesn't get pushed by anything + pev->solid = SOLID_BSP; + UTIL_SetModel( ENT(pev), pev->model ); + SetBits (pFlags, PF_ANGULAR); + + if ( IsActive() ) + { + SetNextThink(1.0); + } + + if (!m_iTankClass) + { + m_iTankClass = 0; + } + + if(m_Range.m_flMax == 0) m_Range.m_flMax = 4096; + + if ( m_fireRate <= 0 ) m_fireRate = 1; + if ( m_spread > MAX_FIRING_SPREADS ) m_spread = 0; + + pev->oldorigin = pev->origin; + pev->flags |= FL_TANK;//this is tank entity +} + +void CFuncTank::PostSpawn( void ) +{ + if (m_pParent) + { + m_yawCenter = pev->angles.y - m_pParent->pev->angles.y; + m_pitchCenter = pev->angles.x - m_pParent->pev->angles.x; + } + else + { + m_yawCenter = pev->angles.y; + m_pitchCenter = pev->angles.x; + } + + CBaseEntity *pTarget = UTIL_FindEntityByTargetname(NULL, STRING(pev->netname), NULL ); + if(pTarget) + { + m_barrelPos = pTarget->pev->origin - pev->origin; //write offset + UTIL_Remove( pTarget ); + } + else if(!IsLaserTank()) //get "laserentity" position for tanklaser + { + Msg("Error! %s: %s haven't barrel end entity! Use default values.\n", STRING(pev->classname), STRING(pev->targetname)); + m_barrelPos = Vector( fabs(pev->absmax.x), 0, 0 ); + } + m_sightOrigin = BarrelPosition(); // Point at the end of the barrel +} + +void CFuncTank :: Precache( void ) +{ + if ( m_iszSpriteSmoke ) + UTIL_PrecacheModel( m_iszSpriteSmoke ); + if ( m_iszSpriteFlash ) + UTIL_PrecacheModel( m_iszSpriteFlash ); + if ( pev->noise ) + UTIL_PrecacheSound( pev->noise ); +} + +BOOL CFuncTank :: StartControl( CBasePlayer* pController, CFuncTankControls *pControls ) +{ + // we're already being controlled or playing a sequence + if ( m_pControls != NULL ) + return FALSE; + + // Team only or disabled? + if ( m_iszMaster ) + { + if ( !UTIL_IsMasterTriggered( m_iszMaster, pController ) ) + return FALSE; + } + + m_iState = STATE_ON; + m_pControls = pControls; + + if (m_pSpot) m_pSpot->Revive(); + + SetNextThink(0.3); + return TRUE; +} + +void CFuncTank :: StopControl( CFuncTankControls* pControls) +{ + if ( !m_pControls || m_pControls != pControls) + { + return; + } + m_iState = STATE_OFF; + + if (m_pSpot) m_pSpot->Suspend(-1); + StopRotSound(); //LRC + + DontThink(); + UTIL_SetAvelocity(this, g_vecZero); + m_pControls = NULL; + + if ( IsActive() ) + { + SetNextThink(1.0); + } +} + +void CFuncTank::UpdateSpot( void ) +{ + if ( pev->spawnflags & SF_TANK_LASERSPOT ) + { + + if (!m_pSpot) m_pSpot = CLaserSpot::CreateSpot(); + + Vector vecAiming; + UTIL_MakeVectorsPrivate( pev->angles, vecAiming, NULL, NULL ); + Vector vecSrc = BarrelPosition( ); + + TraceResult tr; + UTIL_TraceLine ( vecSrc, vecSrc + vecAiming * 8192, dont_ignore_monsters, ENT(pev), &tr ); + UTIL_SetOrigin( m_pSpot, tr.vecEndPos ); + } +} + +void CFuncTank :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if ( pev->spawnflags & SF_TANK_CANCONTROL ) + { + if ( pActivator->Classify() != CLASS_PLAYER ) return; + } + else + { + if (useType == USE_TOGGLE) + { + if(IsActive()) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + TankActivate(); + if (m_pSpot) m_pSpot->Revive(); + } + else if (useType == USE_OFF) + { + TankDeactivate(); + if (m_pSpot) m_pSpot->Suspend(-1); + } + } +} + +CBaseEntity *CFuncTank:: BestVisibleEnemy ( void ) +{ + CBaseEntity *pReturn; + int iNearest; + int iDist; + int iBestRelationship; + int iLookDist = m_Range.m_flMax ? m_Range.m_flMax : 512; //thanks to Waldo for this. + + iNearest = 8192;// so first visible entity will become the closest. + pReturn = NULL; + iBestRelationship = R_DL; + + CBaseEntity *pList[100]; + + Vector delta = Vector( iLookDist, iLookDist, iLookDist ); + + // Find only monsters/clients in box, NOT limited to PVS + int count = UTIL_EntitiesInBox( pList, 100, pev->origin - delta, pev->origin + delta, FL_CLIENT|FL_MONSTER ); + int i; + + for (i = 0; i < count; i++ ) + { + if ( pList[i]->IsAlive() ) + { + if ( IRelationship( pList[i] ) > iBestRelationship ) + { + // this entity is disliked MORE than the entity that we + // currently think is the best visible enemy. No need to do + // a distance check, just get mad at this one for now. + iBestRelationship = IRelationship ( pList[i] ); + iNearest = ( pList[i]->pev->origin - pev->origin ).Length(); + pReturn = pList[i]; + } + else if ( IRelationship( pList[i] ) == iBestRelationship ) + { + // this entity is disliked just as much as the entity that + // we currently think is the best visible enemy, so we only + // get mad at it if it is closer. + iDist = ( pList[i]->pev->origin - pev->origin ).Length(); + + if ( iDist <= iNearest ) + { + iNearest = iDist; + //these are guaranteed to be the same! iBestRelationship = IRelationship ( pList[i] ); + pReturn = pList[i]; + } + } + } + } + return pReturn; +} + + +int CFuncTank::IRelationship( CBaseEntity* pTarget ) +{ + int iOtherClass = pTarget->Classify(); + if (iOtherClass == CLASS_NONE) return R_NO; + + if (!m_iTankClass) + { + if (iOtherClass == CLASS_PLAYER) + return R_HT; + else + return R_NO; + } + else if (m_iTankClass == CLASS_PLAYER_ALLY) + { + switch (iOtherClass) + { + case CLASS_HUMAN_MILITARY: + case CLASS_MACHINE: + case CLASS_ALIEN_MILITARY: + case CLASS_ALIEN_MONSTER: + case CLASS_ALIEN_PREDATOR: + case CLASS_ALIEN_PREY: + return R_HT; + default: + return R_NO; + } + } + else if (m_iTankClass == CLASS_HUMAN_MILITARY) + { + switch (iOtherClass) + { + case CLASS_PLAYER: + case CLASS_PLAYER_ALLY: + case CLASS_ALIEN_MILITARY: + case CLASS_ALIEN_MONSTER: + case CLASS_ALIEN_PREDATOR: + case CLASS_ALIEN_PREY: + return R_HT; + case CLASS_HUMAN_PASSIVE: + return R_DL; + default: + return R_NO; + } + } + else if (m_iTankClass == CLASS_ALIEN_MILITARY) + { + switch (iOtherClass) + { + case CLASS_PLAYER: + case CLASS_PLAYER_ALLY: + case CLASS_HUMAN_MILITARY: + return R_HT; + case CLASS_HUMAN_PASSIVE: + return R_DL; + default: + return R_NO; + } + } + else return R_NO; +} + +BOOL CFuncTank :: InRange( float range ) +{ + if ( range < m_Range.m_flMin ) + return FALSE; + if ( m_Range.m_flMax > 0 && range > m_Range.m_flMax ) + return FALSE; + + return TRUE; +} + +void CFuncTank :: Think( void ) +{ + TrackTarget(); + + if ( fabs(pev->avelocity.x) > 1 || fabs(pev->avelocity.y) > 1 ) + StartRotSound(); + else StopRotSound(); +} + +void CFuncTank::TrackTarget( void ) +{ + TraceResult tr; + BOOL updateTime = FALSE, lineOfSight; + Vector angles, direction, targetPosition, barrelEnd; + Vector v_right, v_up; + CBaseEntity *pTarget; + CBasePlayer* pController = NULL; + + if (m_pControls && m_pControls->m_pController) + { + UpdateSpot(); + pController = m_pControls->m_pController; + pController->pev->viewmodel = 0; + SetNextThink(0.05); + + if (pev->spawnflags & SF_TANK_MATCHTARGET) + { + // "Match target" mode: + // first, get the player's angles + angles = pController->pev->v_angle; + // Work out what point the player is looking at + UTIL_MakeVectorsPrivate(angles, direction, NULL, NULL); + + targetPosition = pController->EyePosition() + direction * 1000; + + edict_t *ownerTemp = pev->owner; //LRC store the owner, so we can put it back after the check + pev->owner = pController->edict(); //LRC when doing the matchtarget check, don't hit the player or the tank. + + UTIL_TraceLine( + pController->EyePosition(), + targetPosition, + missile, //the opposite of ignore_monsters: target them if we go anywhere near! + ignore_glass, + edict(), &tr + ); + + pev->owner = ownerTemp; //LRC put the owner back + + // Work out what angle we need to face to look at that point + direction = tr.vecEndPos - pev->origin; + angles = UTIL_VecToAngles( direction ); + targetPosition = tr.vecEndPos; + + // Calculate the additional rotation to point the end of the barrel at the target + // (instead of the gun's center) + AdjustAnglesForBarrel( angles, direction.Length() ); + } + else + { + // "Match angles" mode + // just get the player's angles + angles = pController->pev->v_angle; + angles[0] = 0 - angles[0]; + + UpdateSpot(); + SetNextThink(0.05);//G-Cont.For more smoothing motion a laser spot + } + } + else + { + if ( IsActive() ) + { + SetNextThink(0.1); + } + else + { + DontThink(); + UTIL_SetAvelocity(this, g_vecZero); + return; + } + + UpdateSpot(); + + // if we can't see any players + pTarget = BestVisibleEnemy(); + if ( FNullEnt( pTarget ) ) + { + if ( IsActive() ) SetNextThink(2); // No enemies visible, wait 2 secs + return; + } + + // Calculate angle needed to aim at target + barrelEnd = BarrelPosition(); + targetPosition = pTarget->pev->origin + pTarget->pev->view_ofs; + float range = (targetPosition - barrelEnd).Length(); + + if ( !InRange( range ) ) + return; + + UTIL_TraceLine( barrelEnd, targetPosition, dont_ignore_monsters, edict(), &tr ); + + if ( tr.flFraction == 1.0 || tr.pHit == ENT(pTarget->pev) ) + { + lineOfSight = TRUE; + + if ( InRange( range ) && pTarget->IsAlive() ) + { + updateTime = TRUE; // I think I saw him, pa! + m_sightOrigin = UpdateTargetPosition( pTarget ); + } + } + else + { + // No line of sight, don't track + lineOfSight = FALSE; + } + + direction = m_sightOrigin - pev->origin; + angles = UTIL_VecToAngles( direction ); + AdjustAnglesForBarrel( angles, direction.Length() ); + } + + angles.x = -angles.x; + + float currentYawCenter, currentPitchCenter; + + // Force the angles to be relative to the center position + if (m_pParent) + { + currentYawCenter = m_yawCenter + m_pParent->pev->angles.y; + currentPitchCenter = m_pitchCenter + m_pParent->pev->angles.x; + } + else + { + currentYawCenter = m_yawCenter; + currentPitchCenter = m_pitchCenter; + } + + angles.y = currentYawCenter + UTIL_AngleDistance( angles.y, currentYawCenter ); + angles.x = currentPitchCenter + UTIL_AngleDistance( angles.x, currentPitchCenter ); + + // Limit against range in y + if (m_yawRange < 360) + { + if ( angles.y > currentYawCenter + m_yawRange ) + { + angles.y = currentYawCenter + m_yawRange; + updateTime = FALSE; // If player is outside fire arc, we didn't really see him + } + else if ( angles.y < (currentYawCenter - m_yawRange) ) + { + angles.y = (currentYawCenter - m_yawRange); + updateTime = FALSE; // If player is outside fire arc, we didn't really see him + } + } + // we can always 'see' the whole vertical arc, so it's just the yaw we needed to check. + + if ( updateTime ) + m_lastSightTime = gpGlobals->time; + + // Move toward target at rate or less + float distY = UTIL_AngleDistance( angles.y, pev->angles.y ); + Vector setAVel = g_vecZero; + + setAVel.y = distY * 10; + if ( setAVel.y > m_yawRate ) setAVel.y = m_yawRate; + else if ( setAVel.y < -m_yawRate ) setAVel.y = -m_yawRate; + + // Limit against range in x + if ( angles.x > currentPitchCenter + m_pitchRange ) angles.x = currentPitchCenter + m_pitchRange; + else if ( angles.x < currentPitchCenter - m_pitchRange ) angles.x = currentPitchCenter - m_pitchRange; + + // Move toward target at rate or less + float distX = UTIL_AngleDistance( angles.x, pev->angles.x ); + setAVel.x = distX * 10; + + if ( setAVel.x > m_pitchRate ) setAVel.x = m_pitchRate; + else if ( setAVel.x < -m_pitchRate ) setAVel.x = -m_pitchRate; + + UTIL_SetAvelocity(this, setAVel); + + // notify the TankSequence if we're (pretty close to) facing the target + + // firing with player-controlled tanks: + if ( pController ) + { + if ( gpGlobals->time < m_flNextAttack ) + return; + + if ( pController->pev->button & IN_ATTACK ) + { + Vector forward; + UTIL_MakeVectorsPrivate( pev->angles, forward, NULL, NULL ); + + // to make sure the gun doesn't fire too many bullets + m_fireLast = gpGlobals->time - (1/m_fireRate) - 0.01; + + TryFire( BarrelPosition(), forward, pController->pev ); + + if ( pController && pController->IsPlayer() ) + ((CBasePlayer *)pController)->m_iWeaponVolume = LOUD_GUN_VOLUME; + + m_flNextAttack = gpGlobals->time + (1/m_fireRate); + } + else m_iState = STATE_ON; + } + // firing with automatic guns: + else if ( CanFire() && ( (fabs(distX) < m_pitchTolerance && fabs(distY) < m_yawTolerance) || (pev->spawnflags & SF_TANK_LINEOFSIGHT) ) ) + { + BOOL fire = FALSE; + Vector forward; + UTIL_MakeVectorsPrivate( pev->angles, forward, NULL, NULL ); + + if ( pev->spawnflags & SF_TANK_LINEOFSIGHT ) + { + float length = direction.Length(); + UTIL_TraceLine( barrelEnd, barrelEnd + forward * length, dont_ignore_monsters, edict(), &tr ); + if ( tr.pHit == ENT(pTarget->pev) ) + fire = TRUE; + } + else fire = TRUE; + + if ( fire ) + { + TryFire( BarrelPosition(), forward, pev ); + } + else + { + m_fireLast = 0; + } + } + else m_fireLast = 0; +} + +void CFuncTank::AdjustAnglesForBarrel( Vector &angles, float distance ) +{ + float r2, d2; + + + if ( m_barrelPos.y != 0 || m_barrelPos.z != 0 ) + { + distance -= m_barrelPos.z; + d2 = distance * distance; + if ( m_barrelPos.y ) + { + r2 = m_barrelPos.y * m_barrelPos.y; + angles.y += (180.0 / M_PI) * atan2( m_barrelPos.y, sqrt( d2 - r2 ) ); + } + if ( m_barrelPos.z ) + { + r2 = m_barrelPos.z * m_barrelPos.z; + angles.x += (180.0 / M_PI) * atan2( -m_barrelPos.z, sqrt( d2 - r2 ) ); + } + } +} + +// Check the FireMaster before actually firing +void CFuncTank::TryFire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ) +{ + m_iState = STATE_IN_USE; + if (UTIL_IsMasterTriggered(m_iszFireMaster, NULL)) + { + Fire( barrelEnd, forward, pevAttacker ); + } +} + +// Fire targets and spawn sprites +void CFuncTank::Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ) +{ + if ( m_fireLast != 0 ) + { + if ( m_iszSpriteSmoke ) + { + CSprite *pSprite = CSprite::SpriteCreate( STRING(m_iszSpriteSmoke), barrelEnd, TRUE ); + pSprite->AnimateAndDie( RANDOM_FLOAT( 15.0, 20.0 ) ); + pSprite->SetTransparency( kRenderTransAlpha, pev->rendercolor.x, pev->rendercolor.y, pev->rendercolor.z, 255, kRenderFxNone ); + pSprite->pev->velocity.z = RANDOM_FLOAT(40, 80); + pSprite->SetScale( m_spriteScale ); + } + if ( m_iszSpriteFlash ) + { + CSprite *pSprite = CSprite::SpriteCreate( STRING(m_iszSpriteFlash), barrelEnd, TRUE ); + pSprite->AnimateAndDie( 60 ); + pSprite->SetTransparency( kRenderTransAdd, 255, 255, 255, 255, kRenderFxNoDissipation ); + pSprite->SetScale( m_spriteScale ); + } + UTIL_FireTargets( pev->target, this, this, USE_TOGGLE ); + } + m_fireLast = gpGlobals->time; +} + +void CFuncTank::TankTrace( const Vector &vecStart, const Vector &vecForward, const Vector &vecSpread, TraceResult &tr ) +{ + // get circular gaussian spread + float x, y, z; + do { + x = RANDOM_FLOAT(-0.5,0.5) + RANDOM_FLOAT(-0.5,0.5); + y = RANDOM_FLOAT(-0.5,0.5) + RANDOM_FLOAT(-0.5,0.5); + z = x*x+y*y; + } while (z > 1); + Vector vecDir = vecForward + + x * vecSpread.x * gpGlobals->v_right + + y * vecSpread.y * gpGlobals->v_up; + Vector vecEnd; + + vecEnd = vecStart + vecDir * 4096; + UTIL_TraceLine( vecStart, vecEnd, dont_ignore_monsters, edict(), &tr ); +} + + +void CFuncTank::StartRotSound( void ) +{ + if ( !pev->noise || (pev->spawnflags & SF_TANK_SOUNDON) ) + return; + pev->spawnflags |= SF_TANK_SOUNDON; + EMIT_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noise), 0.85, ATTN_NORM); +} + + +void CFuncTank::StopRotSound( void ) +{ + if ( pev->spawnflags & SF_TANK_SOUNDON ) + STOP_SOUND( edict(), CHAN_STATIC, (char*)STRING(pev->noise) ); + pev->spawnflags &= ~SF_TANK_SOUNDON; +} + +//============================================================================ +// FUNC TANK +//============================================================================ +class CFuncTankGun : public CFuncTank +{ +public: + void Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ); +}; +LINK_ENTITY_TO_CLASS( func_tank, CFuncTankGun ); + +void CFuncTankGun::Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ) +{ + int i; + + if ( m_fireLast != 0 ) + { + // FireBullets needs gpGlobals->v_up, etc. + UTIL_MakeAimVectors(pev->angles); + + int bulletCount = (gpGlobals->time - m_fireLast) * m_fireRate; + if ( bulletCount > 0 ) + { + for ( i = 0; i < bulletCount; i++ ) + { + switch( m_bulletType ) + { + case TANK_BULLET_9MM: + FireBullets( 1, barrelEnd, forward, gTankSpread[m_spread], 4096, BULLET_9MM, 1, m_iBulletDamage, pevAttacker ); + break; + + case TANK_BULLET_MP5: + FireBullets( 1, barrelEnd, forward, gTankSpread[m_spread], 4096, BULLET_MP5, 1, m_iBulletDamage, pevAttacker ); + break; + + case TANK_BULLET_12MM: + FireBullets( 1, barrelEnd, forward, gTankSpread[m_spread], 4096, BULLET_12MM, 1, m_iBulletDamage, pevAttacker ); + break; + + default: + case TANK_BULLET_NONE: + break; + } + } + CFuncTank::Fire( barrelEnd, forward, pevAttacker ); + } + } + else CFuncTank::Fire( barrelEnd, forward, pevAttacker ); +} + + +//============================================================================ +// FUNC TANK LASER +//============================================================================ +class CFuncTankLaser : public CFuncTank +{ +public: + void Activate( void ); + void KeyValue( KeyValueData *pkvd ); + void Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ); + BOOL IsLaserTank( void ) { return TRUE; } + void Think( void ); + void PostActivate( void ); + void PostSpawn( void ); + CLaser *GetLaser( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + +private: + CLaser *m_pLaser; + float m_laserTime; +}; +LINK_ENTITY_TO_CLASS( func_tanklaser, CFuncTankLaser ); + +TYPEDESCRIPTION CFuncTankLaser::m_SaveData[] = +{ + DEFINE_FIELD( CFuncTankLaser, m_pLaser, FIELD_CLASSPTR ), + DEFINE_FIELD( CFuncTankLaser, m_laserTime, FIELD_TIME ), +}; + +IMPLEMENT_SAVERESTORE( CFuncTankLaser, CFuncTank ); + +void CFuncTankLaser::Activate( void ) +{ + if ( !GetLaser() ) + { + UTIL_Remove(this); + Msg( "Error: Laser tank with no env_laser!\n" ); + } + else + { + m_pLaser->TurnOff(); + } + CFuncTank::Activate(); +} + +void CFuncTankLaser::PostSpawn( void ) +{ + CFuncTank::PostSpawn(); + + if(m_barrelPos == g_vecZero) //check for custom barrelend + { + if(GetLaser()) m_barrelPos = m_pLaser->pev->origin - pev->origin; //write offset + else m_barrelPos = Vector( fabs(pev->absmax.x), 0, 0 ); //write default + m_sightOrigin = BarrelPosition(); // Point at the end of the barrel + } +} + +void CFuncTankLaser::PostActivate( void ) +{ + //set laser parent once only + if(m_pLaser && m_pLaser->m_pParent != this) + { + m_pLaser->SetParent( this ); + } +} + +void CFuncTankLaser::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "laserentity")) + { + pev->message = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CFuncTank::KeyValue( pkvd ); +} + + +CLaser *CFuncTankLaser::GetLaser( void ) +{ + if ( m_pLaser ) return m_pLaser; + + CBaseEntity *pEntity; + + pEntity = UTIL_FindEntityByTargetname( NULL, STRING(pev->message) ); + while ( pEntity ) + { + // Found the laser + if ( FClassnameIs( pEntity->pev, "env_laser" ) ) + { + m_pLaser = (CLaser *)pEntity; + m_pLaser->pev->angles = pev->angles; //copy tank angles + break; + } + else pEntity = UTIL_FindEntityByTargetname( pEntity, STRING(pev->message) ); + } + + return m_pLaser; +} + + +void CFuncTankLaser::Think( void ) +{ + if ( m_pLaser && m_pLaser->GetState() == STATE_ON && (gpGlobals->time > m_laserTime)) + m_pLaser->TurnOff(); + CFuncTank::Think(); +} + +void CFuncTankLaser::Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ) +{ + int i; + TraceResult tr; + + if ( m_fireLast != 0 && GetLaser() ) + { + // TankTrace needs gpGlobals->v_up, etc. + UTIL_MakeAimVectors(pev->angles); + + int bulletCount = (gpGlobals->time - m_fireLast) * m_fireRate; + if ( bulletCount ) + { + for ( i = 0; i < bulletCount; i++ ) + { + m_pLaser->pev->origin = barrelEnd; + TankTrace( barrelEnd, forward, gTankSpread[m_spread], tr ); + + m_laserTime = gpGlobals->time + 0.1; + m_pLaser->TurnOn(); + m_pLaser->pev->dmgtime = gpGlobals->time - 1.0; + } + CFuncTank::Fire( barrelEnd, forward, pev ); + } + } + else CFuncTank::Fire( barrelEnd, forward, pev ); + +} + +//============================================================================ +// FUNC TANK ROCKET +//============================================================================ +class CFuncTankRocket : public CFuncTank +{ +public: + void Precache( void ); + void Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ); + BOOL IsRocketTank( void ) { return TRUE; } +}; +LINK_ENTITY_TO_CLASS( func_tankrocket, CFuncTankRocket ); + +void CFuncTankRocket::Precache( void ) +{ + UTIL_PrecacheEntity( "rpg_rocket" ); + CFuncTank::Precache(); +} + +void CFuncTankRocket::Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ) +{ + int i; + + if ( m_fireLast != 0 ) + { + int bulletCount = (gpGlobals->time - m_fireLast) * m_fireRate; + if ( bulletCount > 0 ) + { + for ( i = 0; i < bulletCount; i++ ) + { + CBaseEntity *pRocket = CBaseEntity::Create( "rpg_rocket", barrelEnd, pev->angles, edict() ); + } + CFuncTank::Fire( barrelEnd, forward, pev ); + } + } + else CFuncTank::Fire( barrelEnd, forward, pev ); +} + +//============================================================================ +// FUNC TANK MORTAR +//============================================================================ +class CFuncTankMortar : public CFuncTank +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ); +}; +LINK_ENTITY_TO_CLASS( func_tankmortar, CFuncTankMortar ); + + +void CFuncTankMortar::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "iMagnitude")) + { + pev->impulse = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else + CFuncTank::KeyValue( pkvd ); +} + + +void CFuncTankMortar::Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ) +{ + if ( m_fireLast != 0 ) + { + int bulletCount = (gpGlobals->time - m_fireLast) * m_fireRate; + // Only create 1 explosion + if ( bulletCount > 0 ) + { + TraceResult tr; + + // TankTrace needs gpGlobals->v_up, etc. + UTIL_MakeAimVectors(pev->angles); + + TankTrace( barrelEnd, forward, gTankSpread[m_spread], tr ); + + UTIL_Explode( tr.vecEndPos, edict(), pev->impulse ); + CFuncTank::Fire( barrelEnd, forward, pev ); + } + } + else CFuncTank::Fire( barrelEnd, forward, pev ); +} + + +//============================================================================ +// FUNC TANK CONTROLS +//============================================================================ +void CFuncTankControls::Spawn( void ) +{ + pev->solid = SOLID_TRIGGER; + pev->movetype = MOVETYPE_NONE; + pev->effects |= EF_NODRAW; + UTIL_SetModel( ENT(pev), pev->model ); + + UTIL_SetSize( pev, pev->mins, pev->maxs ); + UTIL_SetOrigin( this, pev->origin ); +} + +void CFuncTankControls :: KeyValue( KeyValueData *pkvd ) +{ + if ( m_cTanks < MAX_MULTI_TARGETS ) + { + // add this field to the target list + // this assumes that additional fields are targetnames and their values are delay values. + + char tmp[128]; + UTIL_StripToken( pkvd->szKeyName, tmp ); + m_iTankName[ m_cTanks ] = ALLOC_STRING( tmp ); + m_cTanks++; + pkvd->fHandled = TRUE; + } +} + +TYPEDESCRIPTION CFuncTankControls::m_SaveData[] = +{ + DEFINE_FIELD( CFuncTankControls, m_pController, FIELD_CLASSPTR ), + DEFINE_FIELD( CFuncTankControls, m_vecControllerUsePos, FIELD_VECTOR ), + DEFINE_FIELD( CFuncTankControls, m_cTanks, FIELD_INTEGER ), + DEFINE_ARRAY( CFuncTankControls, m_iTankName, FIELD_STRING, MAX_MULTI_TARGETS ), +}; +IMPLEMENT_SAVERESTORE( CFuncTankControls, CBaseEntity ); + +BOOL CFuncTankControls :: OnControls( entvars_t *pevTest ) +{ + Vector offset = pevTest->origin - pev->origin; + + if (m_pParent) + { + if ( ((m_vecControllerUsePos + m_pParent->pev->origin) - pevTest->origin).Length() <= 30 ) + return TRUE; + } + else if ( (m_vecControllerUsePos - pevTest->origin).Length() <= 30 ) return TRUE; + return FALSE; +} + +void CFuncTankControls :: HandleTank ( CBaseEntity *pActivator, CBaseEntity *m_pTank, BOOL activate ) +{ + if(m_pTank->pev->flags & FL_TANK)//it's tank entity + { + if(activate) + { + if (((CFuncTank*)m_pTank)->StartControl((CBasePlayer*)pActivator, this)) + { + m_iState = STATE_ON; //we have active tank! + } + } + else ((CFuncTank*)m_pTank)->StopControl(this); + } +} + +void CFuncTankControls :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + CBaseEntity *tryTank = NULL; + if ( useType == USE_SHOWINFO) + { + DEBUGHEAD; + return; + } + + if ( !m_pController && useType != USE_OFF ) + { + if (!pActivator || !(pActivator->IsPlayer())) return; + if (m_iState != STATE_OFF || ((CBasePlayer*)pActivator)->m_pTank != NULL) return; + + //find all specified tanks + for(int i = 0; i < m_cTanks; i++) + { + //find all tanks with current name + while( tryTank = UTIL_FindEntityByTargetname(tryTank, STRING(m_iTankName[i]))) + { + HandleTank( pActivator, tryTank, TRUE ); + } + } + if (m_iState == STATE_ON) + { + // we found at least one tank to use, so holster player's weapon + m_pController = (CBasePlayer*)pActivator; + m_pController->m_pTank = this; + if ( m_pController->m_pActiveItem ) + { + m_pController->m_pActiveItem->Holster(); + m_pController->pev->weaponmodel = 0; + //viewmodel reset in tank + } + m_pController->m_iHideHUD |= HIDEHUD_WEAPONS; + + if (m_pParent) + m_vecControllerUsePos = m_pController->pev->origin - m_pParent->pev->origin; + else m_vecControllerUsePos = m_pController->pev->origin; + } + } + else if (m_pController && useType != USE_ON) + { + //find all specified tanks + for(int i = 0; i < m_cTanks; i++) + { + //find all tanks with current name + while( tryTank = UTIL_FindEntityByTargetname(tryTank, STRING(m_iTankName[i]))) + { + HandleTank( pActivator, tryTank, FALSE ); + } + } + + // bring back player's weapons + if ( m_pController->m_pActiveItem ) m_pController->m_pActiveItem->Deploy(); + + m_pController->m_iHideHUD &= ~HIDEHUD_WEAPONS; + m_pController->m_pTank = NULL; + + m_pController = NULL; + m_iState = STATE_OFF; + } + +} + +LINK_ENTITY_TO_CLASS( func_tankcontrols, CFuncTankControls ); \ No newline at end of file diff --git a/server/ents/basetank.h b/server/ents/basetank.h new file mode 100644 index 00000000..828dd6a2 --- /dev/null +++ b/server/ents/basetank.h @@ -0,0 +1,143 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#define SF_TANK_ACTIVE 0x0001 +#define SF_TANK_LINEOFSIGHT 0x0010 +#define SF_TANK_CANCONTROL 0x0020 +#define SF_TANK_LASERSPOT 0x0040 //LRC +#define SF_TANK_MATCHTARGET 0x0080 //LRC +#define SF_TANK_SOUNDON 0x8000 +#define SF_TANK_SEQFIRE 0x10000 //LRC - a TankSequence is telling me to fire + +static Vector gTankSpread[] = +{ + Vector( 0, 0, 0 ), // perfect + Vector( 0.025, 0.025, 0.025 ), // small cone + Vector( 0.05, 0.05, 0.05 ), // medium cone + Vector( 0.1, 0.1, 0.1 ), // large cone + Vector( 0.25, 0.25, 0.25 ), // extra-large cone +}; +#define MAX_FIRING_SPREADS ARRAYSIZE(gTankSpread) + +enum TANKBULLET +{ + TANK_BULLET_NONE = 0, + TANK_BULLET_9MM = 1, + TANK_BULLET_MP5 = 2, + TANK_BULLET_12MM = 3, +}; + +class CFuncTankControls; +class CFuncTank : public CBaseBrush +{ +public: + void Spawn( void ); + void PostSpawn( void ); + void Precache( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Think( void ); + void TrackTarget( void ); + int IRelationship( CBaseEntity* pTarget ); + int Classify( void ) { return m_iTankClass; } + + void TryFire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ); + virtual void Fire( const Vector &barrelEnd, const Vector &forward, entvars_t *pevAttacker ); + virtual Vector UpdateTargetPosition( CBaseEntity *pTarget ) + { + return pTarget->BodyTarget( pev->origin ); + } + + void StartRotSound( void ); + void StopRotSound( void ); + + inline BOOL IsActive( void ) { return (pev->spawnflags & SF_TANK_ACTIVE)?TRUE:FALSE; } + inline void TankActivate( void ) { pev->spawnflags |= SF_TANK_ACTIVE; SetNextThink(0.1); m_fireLast = 0; } + inline void TankDeactivate( void ) { pev->spawnflags &= ~SF_TANK_ACTIVE; m_fireLast = 0; StopRotSound(); } + inline BOOL CanFire( void ) { return (gpGlobals->time - m_lastSightTime) < m_persist; } + BOOL InRange( float range ); + void TankTrace( const Vector &vecStart, const Vector &vecForward, const Vector &vecSpread, TraceResult &tr ); + + Vector BarrelPosition( void ) + { + Vector forward, right, up; + UTIL_MakeVectorsPrivate( pev->angles, forward, right, up ); + return pev->origin + (forward * m_barrelPos.x) + (right * m_barrelPos.y) + (up * m_barrelPos.z); + } + + void AdjustAnglesForBarrel( Vector &angles, float distance ); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + void UpdateSpot( void ); + BOOL StartControl( CBasePlayer* pController, CFuncTankControls* pControls ); + void StopControl( CFuncTankControls* pControls ); + + CBaseEntity* BestVisibleEnemy( void ); + CFuncTankControls* m_pControls; // tankcontrols is used as a go-between. + CLaserSpot* m_pSpot; // Laser spot entity + + virtual BOOL IsRocketTank( void ) { return FALSE; } + virtual BOOL IsLaserTank( void ) { return FALSE; } + +protected: + float m_flNextAttack; + + float m_yawCenter; // "Center" yaw + float m_yawRate; // Max turn rate to track targets + float m_yawRange; // Range of turning motion (one-sided: 30 is +/- 30 degress from center) + // Zero is full rotation + float m_yawTolerance; // Tolerance angle + + float m_pitchCenter; // "Center" pitch + float m_pitchRate; // Max turn rate on pitch + float m_pitchRange; // Range of pitch motion as above + float m_pitchTolerance; // Tolerance angle + + float m_lastSightTime;// Last time I saw target + float m_persist; // Persistence of firing (how long do I shoot when I can't see) + RandomRange m_Range; + float m_minRange; // Minimum range to aim/track + float m_maxRange; // Max range to aim/track + float m_fireLast; // Last time I fired + float m_fireRate; // How many rounds/second + + Vector m_barrelPos; // Length of the freakin barrel + float m_spriteScale; // Scale of any sprites we shoot + int m_iszSpriteSmoke; + int m_iszSpriteFlash; + TANKBULLET m_bulletType; // Bullet type + int m_iBulletDamage; // 0 means use Bullet type's default damage + + Vector m_sightOrigin; // Last sight of target + int m_spread; // firing spread + int m_iszMaster; // Master entity + int m_iszFireMaster;//LRC - Fire-Master entity (prevents firing when inactive) + + int m_iTankClass; // Behave As +}; + +class CFuncTankControls : public CBaseLogic +{ +public: + virtual int ObjectCaps( void ) { return (CBaseEntity::ObjectCaps() & ~FCAP_ACROSS_TRANSITION) | FCAP_IMPULSE_USE; } + void Spawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData *pkvd ); + void HandleTank ( CBaseEntity *pActivator, CBaseEntity *m_pTank, BOOL activate ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + BOOL OnControls( entvars_t *pevTest ); + + Vector m_vecControllerUsePos; // where was the player standing when he used me? + + CBasePlayer* m_pController; + //max 32 tanks allowed to control + int m_iTankName [ MAX_MULTI_TARGETS ];// list if indexes into global string array + int m_cTanks;// the total number of targets in this manager's fire list. + EHANDLE m_hPlayer; +}; \ No newline at end of file diff --git a/server/ents/basetrigger.cpp b/server/ents/basetrigger.cpp new file mode 100644 index 00000000..fef556af --- /dev/null +++ b/server/ents/basetrigger.cpp @@ -0,0 +1,1375 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" +#include "baseweapon.h" + +//======================================================================= +// func_trigger - volume, that fire target when player in or out +//======================================================================= +#define SF_TRIGGER_ALLOWMONSTERS 1// monsters allowed to fire this trigger +#define SF_TRIGGER_NOCLIENTS 2// players not allowed to fire this trigger +#define SF_TRIGGER_PUSHABLES 4// only pushables can fire this trigger + +class CBaseTrigger; +class CInOutRegister : public CPointEntity +{ +public: + BOOL IsRegistered ( CBaseEntity *pValue ); + CInOutRegister *Prune( void ); + CInOutRegister *Add( CBaseEntity *pValue ); + BOOL IsEmpty( void ) { return m_pNext ? FALSE:TRUE; }; + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + CBaseTrigger *m_pField; + CInOutRegister *m_pNext; + EHANDLE m_hValue; +}; + +class CBaseTrigger : public CBaseLogic +{ +public: + void Spawn( void ); + void EXPORT Touch( CBaseEntity *pOther ); + void EXPORT Update( void ); + void EXPORT Remove( void ); //generic method for right delete trigger class + virtual void FireOnEntry( CBaseEntity *pOther ){} + virtual void FireOnLeave( CBaseEntity *pOther ){} + + BOOL CanTouch( CBaseEntity *pOther ); + void KeyValue( KeyValueData *pkvd ); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + STATE GetState() { return m_pRegister->IsEmpty()? STATE_OFF : STATE_ON; } + CInOutRegister *m_pRegister; +}; +TYPEDESCRIPTION CBaseTrigger::m_SaveData[] = +{ DEFINE_FIELD( CBaseTrigger, m_pRegister, FIELD_CLASSPTR ), }; +IMPLEMENT_SAVERESTORE(CBaseTrigger, CBaseLogic); + +TYPEDESCRIPTION CInOutRegister::m_SaveData[] = +{ DEFINE_FIELD( CInOutRegister, m_pField, FIELD_CLASSPTR ), + DEFINE_FIELD( CInOutRegister, m_pNext, FIELD_CLASSPTR ), + DEFINE_FIELD( CInOutRegister, m_hValue, FIELD_EHANDLE ), +}; IMPLEMENT_SAVERESTORE(CInOutRegister,CPointEntity); +LINK_ENTITY_TO_CLASS( zoneent, CInOutRegister ); + +BOOL CInOutRegister::IsRegistered ( CBaseEntity *pValue ) +{ + if (m_hValue == pValue) return TRUE; + else if (m_pNext) return m_pNext->IsRegistered( pValue ); + else return FALSE; +} + +CInOutRegister *CInOutRegister::Add( CBaseEntity *pValue ) +{ + if (m_hValue == pValue) + { + // it's already in the list, don't need to do anything + return this; + } + else if (m_pNext) + { + // keep looking + m_pNext = m_pNext->Add( pValue ); + return this; + } + else + { + // reached the end of the list; add the new entry, and trigger + CInOutRegister *pResult = GetClassPtr( (CInOutRegister*)NULL ); + pResult->m_hValue = pValue; + pResult->m_pNext = this; + pResult->m_pField = m_pField; + pResult->pev->classname = MAKE_STRING("zoneent"); + m_pField->FireOnEntry( pValue ); + return pResult; + } +} + +CInOutRegister *CInOutRegister::Prune( void ) +{ + if ( m_hValue ) + { + ASSERTSZ(m_pNext != NULL, "invalid InOut registry terminator\n"); + if ( m_pField->Intersects(m_hValue) ) + { + // this entity is still inside the field, do nothing + m_pNext = m_pNext->Prune(); + return this; + } + else + { + // this entity has just left the field, trigger + m_pField->FireOnLeave( m_hValue ); + SetThink( Remove ); + SetNextThink( 0.1 ); + return m_pNext->Prune(); + } + } + else + { // this register has a missing or null value + if (m_pNext) + { + // this is an invalid list entry, remove it + SetThink( Remove ); + SetNextThink( 0.1 ); + return m_pNext->Prune(); + } + else + { + // this is the list terminator, leave it. + return this; + } + } +} + +void CBaseTrigger::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "offtarget")) + { + pev->netname = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "modifier")) + { + pev->armorvalue = atof(pkvd->szValue) / 100.0; + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "roomtype")) //for soundfx + { + pev->button = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CBaseTrigger :: Spawn( void ) +{ + pev->solid = SOLID_TRIGGER; + pev->movetype = MOVETYPE_NONE; + pev->takedamage = DAMAGE_NO; + UTIL_SetModel(ENT(pev), pev->model ); // set size and link into world + SetBits( pev->effects, EF_NODRAW ); + + // create a null-terminator for the registry + m_pRegister = GetClassPtr( (CInOutRegister*)NULL ); + m_pRegister->m_hValue = NULL; + m_pRegister->m_pNext = NULL; + m_pRegister->m_pField = this; + m_pRegister->pev->classname = MAKE_STRING("zoneent"); + + SetThink( Update ); +} + +BOOL CBaseTrigger :: CanTouch( CBaseEntity *pOther ) +{ + if ( gpGlobals->time < pev->dmgtime) return FALSE; + pev->dmgtime = gpGlobals->time + m_flWait; + + // Only touch clients, monsters, or pushables (depending on flags) + if (pOther->pev->flags & FL_CLIENT) return !(pev->spawnflags & SF_TRIGGER_NOCLIENTS); + else if (pOther->pev->flags & FL_MONSTER) return pev->spawnflags & SF_TRIGGER_ALLOWMONSTERS; + else if (pOther->IsPushable()) return pev->spawnflags & SF_TRIGGER_PUSHABLES; + return FALSE; +} + +void CBaseTrigger :: Touch( CBaseEntity *pOther ) +{ + if (!CanTouch(pOther)) return; + m_hActivator = pOther; //save activator; + + m_pRegister = m_pRegister->Add(pOther); + if (m_fNextThink <= 0 && !m_pRegister->IsEmpty())SetNextThink( 0.1 ); +} + +void CBaseTrigger :: Update( void ) +{ + // Prune handles all Intersects tests and fires targets as appropriate + m_pRegister = m_pRegister->Prune(); + + if (m_pRegister->IsEmpty()) DontThink(); + else SetNextThink( 0.1 ); +} + +void CBaseTrigger :: Remove( void ) +{ + UTIL_Remove( m_pRegister ); + UTIL_Remove( this ); +} + +//======================================================================= +// trigger_mutiple - classic QUAKE trigger +//======================================================================= +class CTriggerMulti : public CBaseTrigger +{ + void FireOnEntry( CBaseEntity *pOther ) + { + if(!IsLockedByMaster(pOther)) FireTargets(); + } + void FireOnLeave( CBaseEntity *pOther ) + { + if (!IsLockedByMaster(pOther)) + UTIL_FireTargets(pev->netname, pOther, this, USE_TOGGLE ); + } +}; +LINK_ENTITY_TO_CLASS( trigger_multiple, CTriggerMulti ); + +//======================================================================= +// trigger_once - classic QUAKE trigger +//======================================================================= +class CTriggerOnce : public CBaseTrigger +{ + void FireOnEntry( CBaseEntity *pOther ) + { + if ( !IsLockedByMaster(pOther)) + { + FireTargets(); + SetThink( Remove ); + SetNextThink( 0 ); + } + } +}; +LINK_ENTITY_TO_CLASS( trigger_once, CTriggerOnce ); + +//======================================================================= +// trigger_autosave - game autosave +//======================================================================= +class CTriggerSave : public CBaseTrigger +{ + void FireOnEntry( CBaseEntity *pOther ) { SERVER_COMMAND( "autosave\n" ); } + void FireOnLeave( CBaseEntity *pOther ) + { + SetThink( Remove ); + SetNextThink( 0 ); + } +}; +LINK_ENTITY_TO_CLASS( trigger_autosave, CTriggerSave ); + +//======================================================================= +// func_friction - game autosave +//======================================================================= +class CChangeFriction : public CBaseTrigger +{ + void Spawn( void ) + { + pev->solid = SOLID_TRIGGER; + pev->movetype = MOVETYPE_NONE; + UTIL_SetModel(ENT(pev), pev->model ); + } + void Touch( CBaseEntity *pOther ) + { + if ( pOther->pev->movetype != 11 && pOther->pev->movetype != 10 ) + pOther->pev->friction = pev->armorvalue; + } +}; +LINK_ENTITY_TO_CLASS( func_friction, CChangeFriction ); + +//======================================================================= +// trigger_push - triger that pushes player +//======================================================================= +class CTriggerPush : public CBaseTrigger +{ + void Spawn( void ) + { + if ( pev->angles == g_vecZero ) pev->angles.y = 360; + if (pev->speed == 0) pev->speed = 100; + UTIL_LinearVector( this ); + + if ( FBitSet (pev->spawnflags, 2) ) pev->solid = SOLID_NOT; + else pev->solid = SOLID_TRIGGER; + + pev->movetype = MOVETYPE_NONE; + UTIL_SetModel(ENT(pev), pev->model ); + SetBits( pev->effects, EF_NODRAW ); + UTIL_SetOrigin( this, pev->origin ); + } + void Use ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + if( pev->solid == SOLID_NOT ) + { + pev->solid = SOLID_TRIGGER; + pev->contents = CONTENTS_TRIGGER; + } + else pev->solid = SOLID_NOT; + UTIL_SetOrigin( this, pev->origin ); + } + void Touch( CBaseEntity *pOther ) + { + switch( pOther->pev->movetype ) + { + case MOVETYPE_NONE: + case MOVETYPE_PUSH: + case MOVETYPE_NOCLIP: + case MOVETYPE_FOLLOW: + return; + } + + if ( pOther->pev->solid != SOLID_NOT && pOther->pev->solid != SOLID_BSP ) + { + // Instant trigger, just transfer velocity and remove + if (FBitSet(pev->spawnflags, 1)) + { + pOther->pev->velocity = pOther->pev->velocity + (pev->speed * pev->movedir); + if ( pOther->pev->velocity.z > 0 ) pOther->pev->flags &= ~FL_ONGROUND; + UTIL_Remove( this ); + } + else + { + Vector vecPush = (pev->speed * pev->movedir); + vecPush = vecPush + pOther->pev->velocity; + pOther->pev->velocity = vecPush; + } + } + } +}; +LINK_ENTITY_TO_CLASS( trigger_push, CTriggerPush ); + +//======================================================================= +// trigger_gravity - classic HALF_LIFE gravity modifier +//======================================================================= +class CTriggerGravity : public CBaseTrigger +{ + void FireOnEntry( CBaseEntity *pOther ) { pOther->pev->gravity = pev->gravity; } + //void FireOnLeave( CBaseEntity *pOther ) { pOther->pev->gravity = EARTH_GRAVITY;} +}; +LINK_ENTITY_TO_CLASS( trigger_gravity, CTriggerGravity); + +//======================================================================= +// trigger_sound - a DSP zone, brush prototype of env_sound +//======================================================================= +class CTriggerSound : public CBaseTrigger +{ + void FireOnEntry( CBaseEntity *pOther ) + { + if (!pOther->IsPlayer()) return; + pev->impulse = ((CBasePlayer *)pOther)->m_iSndRoomtype; //save old dsp sound + ((CBasePlayer *)pOther)->m_iSndRoomtype = pev->button; //set new dsp + ((CBasePlayer *)pOther)->hearNeedsUpdate = 1; + } + void FireOnLeave( CBaseEntity *pOther ) + { + if (!pOther->IsPlayer()) return; + ((CBasePlayer *)pOther)->m_iSndRoomtype = pev->impulse; //restore old dsp + ((CBasePlayer *)pOther)->hearNeedsUpdate = 1; + } +}; +LINK_ENTITY_TO_CLASS( trigger_sound, CTriggerSound ); + +//======================================================================= +// trigger_relay +//======================================================================= +class CTriggerRelay : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Spawn( void ){ m_iState = STATE_OFF; } + void Think ( void ); +}; +LINK_ENTITY_TO_CLASS( trigger_relay, CTriggerRelay ); + +void CTriggerRelay::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "triggerstate")) + { + int type = atoi( pkvd->szValue ); + switch( type ) + { + case 0: pev->impulse = USE_OFF; break; + case 2: pev->impulse = USE_TOGGLE; break; + case 3: pev->impulse = USE_SET; break; + default: pev->impulse = USE_ON; break; + } + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "locktarget")) + { + pev->message = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CTriggerRelay::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; //save activator + if(pev->impulse) pev->button = pev->impulse; + else pev->button = (int)useType; //save use type + pev->frags = value; //save our value + + if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "target is %s, locktarget %s\n", STRING(pev->target), STRING(pev->message)); + Msg( "new value %g\n", pev->frags ); + } + else //activate target + { + m_iState = STATE_ON; + SetNextThink(m_flDelay); + } +} + +void CTriggerRelay::Think ( void ) +{ + if (IsLockedByMaster()) UTIL_FireTargets( pev->message, m_hActivator, this, (USE_TYPE)pev->button, pev->frags ); + else + { + UTIL_FireTargets( pev->target, m_hActivator, this, (USE_TYPE)pev->button, pev->frags ); + UTIL_FireTargets( m_iszKillTarget, m_hActivator, this, USE_REMOVE ); + } + + //suhtdown + m_iState = STATE_OFF; + DontThink();//just in case + if ( pev->spawnflags & 1 ) UTIL_Remove( this ); +} + +//======================================================================= +// trigger_auto +//======================================================================= +class CAutoTrigger : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void PostActivate( void ); +}; +LINK_ENTITY_TO_CLASS( trigger_auto, CAutoTrigger ); + +void CAutoTrigger::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "triggerstate")) + { + int type = atoi( pkvd->szValue ); + switch( type ) + { + case 0: pev->impulse = USE_OFF; break; + case 2: pev->impulse = USE_TOGGLE; break; + case 3: pev->impulse = USE_SET; break; + default: pev->impulse = USE_ON; break; + } + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CAutoTrigger::PostActivate( void ) +{ + if ( !m_globalstate || gGlobalState.EntityGetState( m_globalstate ) == GLOBAL_ON ) + { + FireTargets( (USE_TYPE)pev->impulse ); + if ( pev->spawnflags & 1 ) UTIL_Remove( this ); + } +} + +//======================================================================= +// trigger_changetarget +//======================================================================= +class CChangeTarget : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( trigger_changetarget, CChangeTarget ); + +void CChangeTarget::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "newtarget") || FStrEq(pkvd->szKeyName, "m_iszNewTarget")) + { + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CChangeTarget::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + CBaseEntity *pTarget = UTIL_FindEntityByTargetname( NULL, STRING( pev->target ), pActivator ); + + if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "Current target %s, new target %s\n", STRING(pev->target), STRING(pev->message) ); + Msg( "target entity is: %s, current target: %s\n", STRING(pTarget->pev->classname), STRING(pTarget->pev->target)); + } + else + { + if (pTarget) + { + if (FStrEq(STRING(pev->message), "*this")) + { + if (pActivator) pTarget->pev->target = pActivator->pev->targetname; + else ALERT(at_error, "util_settarget \"%s\" requires a self pointer!\n", STRING(pev->targetname)); + } + else + { + if(pTarget->IsFuncScreen()) pTarget->ChangeCamera( pev->message ); + else pTarget->pev->target = pev->message; + } + CBaseMonster *pMonster = pTarget->MyMonsterPointer( ); + if (pMonster) pMonster->m_pGoalEnt = NULL; + + } + } +} + +//======================================================================= +// multi_manager +//======================================================================= +#define FL_CLONE 0x80000000 + +class CMultiManager : public CBaseLogic +{ +public: + void KeyValue( KeyValueData *pkvd ); + void Spawn ( void ); + void Think ( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + BOOL HasTarget( string_t targetname ); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + + int m_cTargets; // the total number of targets in this manager's fire list. + int m_index; // Current target + float m_startTime; // Time we started firing + int m_iTargetName [ MAX_MULTI_TARGETS ];// list if indexes into global string array + float m_flTargetDelay [ MAX_MULTI_TARGETS ];// delay (in seconds) +private: + inline BOOL IsClone( void ) { return (pev->spawnflags & FL_CLONE) ? TRUE : FALSE; } + inline BOOL ShouldClone( void ) + { + if ( IsClone() )return FALSE; + //work in progress and calling again ? + return (m_iState == STATE_ON) ? TRUE : FALSE; + } + + CMultiManager *Clone( void ); +}; +LINK_ENTITY_TO_CLASS( multi_manager, CMultiManager ); + +// Global Savedata for multi_manager +TYPEDESCRIPTION CMultiManager::m_SaveData[] = +{ DEFINE_FIELD( CMultiManager, m_cTargets, FIELD_INTEGER ), + DEFINE_FIELD( CMultiManager, m_index, FIELD_INTEGER ), + DEFINE_FIELD( CMultiManager, m_startTime, FIELD_TIME ), + DEFINE_ARRAY( CMultiManager, m_iTargetName, FIELD_STRING, MAX_MULTI_TARGETS ), + DEFINE_ARRAY( CMultiManager, m_flTargetDelay, FIELD_FLOAT, MAX_MULTI_TARGETS ), +};IMPLEMENT_SAVERESTORE(CMultiManager, CBaseLogic); + +void CMultiManager :: KeyValue( KeyValueData *pkvd ) +{ + if ( m_cTargets < MAX_MULTI_TARGETS ) + { + char tmp[128]; + + UTIL_StripToken( pkvd->szKeyName, tmp ); + m_iTargetName [ m_cTargets ] = ALLOC_STRING( tmp ); + m_flTargetDelay [ m_cTargets ] = RandomRange((char *)STRING(ALLOC_STRING(pkvd->szValue))).Random(); + m_cTargets++; + pkvd->fHandled = TRUE; + } +} + +void CMultiManager :: Spawn( void ) +{ + // Sort targets + // Quick and dirty bubble sort + int swapped = 1; + + while ( swapped ) + { + swapped = 0; + for ( int i = 1; i < m_cTargets; i++ ) + { + if ( m_flTargetDelay[i] < m_flTargetDelay[i-1] ) + { + // Swap out of order elements + int name = m_iTargetName[i]; + float delay = m_flTargetDelay[i]; + m_iTargetName[i] = m_iTargetName[i-1]; + m_flTargetDelay[i] = m_flTargetDelay[i-1]; + m_iTargetName[i-1] = name; + m_flTargetDelay[i-1] = delay; + swapped = 1; + } + } + } + + m_iState = STATE_OFF; + m_index = 0; +} + +BOOL CMultiManager::HasTarget( string_t targetname ) +{ + for ( int i = 0; i < m_cTargets; i++ ) + if ( FStrEq(STRING(targetname), STRING(m_iTargetName[i])) ) return TRUE; + return FALSE; +} + +void CMultiManager :: Think ( void ) +{ + float time; + + time = gpGlobals->time - m_startTime; + while ( m_index < m_cTargets && m_flTargetDelay[ m_index ] <= time ) + { + UTIL_FireTargets( m_iTargetName[ m_index ], m_hActivator, this, USE_TOGGLE, pev->frags ); + m_index++; + } + if ( m_index >= m_cTargets )// have we fired all targets? + { + if ( IsClone() ) + { + UTIL_Remove( this ); + return; + } + + //stop manager + m_iState = STATE_OFF; + DontThink(); + return; + } + m_iState = STATE_ON; //continue firing targets + pev->nextthink = m_startTime + m_flTargetDelay[ m_index ]; +} + +CMultiManager *CMultiManager::Clone( void ) +{ + CMultiManager *pMulti = GetClassPtr( (CMultiManager *)NULL ); + + edict_t *pEdict = pMulti->pev->pContainingEntity; + memcpy( pMulti->pev, pev, sizeof(*pev) ); + pMulti->pev->pContainingEntity = pEdict; + + pMulti->pev->spawnflags |= FL_CLONE; + pMulti->m_cTargets = m_cTargets; + pMulti->m_flWait = m_flWait; + pMulti->m_iState = m_iState; + memcpy( pMulti->m_iTargetName, m_iTargetName, sizeof( m_iTargetName ) ); + memcpy( pMulti->m_flTargetDelay, m_flTargetDelay, sizeof( m_flTargetDelay ) ); + + return pMulti; +} + +void CMultiManager :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_hActivator = pActivator; + + if(IsLockedByMaster( useType )) return; + pev->frags = value;//save our value + + if (useType == USE_TOGGLE || useType == USE_ON) + { + if ( ShouldClone() )//create clone if needed + { + CMultiManager *pClone = Clone(); + pClone->Use( pActivator, pCaller, useType, value ); + return; + } + if(m_iState == STATE_OFF) + { + m_startTime = m_flWait + gpGlobals->time; + m_iState = STATE_TURN_ON; + m_index = 0; + SetNextThink( m_flWait ); + } + } + else if (useType == USE_OFF) + { + m_iState = STATE_OFF; + DontThink(); + } + else if (useType == USE_SHOWINFO)//only show info if locked by master + { + DEBUGHEAD; + Msg("State: %s, number of targets %d\n", GetStringForState( GetState()), m_cTargets); + if(m_iState == STATE_ON) Msg("Current target %s, delay time %f\n", STRING(m_iTargetName[ m_index ]), m_flTargetDelay[ m_index ]); + else Msg("No targets for firing.\n"); + } +} + +//======================================================================= +// multi_master +//======================================================================= +#define LOGIC_AND 0 // fire if all objects active +#define LOGIC_OR 1 // fire if any object active +#define LOGIC_NAND 2 // fire if not all objects active +#define LOGIC_NOR 3 // fire if all objects disable +#define LOGIC_XOR 4 // fire if only one (any) object active +#define LOGIC_XNOR 5 // fire if active any number objects, but < then all + +#define ST_ON 0 +#define ST_OFF 1 +#define ST_TURNON 2 +#define ST_TURNOFF 3 +#define ST_IN_USE 4 + +class CMultiMaster : public CBaseLogic +{ +public: + void Spawn ( void ){ SetNextThink( 0.1 ); } + void Think ( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual STATE GetState( void ) { return m_iState; }; + virtual STATE GetState( CBaseEntity *pActivator ) { return EvalLogic(pActivator)?STATE_ON:STATE_OFF; }; + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + int m_cTargets;// the total number of targets in this manager's fire list. + int m_iTargetName[ MAX_MULTI_TARGETS ];// list of indexes into global string array + int m_iTargetState[ MAX_MULTI_TARGETS ];//list of wishstate targets + + BOOL EvalLogic ( CBaseEntity *pEntity ); +}; +LINK_ENTITY_TO_CLASS( multi_watcher, CMultiMaster ); + +TYPEDESCRIPTION CMultiMaster::m_SaveData[] = +{ + DEFINE_FIELD( CMultiMaster, m_cTargets, FIELD_INTEGER ), + DEFINE_ARRAY( CMultiMaster, m_iTargetName, FIELD_STRING, MAX_MULTI_TARGETS ), + DEFINE_ARRAY( CMultiMaster, m_iTargetState, FIELD_INTEGER, MAX_MULTI_TARGETS ), +};IMPLEMENT_SAVERESTORE(CMultiMaster,CBaseLogic); + +void CMultiMaster :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "mode")) + { + pev->button = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + if (FStrEq(pkvd->szKeyName, "offtarget")) + { + pev->netname = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else // add this field to the target list + { + // this assumes that additional fields are targetnames and their values are delay values. + if ( m_cTargets < MAX_MULTI_TARGETS ) + { + char tmp[128]; + + UTIL_StripToken( pkvd->szKeyName, tmp ); + m_iTargetName [ m_cTargets ] = ALLOC_STRING( tmp ); + m_iTargetState [ m_cTargets ] = atoi (pkvd->szValue); + m_cTargets++; + pkvd->fHandled = TRUE; + } + } +} + +void CMultiMaster :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg("State: %s, Number of targets %d\n", GetStringForState( GetState()), m_cTargets); + Msg("Limit is %d entities\n", MAX_MULTI_TARGETS); + } +} + +void CMultiMaster :: Think ( void ) +{ + if(EvalLogic(NULL)) + { + if(m_iState == STATE_OFF) + { + m_iState = STATE_ON; + UTIL_FireTargets( pev->target, this, this, USE_ON ); + } + } + else + { + if(m_iState == STATE_ON) + { + m_iState = STATE_OFF; + UTIL_FireTargets( pev->netname, this, this, USE_OFF ); + } + } + SetNextThink( 0.05 ); +} + +BOOL CMultiMaster :: EvalLogic ( CBaseEntity *pActivator ) +{ + int i; + BOOL b; + BOOL xorgot = FALSE; + + CBaseEntity* pEntity; + + for (i = 0; i < m_cTargets; i++) + { + pEntity = UTIL_FindEntityByTargetname(NULL,STRING(m_iTargetName[i]), pActivator); + if (pEntity != NULL); + else continue; + b = FALSE; + + switch (pEntity->GetState()) + { + case STATE_ON: if(m_iTargetState[i] == ST_ON) b = TRUE; break; + case STATE_OFF: if(m_iTargetState[i] == ST_OFF) b = TRUE; break; + case STATE_TURN_ON: if(m_iTargetState[i] == ST_TURNON) b = TRUE; break; + case STATE_TURN_OFF: if(m_iTargetState[i] == ST_TURNOFF) b = TRUE; break; + case STATE_IN_USE: if(m_iTargetState[i] == ST_IN_USE) b = TRUE; break; + } + // handle the states for this logic mode + if (b) + { + switch (pev->button) + { + case LOGIC_OR: return TRUE; + case LOGIC_NOR: return FALSE; + case LOGIC_XOR: + if(xorgot) return FALSE; + xorgot = TRUE; + break; + case LOGIC_XNOR: + if(xorgot) return TRUE; + xorgot = TRUE; + break; + } + } + else // b is false + { + switch (pev->button) + { + case LOGIC_AND: return FALSE; + case LOGIC_NAND: return TRUE; + } + } + } + + // handle the default cases for each logic mode + switch (pev->button) + { + case LOGIC_AND: + case LOGIC_NOR: return TRUE; + case LOGIC_XOR: return xorgot; + case LOGIC_XNOR: return !xorgot; + default: return FALSE; + } +} + +//======================================================================= +// trigger_changelevel - classic HALF_LIFE changelevel +//======================================================================= +class CChangeLevel : public CBaseLogic +{ +public: + void Spawn( void ); + void KeyValue( KeyValueData *pkvd ); + virtual int ObjectCaps( void ) { return CBaseLogic :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void ChangeLevelNow( CBaseEntity *pActivator ); + void Touch( CBaseEntity *pOther ){ if (pOther->IsPlayer())Think(); } + void Think( void ) + { + FireAfter(); + //SERVER_COMMAND( "intermission\n" ); + UTIL_ChangeLevel( pev->netname, pev->message ); + } + void FireAfter( void ); +}; +LINK_ENTITY_TO_CLASS( trigger_changelevel, CChangeLevel ); + +void CChangeLevel :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "map")) + { + char m_szMapName[MAP_MAX_NAME]; + if (strlen(pkvd->szValue) >= MAP_MAX_NAME) + Msg("ERROR: Map name '%s' too long (%d chars)\n", pkvd->szValue, MAP_MAX_NAME ); + + strcpy(m_szMapName, pkvd->szValue); + for (int i = 0; m_szMapName[i]; i++) { m_szMapName[i] = tolower(m_szMapName[i]); } + pev->netname = ALLOC_STRING( m_szMapName ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "landmark")) + { + if (strlen(pkvd->szValue) >= MAP_MAX_NAME) + Msg("ERROR: Landmark name '%s' too long (%d chars)\n", pkvd->szValue, MAP_MAX_NAME ); + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "changetarget")) + { + pev->target = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CChangeLevel :: Spawn( void ) +{ + if ( FStringNull( pev->netname)) Msg( "a % doesn't have a map", STRING(pev->classname) ); + if ( FStringNull( pev->message)) Msg( "trigger_changelevel to %s doesn't have a landmark", STRING(pev->netname) ); + + //determine work style + if (FStringNull ( pev->targetname )) SetUse( NULL ); + else SetTouch( NULL ); + + pev->solid = SOLID_TRIGGER; + pev->movetype = MOVETYPE_NONE; + UTIL_SetModel(ENT(pev), pev->model); + SetBits( pev->effects, EF_NODRAW );//make invisible +} + +void CChangeLevel :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "Mapname: %s, Landmark name %f\n", STRING(pev->netname), STRING(pev->message)); + SHIFT; + } + else SetNextThink( 0 ); +} + +void CChangeLevel :: FireAfter( void ) +{ + // Create an entity to fire the changetarget + if (!FStringNull( pev->target )) + { + CBaseEntity *pPlayer = UTIL_PlayerByIndex( 1 ); + CBaseEntity *pChlFire = CBaseEntity::Create( "fireent", pPlayer->pev->origin, g_vecZero, NULL ); + if ( pChlFire ) pChlFire->pev->target = pev->target; + } +} + +//========================================================= +// trigger_playerfreeze +//========================================================= +class CTriggerFreeze : public CBaseLogic +{ + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + m_hActivator = pActivator; + + if (pActivator && pActivator->pev->flags & FL_CLIENT); + else pActivator = UTIL_PlayerByIndex( 1 ); + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + ((CBasePlayer *)((CBaseEntity *)pActivator))->EnableControl(FALSE); + m_iState = STATE_ON; + if(m_flDelay) SetNextThink( m_flDelay ); + } + else if(useType == USE_OFF) + { + ((CBasePlayer *)((CBaseEntity *)pActivator))->EnableControl(TRUE); + m_iState = STATE_OFF; + DontThink(); + } + else if(useType == USE_SHOWINFO) { DEBUGHEAD; } + } + void Think( void ) + { + ((CBasePlayer *)((CBaseEntity *)m_hActivator))->EnableControl(TRUE); + m_iState = STATE_OFF; + } + +}; +LINK_ENTITY_TO_CLASS( trigger_playerfreeze, CTriggerFreeze ); + +//======================================================================= +// trigger_hurt - classic QUAKE damage inflictor +//======================================================================= +class CTriggerHurt : public CBaseLogic +{ +public: + void Spawn( void ); + void Think( void ); + void Touch ( CBaseEntity *pOther ); + void KeyValue( KeyValueData *pkvd ); + void Use ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( trigger_hurt, CTriggerHurt ); + +void CTriggerHurt :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "damage")) + { + pev->dmg = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "damagetype")) + { + pev->button |= atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CTriggerHurt :: Spawn( void ) +{ + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + UTIL_SetModel(ENT(pev), pev->model); + UTIL_SetOrigin( this, pev->origin ); + SetBits( pev->effects, EF_NODRAW ); + m_iState = STATE_OFF; + + if (!FBitSet(pev->spawnflags, 2 )) Use( this, this, USE_ON, 0 ); +} + +void CTriggerHurt :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + pev->solid = SOLID_TRIGGER; + pev->contents = CONTENTS_TRIGGER; + UTIL_SetOrigin( this, pev->origin ); + if (pev->button & DMG_RADIATION) SetNextThink( RANDOM_FLOAT(0.0, 0.5) ); + m_iState = STATE_ON; + } + else if (useType == USE_OFF) + { + pev->solid = SOLID_NOT; + UTIL_SetOrigin( this, pev->origin ); + if (pev->button & DMG_RADIATION) DontThink(); + m_iState = STATE_OFF; + } + else if (useType == USE_SET) + { + pev->dmg = value;//set dmg level + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg( "State: %s, Dmg value %g\n", GetStringForState( GetState()), pev->dmg); + PrintStringForDamage( pev->button ); + } + +} + +void CTriggerHurt :: Touch ( CBaseEntity *pOther ) +{ + float fldmg; + + if ( !pOther->pev->takedamage ) return; + if ( !FBitSet( pOther->pev->flags, FL_CLIENT|FL_MONSTER ) && !pOther->IsPushable() ) return; + + if ( IsMultiplayer() ) + { + if ( pev->dmgtime > gpGlobals->time ) + { + if ( gpGlobals->time != pev->pain_finished ) + { + // too early to hurt again, and not same frame with a different entity + if ( pOther->IsPlayer() ) + { + int playerMask = 1 << (pOther->entindex() - 1); + if ( pev->impulse & playerMask ) return; + pev->impulse |= playerMask; + } + else return; + } + } + else + { + pev->impulse = 0; + if ( pOther->IsPlayer() ) + { + int playerMask = 1 << (pOther->entindex() - 1); + pev->impulse |= playerMask; + } + } + } + else if( pev->dmgtime > gpGlobals->time && gpGlobals->time != pev->pain_finished ) return; + + fldmg = pev->dmg * 0.5; // 0.5 seconds worth of damage, pev->dmg is damage/second + + if ( fldmg < 0 ) pOther->TakeHealth( -fldmg, pev->button ); + else pOther->TakeDamage( pev, pev, fldmg, pev->button ); + + pev->pain_finished = gpGlobals->time; + pev->dmgtime = gpGlobals->time + 0.5; +} + +void CTriggerHurt :: Think( void ) +{ + edict_t *pentPlayer; + CBasePlayer *pPlayer = NULL; + float flRange; + entvars_t *pevTarget; + Vector vecSpot1; + Vector vecSpot2; + Vector vecRange; + Vector origin; + Vector view_ofs; + + origin = pev->origin; + view_ofs = pev->view_ofs; + + pev->origin = (pev->absmin + pev->absmax) * 0.5; + pev->view_ofs = pev->view_ofs * 0.0; + pentPlayer = FIND_CLIENT_IN_PVS(edict()); + + pev->origin = origin; + pev->view_ofs = view_ofs; + + if (!FNullEnt(pentPlayer)) + { + pPlayer = GetClassPtr( (CBasePlayer *)VARS(pentPlayer)); + pevTarget = VARS(pentPlayer); + vecSpot1 = (pev->absmin + pev->absmax) * 0.5; + vecSpot2 = (pevTarget->absmin + pevTarget->absmax) * 0.5; + vecRange = vecSpot1 - vecSpot2; + flRange = vecRange.Length(); + if (pPlayer->m_flgeigerRange >= flRange) pPlayer->m_flgeigerRange = flRange; + } + SetNextThink( 0.25 ); +} + +//======================================================================= +// trigger_transition - area that moving all entities inside across level +//======================================================================= +class CVolumeTransition : public CPointEntity // Don't change this! +{ +public: + void Spawn( void ) + { + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + UTIL_SetModel(ENT(pev), pev->model); + pev->model = NULL; + pev->modelindex = 0; + } +}; +LINK_ENTITY_TO_CLASS( trigger_transition, CVolumeTransition ); + +//======================================================================= +// trigger_camera - generic camera +//======================================================================= +class CTriggerCamera : public CBaseLogic +{ + void Spawn (void ); + void PostSpawn( void ); + void UpdatePlayerView( void ); + void Think( void ); + void PostActivate( void ); + void OverrideReset( void ); + void Move( void ); + void TurnOn( void ); + void TurnOff(void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData* ); + int ObjectCaps( void ) { return CBaseLogic::ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + CBaseEntity* pTarget; +}; +LINK_ENTITY_TO_CLASS( trigger_camera, CTriggerCamera ); + + +#define SF_CAMERA_PLAYER_POSITION 1 //start from player eyes +#define SF_CAMERA_PLAYER_TARGET 2 //player it's camera target +#define SF_CAMERA_PLAYER_TAKECONTROL 4 //freeze player + +void CTriggerCamera :: KeyValue( KeyValueData* pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "viewentity")) + { + pev->netname = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "moveto")) + { + pev->message = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CTriggerCamera :: Spawn (void ) +{ + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NOCLIP; + + m_iState = STATE_OFF; + + UTIL_SetModel(ENT(pev),"models/common/null.mdl"); + UTIL_SetSize(pev, g_vecZero, g_vecZero); + SetBits( pev->flags, FL_POINTENTITY ); +} + +void CTriggerCamera::PostSpawn( void ) +{ + m_pGoalEnt = UTIL_FindEntityByTargetname (NULL, STRING(pev->message)); + if(m_pGoalEnt) UTIL_SetOrigin( this, m_pGoalEnt->pev->origin ); +} + +void CTriggerCamera::OverrideReset( void ) +{ + //find path_corner on a next level + m_pGoalEnt = UTIL_FindEntityByTargetname (NULL, STRING(pev->message)); + if(m_pGoalEnt) UTIL_SetOrigin( this, m_pGoalEnt->pev->origin ); +} + +void CTriggerCamera::PostActivate( void ) +{ + if (FStrEq(STRING(pev->target), "player") || (pev->spawnflags & SF_CAMERA_PLAYER_TARGET)) + pTarget = UTIL_PlayerByIndex( 1 ); + else pTarget = UTIL_FindEntityByTargetname( NULL, STRING(pev->target) ); +} + +void CTriggerCamera::Think( void ) +{ + SetNextThink( 0 ); + + Move(); + pev->dmgtime = gpGlobals->time; + if ( pTarget ) UTIL_WatchTarget( this, pTarget ); + if(m_flWait && pev->teleport_time < gpGlobals->time) TurnOff(); +} + +void CTriggerCamera :: UpdatePlayerView( void ) +{ + int flags; + + if(pev->spawnflags & SF_CAMERA_PLAYER_TAKECONTROL) //freeze player + ((CBasePlayer *)((CBaseEntity *)m_hActivator))->EnableControl(GetState() ? FALSE : TRUE ); + if(GetState() == STATE_OFF) flags = 0; + else flags |= CAMERA_ON; + CBaseEntity *pCamera = UTIL_FindEntityByTargetname( NULL, STRING(pev->netname) ); + if(pCamera && !pCamera->IsBSPModel()) UTIL_SetView( (CBaseEntity *)m_hActivator, pCamera, flags ); + else UTIL_SetView( (CBaseEntity *)m_hActivator, this, flags ); +} + +void CTriggerCamera :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(pActivator && pActivator->IsPlayer())//only at player + { + m_hActivator = pActivator;//save activator + } + else m_hActivator = UTIL_PlayerByIndex( 1 ); + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if (useType == USE_ON ) TurnOn(); + else if (useType == USE_OFF) TurnOff(); + else if (useType == USE_SHOWINFO) + { + Msg("======/Xash Debug System/======\n"); + Msg("classname: %s\n", STRING(pev->classname)); + Msg("State: %s, Look at %s\n", GetStringForState( GetState()), pev->netname ? STRING(pev->netname) : STRING(pev->targetname)); + Msg("Speed: %g Camera target: %s\n", pev->speed, pTarget ? STRING(pTarget->pev->targetname) : "None" ); + } +} + +void CTriggerCamera::Move( void ) +{ + // Not moving on a path, return + if (!m_pGoalEnt) return; + + // Subtract movement from the previous frame + pev->frags -= pev->speed * gpGlobals->frametime; + + // Have we moved enough to reach the target? + if ( pev->frags <= 0 ) + { + // Fire the passtarget if there is one + UTIL_FireTargets(m_pGoalEnt->pev->message, this, this, USE_TOGGLE ); + if ( FBitSet( m_pGoalEnt->pev->spawnflags, SF_CORNER_FIREONCE ) ) m_pGoalEnt->pev->message = iStringNull; + if ( FBitSet( m_pGoalEnt->pev->spawnflags, SF_CORNER_TELEPORT ) ) + { + m_pGoalEnt = m_pGoalEnt->GetNext(); + if ( m_pGoalEnt ) UTIL_AssignOrigin( this, m_pGoalEnt->pev->origin); + } + if ( FBitSet( m_pGoalEnt->pev->spawnflags, SF_CORNER_WAITTRIG ) ) + { + //strange feature... + } + + // Time to go to the next target + m_pGoalEnt = m_pGoalEnt->GetNext(); + + // Set up next corner + if ( !m_pGoalEnt ) UTIL_SetVelocity( this, g_vecZero ); + else + { + pev->message = m_pGoalEnt->pev->targetname; //save last corner + ((CPathCorner *)m_pGoalEnt)->GetSpeed( &pev->armorvalue ); + + Vector delta = m_pGoalEnt->pev->origin - pev->origin; + pev->frags = delta.Length(); + pev->movedir = delta.Normalize(); + m_flDelay = gpGlobals->time + m_pGoalEnt->GetDelay(); + } + } + + if ( m_flDelay > gpGlobals->time ) + pev->speed = UTIL_Approach( 0, pev->speed, 500 * gpGlobals->frametime ); + else pev->speed = UTIL_Approach( pev->armorvalue, pev->speed, 500 * gpGlobals->frametime ); + + if(!pTarget)UTIL_WatchTarget( this, m_pGoalEnt );//watch for track + + float fraction = 2 * gpGlobals->frametime; + UTIL_SetVelocity( this, ((pev->movedir * pev->speed) * fraction) + (pev->velocity * (1-fraction))); +} + +void CTriggerCamera::TurnOff( void ) +{ + m_iState = STATE_OFF; + if(m_pGoalEnt) m_pGoalEnt = m_pGoalEnt->GetPrev(); + UTIL_SetVelocity( this, g_vecZero ); + UTIL_SetAvelocity( this, g_vecZero ); + UpdatePlayerView(); + FireTargets(); + DontThink(); +} + +void CTriggerCamera::TurnOn( void ) +{ + pev->dmgtime = gpGlobals->time; + m_iState = STATE_ON; + + pev->armorvalue = pev->speed; + m_flDelay = gpGlobals->time; + pev->frags = 0; + + // copy over player information + if (pev->spawnflags & SF_CAMERA_PLAYER_POSITION ) + { + UTIL_SetOrigin( this, m_hActivator->pev->origin + m_hActivator->pev->view_ofs ); + pev->angles.x = -m_hActivator->pev->angles.x; + pev->angles.y = m_hActivator->pev->angles.y; + pev->angles.z = 0; + pev->velocity = m_hActivator->pev->velocity; + ClearBits( pev->spawnflags, SF_CAMERA_PLAYER_POSITION ); + } + + //time-based camera + if(m_flWait)pev->teleport_time = gpGlobals->time + m_flWait; + + Move(); + UpdatePlayerView(); + SetNextThink( 0 ); +} \ No newline at end of file diff --git a/server/ents/baseutil.cpp b/server/ents/baseutil.cpp new file mode 100644 index 00000000..07a37dd1 --- /dev/null +++ b/server/ents/baseutil.cpp @@ -0,0 +1,405 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// util_.cpp - utility entities: util_fade, +// util_shake, util_fov etc +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "saverestore.h" +#include "player.h" +#include "defaults.h" +#include "shake.h" + +//===================================================== +// util_changehud: change player hud +//===================================================== +class CUtilChangeHUD : public CBaseLogic +{ + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData* ); + void SetHUD( int hud_num ); + void ResetHUD( void ); + char* GetStringForMode( void ); +}; + +void CUtilChangeHUD :: KeyValue( KeyValueData* pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "hud")) + { + pev->skin = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CUtilChangeHUD :: SetHUD( int hud_num ) +{ + m_iState = STATE_ON; + ((CBasePlayer *)((CBaseEntity *)m_hActivator))->m_iWarHUD = pev->skin = hud_num; +} + +void CUtilChangeHUD :: ResetHUD( void ) +{ + m_iState = STATE_OFF; + ((CBasePlayer *)((CBaseEntity *)m_hActivator))->m_iWarHUD = 0; + ((CBasePlayer *)((CBaseEntity *)m_hActivator))->fadeNeedsUpdate = 1; +} + +void CUtilChangeHUD :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(pActivator && pActivator->IsPlayer())//only at player + { + m_hActivator = pActivator;//save activator + } + else m_hActivator = UTIL_PlayerByIndex( 1 ); + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if (useType == USE_ON ) SetHUD( pev->skin ); + else if (useType == USE_OFF) ResetHUD(); + else if (useType == USE_SET) + { + if(value) SetHUD( value ); + else ResetHUD(); + } + else if (useType == USE_RESET) ResetHUD(); + else if (useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "State: %s, HUD Mode: %s\n", GetStringForState( GetState()), GetStringForMode()); + SHIFT; + } +} + +char* CUtilChangeHUD :: GetStringForMode( void ) +{ + switch(pev->skin) + { + case 0: return "Disable Custom HUD"; + case 1: return "Draw Redeemer HUD"; + case 2: return "Draw Redeemer Underwater HUD"; + case 3: return "Draw Redeemer Noise Screen"; + case 4: return "Draw Security Camera Screen"; + default: return "Draw None"; + } +} +LINK_ENTITY_TO_CLASS( util_changehud, CUtilChangeHUD ); + +//===================================================== +// util_command: activate a console command +//===================================================== +class CUtilCommand : public CBaseEntity +{ +public: + void PostActvaite( void ) + { + if(pev->spawnflags & SF_START_ON) Use( this, this, USE_ON, 0 ); + } + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + char szCommand[256]; + + if (useType == USE_SHOWINFO) + { + ALERT(at_console, "======/Xash Debug System/======\n"); + ALERT(at_console, "classname: %s\n", STRING(pev->classname)); + ALERT(at_console, "Command: %s\n", STRING(pev->netname)); + SHIFT; + } + else if (pev->netname) + { + const char *pString = (char *)STRING( pev->netname ); + int command, namelen = strlen(pString) - 1; + if( namelen > 2) command = pev->netname;//command name + else command = atoi( pString ); + switch( command ) + { + case 0: strcpy( szCommand, "pause\n" ); break; //pause level + case 1: strcpy( szCommand, "reload\n" ); break; //revert to saved game + case 2: strcpy( szCommand, "game_over\n" ); break;//turn to menu + case 3: strcpy( szCommand, "autosave\n" ); break; //autosave game + case 4: strcpy( szCommand, "restart\n" ); break; //restart level + case 5: strcpy( szCommand, "save quick\n" ); break; //emaulate F6 + case 6: strcpy( szCommand, "load quick\n" ); break; //emulate F7 + case 7: strcpy( szCommand, "exit\n" ); break; //return to windows + case 8: strcpy( szCommand, "gametitle\n" ); break; //show game title + case 9: strcpy( szCommand, "intermission\n" ); break; //show intermission + default: sprintf( szCommand, "%s\n", STRING(pev->netname) ); break; //custom command + } + SERVER_COMMAND( szCommand ); + if ( pev->spawnflags & SF_FIREONCE ) UTIL_Remove( this ); + } + } +}; +LINK_ENTITY_TO_CLASS( util_command, CUtilCommand ); + +//========================================================= +// display message text +//========================================================= +class CGameText : public CBaseLogic +{ +public: + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void KeyValue( KeyValueData *pkvd ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; +private: + hudtextparms_t m_textParms; +}; +LINK_ENTITY_TO_CLASS( util_msgtext, CGameText ); + +TYPEDESCRIPTION CGameText::m_SaveData[] = +{ DEFINE_ARRAY( CGameText, m_textParms, FIELD_CHARACTER, sizeof(hudtextparms_t) ), +};IMPLEMENT_SAVERESTORE( CGameText, CBaseLogic ); + + +void CGameText::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "channel")) + { + m_textParms.channel = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "x")) + { + m_textParms.x = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "y")) + { + m_textParms.y = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "effect")) + { + m_textParms.effect = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "color")) + { + int color[4]; + UTIL_StringToIntArray( color, 4, pkvd->szValue ); + m_textParms.r1 = color[0]; + m_textParms.g1 = color[1]; + m_textParms.b1 = color[2]; + m_textParms.a1 = color[3]; + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "color2")) + { + int color[4]; + UTIL_StringToIntArray( color, 4, pkvd->szValue ); + m_textParms.r2 = color[0]; + m_textParms.g2 = color[1]; + m_textParms.b2 = color[2]; + m_textParms.a2 = color[3]; + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "fadein")) + { + m_textParms.fadeinTime = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "fadeout")) + { + m_textParms.fadeoutTime = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "holdtime")) + { + m_textParms.holdTime = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "fxtime")) + { + m_textParms.fxTime = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseLogic::KeyValue( pkvd ); +} + +void CGameText::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + CBaseEntity *pPlayer = NULL; + if ( IsMultiplayer())UTIL_HudMessageAll( m_textParms, STRING(pev->message) ); + else + { + if ( pActivator && pActivator->IsPlayer() ) pPlayer = pActivator; + else pPlayer = UTIL_PlayerByIndex( 1 ); + } + if ( pPlayer ) UTIL_HudMessage( pPlayer, m_textParms, STRING(pev->message)); + if ( pev->spawnflags & SF_FIREONCE ) UTIL_Remove( this ); +} + +//========================================================= +// remove all items +//========================================================= +#define SF_REMOVE_SUIT 1 + +class CUtilStripWeapons : public CPointEntity +{ +public: + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) + { + CBasePlayer *pPlayer = NULL; + + if ( pActivator && pActivator->IsPlayer() ) pPlayer = (CBasePlayer *)pActivator; + else pPlayer = (CBasePlayer *)UTIL_PlayerByIndex( 1 ); + if ( pPlayer ) pPlayer->RemoveAllItems( pev->spawnflags & SF_REMOVE_SUIT ); + if ( pev->spawnflags & SF_FIREONCE ) UTIL_Remove( this ); + } +}; +LINK_ENTITY_TO_CLASS( util_weaponstrip, CUtilStripWeapons ); + +//========================================================= +// set global fog on a map +//========================================================= +class CUtilSetFog : public CBaseLogic +{ +public: + void PostActivate( void ); + void KeyValue( KeyValueData *pkvd ); + void TurnOn( void ); + void TurnOff( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); +}; +LINK_ENTITY_TO_CLASS( util_setfog, CUtilSetFog ); + +void CUtilSetFog :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "startdist")) + { + pev->dmg_take = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "enddist")) + { + pev->dmg_save = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "fadetime")) + { + m_flDelay = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CUtilSetFog :: PostActivate ( void ) +{ + pev->effects |= EF_NODRAW; + if (pev->spawnflags & SF_START_ON) + { + TurnOn(); + ClearBits(pev->spawnflags, SF_START_ON); + } +} + +void CUtilSetFog :: TurnOn ( void ) +{ + m_iState = STATE_ON; + UTIL_SetFogAll(pev->rendercolor, m_flDelay, pev->dmg_take, pev->dmg_save ); +} + +void CUtilSetFog :: TurnOff ( void ) +{ + m_iState = STATE_OFF; + UTIL_SetFogAll(pev->rendercolor, -m_flDelay, pev->dmg_take, pev->dmg_save ); +} + +void CUtilSetFog :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) TurnOn(); + else if ( useType == USE_OFF ) TurnOff(); +} + +//========================================================= +// UTIL_RainModify (set new rain or snow) +//========================================================= +void CUtilRainModify::Spawn() +{ + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + pev->effects |= EF_NODRAW; + + if (IsMultiplayer() || FStringNull( pev->targetname)) + SetBits( pev->spawnflags, SF_START_ON ); +} + +TYPEDESCRIPTION CUtilRainModify::m_SaveData[] = +{ + DEFINE_FIELD( CUtilRainModify, randXY, FIELD_RANGE ), + DEFINE_FIELD( CUtilRainModify, windXY, FIELD_RANGE ), +}; +IMPLEMENT_SAVERESTORE( CUtilRainModify, CPointEntity ); + +void CUtilRainModify::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "drips")) + { + pev->button = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "fadetime")) + { + pev->dmg = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "rand")) + { + randXY = RandomRange(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "wind")) + { + windXY = RandomRange(pkvd->szValue); + pkvd->fHandled = TRUE; + } +} + +void CUtilRainModify::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(useType == USE_SHOWINFO) + { + DEBUGHEAD; + Msg("Wind X: %g Y: %g. Rand X: %g Y: %g\n", windXY[1], windXY[0], randXY[1], randXY[0] ); + Msg("Fade time %g. Drips per second %d\n", pev->dmg, pev->button ); + } + else if(!pev->spawnflags & SF_START_ON) + { + CBasePlayer *pPlayer = (CBasePlayer *)UTIL_PlayerByIndex(1); + if (pev->dmg) + { + pPlayer->Rain_ideal_dripsPerSecond = pev->button; + pPlayer->Rain_ideal_randX = randXY[1]; + pPlayer->Rain_ideal_randY = randXY[0]; + pPlayer->Rain_ideal_windX = windXY[1]; + pPlayer->Rain_ideal_windY = windXY[0]; + pPlayer->Rain_endFade = gpGlobals->time + pev->dmg; + pPlayer->Rain_nextFadeUpdate = gpGlobals->time + 1; + } + else + { + pPlayer->Rain_dripsPerSecond = pev->button; + pPlayer->Rain_randX = randXY[1]; + pPlayer->Rain_randY = randXY[0]; + pPlayer->Rain_windX = windXY[1]; + pPlayer->Rain_windY = windXY[0]; + pPlayer->rainNeedsUpdate = 1; + } + } +} +LINK_ENTITY_TO_CLASS( util_rainmodify, CUtilRainModify ); \ No newline at end of file diff --git a/server/ents/baseweapon.cpp b/server/ents/baseweapon.cpp new file mode 100644 index 00000000..914197fb --- /dev/null +++ b/server/ents/baseweapon.cpp @@ -0,0 +1,1993 @@ +//======================================================================= +// Copyright (C) Shambler Team 2006 +// baseweapon.cpp - player weapon baseclass +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" +#include "monsters.h" +#include "baseweapon.h" +#include "soundent.h" +#include "decals.h" +#include "gamerules.h" +#include "defaults.h" + +//base defines +extern int gEvilImpulse101; +ItemInfo CBasePlayerWeapon::ItemInfoArray[MAX_WEAPONS]; +AmmoInfo CBasePlayerWeapon::AmmoInfoArray[MAX_AMMO_SLOTS]; +extern int gmsgCurWeapon; +extern int gmsgWeapPickup; +char NameItems[MAX_WEAPONS][29]; +int ID[MAX_WEAPONS]; +int GlobalID = 0; +int g_iSwing; + +const char *NAME_VM_PUMP[] = { "pump", }; +const char *NAME_VM_IDLE1[] = { "idle", "idle1", }; +const char *NAME_VM_IDLE2[] = { "fidget", "fidget1", }; +const char *NAME_VM_IDLE3[] = { "idle3", }; +const char *NAME_VM_RELOAD[] = { "reload", }; +const char *NAME_VM_DEPLOY[] = { "draw", "draw1", "deploy" }; +const char *NAME_VM_HOLSTER[] = { "holster", "holster1" }; +const char *NAME_VM_IDLE_EMPTY[] = { "idle2", }; +const char *NAME_VM_START_RELOAD[] = { "start_reload" }; +const char *NAME_VM_DEPLOY_EMPTY[] = { "draw2", }; +const char *NAME_VM_HOLSTER_EMPTY[] = { "holster2" }; +const char *NAME_VM_RANGE_ATTACK1[] = { "fire", "fire1", "shoot", }; +const char *NAME_VM_RANGE_ATTACK2[] = { "fire3", }; +const char *NAME_VM_RANGE_ATTACK3[] = { "fire4", }; +const char *NAME_VM_MELEE_ATTACK1[] = { "fire2", "shoot_big", "grenade" }; +const char *NAME_VM_MELEE_ATTACK2[] = { "fire3", }; +const char *NAME_VM_MELEE_ATTACK3[] = { "fire4", }; + +//*********************************************************** +// main functions () +//*********************************************************** +TYPEDESCRIPTION CBasePlayerWeapon::m_SaveData[] = +{ + DEFINE_FIELD( CBasePlayerWeapon, m_pPlayer, FIELD_CLASSPTR ), + DEFINE_FIELD( CBasePlayerWeapon, m_pNext, FIELD_CLASSPTR ), + DEFINE_FIELD( CBasePlayerWeapon, m_iId, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_flNextPrimaryAttack, FIELD_TIME ), + DEFINE_FIELD( CBasePlayerWeapon, m_flNextSecondaryAttack, FIELD_TIME ), + DEFINE_FIELD( CBasePlayerWeapon, m_flTimeWeaponIdle, FIELD_TIME ), + DEFINE_FIELD( CBasePlayerWeapon, m_flTimeUpdate, FIELD_TIME ), + DEFINE_FIELD( CBasePlayerWeapon, m_iPrimaryAmmoType, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iSecondaryAmmoType, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iClip, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iDefaultAmmo, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iDefaultAmmo2, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iZoom, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_cActiveRocket, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iOnControl, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iStepReload, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iBody, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iSkin, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayerWeapon, m_iSpot, FIELD_INTEGER ), +}; +IMPLEMENT_SAVERESTORE( CBasePlayerWeapon, CBaseAnimating ); + +//generic weapon entity +LINK_ENTITY_TO_CLASS( weapon_generic, CBasePlayerWeapon ); + +//========================================================= +// AttemptToMaterialize - the item is trying to rematerialize, +// should it do so now or wait longer? +//========================================================= +void CBasePlayerWeapon::AttemptToMaterialize( void ) +{ + float time = g_pGameRules->FlWeaponTryRespawn( this ); + + if ( time == 0 ) + { + //Materialize + SetThink( Materialize ); + SetNextThink( 0 ); + return; + } + SetNextThink( time ); +} + +void CBasePlayerWeapon::Materialize( void ) +{ + if ( pev->effects & EF_NODRAW ) + { + // changing from invisible state to visible. + pev->effects &= ~EF_NODRAW; + pev->renderfx = kRenderFxGlowShell; + pev->renderamt = 40; + pev->frags = 0; + pev->rendercolor.x = RANDOM_LONG(25, 255); + pev->rendercolor.y = RANDOM_LONG(25, 255); + pev->rendercolor.z = RANDOM_LONG(25, 255); + pev->scale = 0.01; + SetNextThink (0.001); + } + if( pev->scale > 1.2 ) pev->frags = 1; + if ( pev->frags == 1 ) + { //set effects for respawn item + if(pev->scale > 1.0) pev->scale -= 0.05; + else + { + pev->renderfx = kRenderFxNone; + pev->frags = 0; + pev->solid = SOLID_TRIGGER; + UTIL_SetOrigin( this, pev->origin );// link into world. + SetTouch( DefaultTouch); + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, "items/respawn.wav", 1, ATTN_NORM, 0, 150 ); + SetThink( NULL ); + DontThink(); + } + } + else pev->scale += 0.05; + SetNextThink (0.001); +} + +//========================================================= +// CheckRespawn - a player is taking this weapon, should +// it respawn? +//========================================================= +void CBasePlayerWeapon :: CheckRespawn ( void ) +{ + switch ( g_pGameRules->WeaponShouldRespawn( this ) ) + { + case GR_WEAPON_RESPAWN_YES: + Respawn(); + break; + case GR_WEAPON_RESPAWN_NO: return; + } +} + +//========================================================= +// Respawn- this item is already in the world, but it is +// invisible and intangible. Make it visible and tangible. +//========================================================= +CBaseEntity* CBasePlayerWeapon::Respawn( void ) +{ + // make a copy of this weapon that is invisible and inaccessible to players (no touch function). The weapon spawn/respawn code + // will decide when to make the weapon visible and touchable. + CBaseEntity *pNewWeapon = NULL; + edict_t *pent; + + pent = CREATE_NAMED_ENTITY( pev->classname ); + if ( FNullEnt( pent ) ) + { + //it's a custom weapon! + pent = CREATE_NAMED_ENTITY( MAKE_STRING( "weapon_generic" )); + } + pNewWeapon = Instance( pent ); + + if ( pNewWeapon ) + { + pNewWeapon->pev->owner = pev->owner; + pNewWeapon->pev->origin = g_pGameRules->VecWeaponRespawnSpot( this ); + pNewWeapon->pev->angles = pev->angles; + pNewWeapon->pev->netname = pev->netname; //copy weapon_script name + DispatchSpawn( pNewWeapon->edict() ); + pNewWeapon->pev->effects |= EF_NODRAW;// invisible for now + pNewWeapon->SetTouch( NULL );// no touch + pNewWeapon->SetThink( AttemptToMaterialize ); + + DROP_TO_FLOOR ( ENT(pev) ); + + // not a typo! We want to know when the weapon the player just picked up should respawn! This new entity we created is the replacement, + // but when it should respawn is based on conditions belonging to the weapon that was taken. + pNewWeapon->AbsoluteNextThink( g_pGameRules->FlWeaponRespawnTime( this ) ); + } + else Msg("Respawn failed to create %s!\n", STRING( pev->classname ) ); + + return pNewWeapon; +} + +//========================================================= +// FallThink - Items that have just spawned run this think +// to catch them when they hit the ground. Once we're sure +// that the object is grounded, we change its solid type +// to trigger and set it in a large box that helps the +// player get it. +//========================================================= +void CBasePlayerWeapon::FallThink ( void ) +{ + SetNextThink( 0.1 ); + + if ( pev->flags & FL_ONGROUND ) + { + // clatter if we have an owner (i.e., dropped by someone) + // don't clatter if the gun is waiting to respawn (if it's waiting, it is invisible!) + if ( !FNullEnt( pev->owner ) ) + { + int pitch = 95 + RANDOM_LONG(0,29); + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "items/weapondrop1.wav", 1, ATTN_NORM, 0, pitch); + } + + // lie flat + pev->angles.x = 0; + pev->angles.z = 0; + + pev->solid = SOLID_TRIGGER; + + UTIL_SetOrigin( this, pev->origin );// link into world. + SetTouch( DefaultTouch ); + SetThink (NULL); + } +} + +BOOL CBasePlayerWeapon :: CanDeploy( void ) +{ + BOOL bHasAmmo = 0; + if(iFlags() & ITEM_FLAG_SELECTONEMPTY) return TRUE; + + // this weapon doesn't use ammo, can always deploy. + if ( !stricmp( pszAmmo1(), "none" )) return TRUE; + + if ( pszAmmo1() ) bHasAmmo |= (m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] != 0); + if ( pszAmmo2() ) bHasAmmo |= (m_pPlayer->m_rgAmmo[m_iSecondaryAmmoType] != 0); + if (m_iClip > 0) bHasAmmo |= 1; + + if (!bHasAmmo) return FALSE; + return TRUE; +} + +//========================================================= +// Precache weapons and all resources +//========================================================= +void CBasePlayerWeapon :: Precache( void ) +{ + ItemInfo II; + if(GetItemInfo(&II)) + { + ItemInfoArray[II.iId] = II; + + if ( II.pszAmmo1 && *II.pszAmmo1 )AddAmmoName( II.pszAmmo1 ); + if ( II.pszAmmo2 && *II.pszAmmo2 )AddAmmoName( II.pszAmmo2 ); + + memset( &II, 0, sizeof II ); + + UTIL_PrecacheModel( iViewModel() ); + UTIL_PrecacheModel( iWorldModel() ); + } +} + +//========================================================= +// Get Weapon Info from Script File +//========================================================= +int CBasePlayerWeapon :: GetItemInfo(ItemInfo *p) +{ + if (FStringNull(pev->netname)) pev->netname = pev->classname; + if(ParseWeaponFile( p, STRING(pev->netname))) + { + //make unical weapon number + GenerateID(); + p->iId = m_iId; + return 1; + } + return 0; +} + +//========================================================= +// Base Spawn +//========================================================= +void CBasePlayerWeapon :: Spawn( void ) +{ + Precache(); + + pev->movetype = MOVETYPE_TOSS; + pev->solid = SOLID_BBOX; + pev->sequence = 1;//set world animation + + UTIL_SetOrigin( this, pev->origin ); + UTIL_SetSize(pev, Vector( 0, 0, 0), Vector(0, 0, 0) );//pointsize until it lands on the ground. + + SetTouch( DefaultTouch ); + SetThink( FallThink ); + + UTIL_SetModel(ENT(pev), iWorldModel()); + + m_iSpot = 0; + pev->animtime = gpGlobals->time + 0.1; + b_restored = TRUE; //already restored + + SetNextThink( 0.1 ); +} + +//========================================================= +// Make unical ID number for each weapon +//========================================================= +void CBasePlayerWeapon :: GenerateID( void ) +{ + for( int i = 0; i < GlobalID; i++ ) + { + if( FStrEq( STRING( pev->netname), NameItems[i] )) + { + m_iId = ID[i]; + return; + } + } + + GlobalID++; + m_iId = GlobalID; + strcpy( NameItems[GlobalID-1], STRING( pev->netname )); + ID[GlobalID-1] = m_iId; +} + +//========================================================= +// Default Touch +//========================================================= +void CBasePlayerWeapon::DefaultTouch( CBaseEntity *pOther ) +{ + // if it's not a player, ignore + if ( !pOther->IsPlayer() ) return; + + CBasePlayer *pPlayer = (CBasePlayer *)pOther; + + // can I have this? + if ( !g_pGameRules->CanHavePlayerItem( pPlayer, this ) ) + { + if ( gEvilImpulse101 ) UTIL_Remove( this ); + return; + } + + if (pOther->AddPlayerItem( this )) + { + AttachToPlayer( pPlayer ); + EMIT_SOUND(ENT(pPlayer->pev), CHAN_ITEM, "items/gunpickup2.wav", 1, ATTN_NORM); + } +} + +//========================================================= +// Reset ItemInfo as default +//========================================================= +void CBasePlayerWeapon :: ResetParse( ItemInfo *II ) +{ + m_iId = MAX_WEAPONS; //Will be owerwrite with GenerateID() + II->iSlot = 0; + II->iPosition = 0; + II->iViewModel = MAKE_STRING("models/weapons/v_glock.mdl"); + II->iWorldModel = MAKE_STRING("models/weapons/w_glock.mdl"); + strcpy(II->szAnimExt, "onehanded"); + II->pszAmmo1 = "none"; + II->iMaxAmmo1 = -1; + II->pszAmmo2 = "none"; + II->iMaxAmmo2 = -1; + II->iMaxClip = -1; + II->iFlags = 0; + II->iWeight = 0; + II->attack1 = NONE; + II->attack2 = NONE; + II->fNextAttack = 0.5; + II->fNextAttack2 = 0.5; + memset( II->firesound, 0, sizeof(MAX_SHOOTSOUNDS) ); + memset( II->sfxsound, 0, sizeof(MAX_SHOOTSOUNDS) ); + II->sndcount = 0; + II->sfxcount = 0; + II->emptysnd = iStringNull; + II->punchangle1 = RandomRange(0.0); + II->punchangle2 = RandomRange(0.0); + II->recoil1 = RandomRange(0.0); + II->recoil2 = RandomRange(0.0); + m_iDefaultAmmo = 0; + m_iDefaultAmmo2 = 0; +} + +//========================================================= +// Parse Script File +//========================================================= +int CBasePlayerWeapon :: ParseWeaponFile( ItemInfo *II, const char *filename ) +{ + char path[256]; + int iResult = 0; + + sprintf( path, "scripts/weapons/%s.txt", filename ); + char *pfile = (char *)LOAD_FILE( path, NULL ); + ResetParse( II ); + + if(!pfile) + { + Msg( "Warning: Weapon info file for %s not found!\n", STRING(pev->netname) ); + COM_FreeFile( pfile ); + UTIL_Remove( this ); + return 0; + } + else + { + + II->pszName = STRING(pev->netname); + DevMsg("Parse %s.txt\n", II->pszName ); + // parses the type, moves the file pointer + iResult = ParseWeaponData( II, pfile ); + DevMsg("Parsing: WeaponData{} %s\n", iResult ? "OK" : "ERROR" ); + iResult = ParsePrimaryAttack( II, pfile ); + DevMsg("Parsing: PrimaryAttack{} %s\n", iResult ? "OK" : "ERROR" ); + iResult = ParseSecondaryAttack( II, pfile ); + DevMsg("Parsing: SecondaryAttack{} %s\n", iResult ? "OK" : "ERROR" ); + iResult = ParseSoundData( II, pfile ); + DevMsg("Parsing: SoundData{} %s\n", iResult ? "OK" : "ERROR" ); + COM_FreeFile( pfile ); + return 1; + } +} + +//========================================================= +// Parse SoundData {} section +//========================================================= +int CBasePlayerWeapon :: ParseSoundData( ItemInfo *II, char *pfile ) +{ + char token[256]; + int i = 0; + int j = 0; + + while ( stricmp( token, "SoundData") ) //search SoundData header + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + while ( stricmp( token, "{") ) //search { + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + + pfile = COM_ParseFile(pfile, token); + while ( stricmp( token, "}" ) ) + { + if ( !stricmp( token, "firesound" )) + { + pfile = COM_ParseFile(pfile, token); + if(i < MAX_SHOOTSOUNDS) + { + II->firesound[i] = ALLOC_STRING(token);//save path + UTIL_PrecacheSound(ALLOC_STRING(token));//precache sound + i++; + } + else + { + Msg("Warning! Too many shoot sounds for %s\n", STRING(pev->netname)); + break;//syntax error, stop parsing + } + II->sndcount = i; + } + else if ( !stricmp( token, "sfxsound" )) + { + pfile = COM_ParseFile(pfile, token); + if(j < MAX_SHOOTSOUNDS) + { + II->sfxsound[j] = ALLOC_STRING(token);//save path + UTIL_PrecacheSound(ALLOC_STRING(token));//precache sound + j++; + } + else + { + Msg("Warning! Too many sfx sounds for %s\n", STRING(pev->netname)); + break;//syntax error, stop parsing + } + II->sfxcount = j; + } + else if ( !stricmp( token, "emptysound" )) + { + pfile = COM_ParseFile(pfile, token); + UTIL_PrecacheSound(ALLOC_STRING(token));//precache sound + II->emptysnd = ALLOC_STRING(token); + } + pfile = COM_ParseFile(pfile, token); + } + return 1; +} + +//========================================================= +// Parse SecondaryAttack {} section +//========================================================= +int CBasePlayerWeapon :: ParseSecondaryAttack( ItemInfo *II, char *pfile ) +{ + char token[256]; + while ( stricmp( token, "SecondaryAttack") ) //search SecondaryAttack header + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + while ( stricmp( token, "{") ) //search { + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + + pfile = COM_ParseFile(pfile, token); + while ( stricmp( token, "}" ) ) + { + if ( !stricmp( token, "action" )) + { + pfile = COM_ParseFile(pfile, token); + if ( !stricmp( token, "none" )) II->attack2 = NONE; + else if ( !stricmp( token, "ammo1" )) II->attack2 = AMMO1; + else if ( !stricmp( token, "ammo1." )) + { + II->attack2 = AMMO1; + SetBits(pev->impulse, SATTACK_FIREMODE1); + } + else if ( !stricmp( token, "ammo2" )) II->attack2 = AMMO2; + else if ( !stricmp( token, "ammo2." )) + { + II->attack2 = AMMO2; + SetBits(pev->impulse, SATTACK_FIREMODE1); + } + else if ( !stricmp( token, "laserdot" )) II->attack2 = LASER_DOT; + else if ( !stricmp( token, "zoom" )) II->attack2 = ZOOM; + else if ( !stricmp( token, "flashlight" )) II->attack2 = FLASHLIGHT; + else if ( !stricmp( token, "switchmode" )) II->attack2 = SWITCHMODE; + else if ( !stricmp( token, "swing" )) II->attack2 = SWING; + else II->attack2 = ALLOC_STRING( token ); //client command + } + else if ( !stricmp( token, "punchangle" )) + { + pfile = COM_ParseFile(pfile, token); + II->punchangle2 = RandomRange((char *)STRING(ALLOC_STRING(token))); + } + else if ( !stricmp( token, "recoil" )) + { + pfile = COM_ParseFile(pfile, token); + II->recoil2 = RandomRange((char *)STRING(ALLOC_STRING(token))); + } + else if ( !stricmp( token, "nextattack" )) + { + pfile = COM_ParseFile(pfile, token); + II->fNextAttack2 = atof(token); + } + pfile = COM_ParseFile(pfile, token); + } + return 1; +} + +//========================================================= +// Parse PrimaryAttack {} section +//========================================================= +int CBasePlayerWeapon :: ParsePrimaryAttack( ItemInfo *II, char *pfile ) +{ + char token[256]; + + while ( stricmp( token, "PrimaryAttack") ) //search PrimaryAttack header + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + while ( stricmp( token, "{") ) //search { + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + + pfile = COM_ParseFile(pfile, token); + while ( stricmp( token, "}" ) ) + { + if ( !stricmp( token, "action" )) + { + pfile = COM_ParseFile(pfile, token); + if ( !stricmp( token, "none" )) II->attack1 = NONE; + else if ( !stricmp( token, "ammo1" )) II->attack1 = AMMO1; + else if ( !stricmp( token, "ammo1." )) + { + II->attack1 = AMMO1; + SetBits(pev->impulse, PATTACK_FIREMODE1); + } + else if ( !stricmp( token, "ammo2" )) II->attack1 = AMMO2; + else if ( !stricmp( token, "ammo2." )) + { + II->attack1 = AMMO2; + SetBits(pev->impulse, PATTACK_FIREMODE1); + } + else if ( !stricmp( token, "laserdot" )) II->attack1 = LASER_DOT; + else if ( !stricmp( token, "zoom" )) II->attack1 = ZOOM; + else if ( !stricmp( token, "flashlight" )) II->attack1 = FLASHLIGHT; + else if ( !stricmp( token, "switchmode" )) II->attack1 = SWITCHMODE; + else if ( !stricmp( token, "swing" )) II->attack1 = SWING; + else II->attack1 = ALLOC_STRING( token ); //client command + } + else if ( !stricmp( token, "punchangle" )) + { + pfile = COM_ParseFile(pfile, token); + II->punchangle1 = RandomRange((char *)STRING(ALLOC_STRING(token))); + } + else if ( !stricmp( token, "recoil" )) + { + pfile = COM_ParseFile(pfile, token); + II->recoil1 = RandomRange((char *)STRING(ALLOC_STRING(token))); + } + else if ( !stricmp( token, "nextattack" )) + { + pfile = COM_ParseFile(pfile, token); + II->fNextAttack = atof(token); + } + pfile = COM_ParseFile(pfile, token); + } + return 1; +} + +//========================================================= +// Parse WeaponData {} section +//========================================================= +int CBasePlayerWeapon :: ParseWeaponData( ItemInfo *II, char *pfile ) +{ + char token[256]; + + while ( stricmp( token, "WeaponData") ) + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + while ( stricmp( token, "{") ) //search { + { + if(!pfile) return 0; + pfile = COM_ParseFile(pfile, token); + } + + pfile = COM_ParseFile(pfile, token); + while ( stricmp( token, "}" ) ) + { + if (!pfile) break; + + if ( !stricmp( token, "viewmodel" )) + { + pfile = COM_ParseFile(pfile, token); + II->iViewModel = ALLOC_STRING(token); + } + else if ( !stricmp( token, "playermodel" )) + { + pfile = COM_ParseFile(pfile, token); + II->iWorldModel = ALLOC_STRING(token); + } + else if ( !stricmp( token, "anim_prefix" )) + { + pfile = COM_ParseFile(pfile, token); + strcpy(II->szAnimExt, token); + } + else if ( !stricmp( token, "bucket" )) + { + pfile = COM_ParseFile(pfile, token); + II->iSlot = atoi(token); + } + else if ( !stricmp( token, "bucket_position" )) + { + pfile = COM_ParseFile(pfile, token); + II->iPosition = atoi(token); + } + else if ( !stricmp( token, "clip_size" )) + { + pfile = COM_ParseFile(pfile, token); + if ( !stricmp( token, "noclip" ) ) II->iMaxClip = -1; + else II->iMaxClip = atoi(token); + } + else if ( !stricmp( token, "primary_ammo" )) + { + pfile = COM_ParseFile(pfile, token); + + if ( !stricmp( token, "none" )) II->iMaxAmmo1 = -1; + else if ( !stricmp( token, "357" )) II->iMaxAmmo1 = DESERT_MAX_CARRY; + else if ( !stricmp( token, "9mm" )) II->iMaxAmmo1 = GLOCK_MAX_CARRY; + else if ( !stricmp( token, "12mm" )) II->iMaxAmmo1 = GLOCK_MAX_CARRY; + else if ( !stricmp( token, "762" )) II->iMaxAmmo1 = M40A1_MAX_CARRY; + else if ( !stricmp( token, "m203" )) II->iMaxAmmo1 = M203_GRENADE_MAX_CARRY; + else if ( !stricmp( token, "nuke" )) II->iMaxAmmo1 = ROCKET_MAX_CARRY; + else if ( !stricmp( token, "rockets" )) II->iMaxAmmo1 = ROCKET_MAX_CARRY; + else if ( !stricmp( token, "556" )) II->iMaxAmmo1 = GLOCK_MAX_CARRY; + else if ( !stricmp( token, "buckshot" )) II->iMaxAmmo1 = BUCKSHOT_MAX_CARRY; + else if ( !stricmp( token, "grenade" )) II->iMaxAmmo1 = M203_GRENADE_MAX_CARRY; + else if ( !stricmp( token, "bolts" )) II->iMaxAmmo1 = BOLT_MAX_CARRY; + + char *psz = new char[16]; + strcpy(psz, token); + II->pszAmmo1 = const_cast(psz); + } + else if ( !stricmp( token, "secondary_ammo" )) + { + pfile = COM_ParseFile(pfile, token); + + if ( !stricmp( token, "none" )) II->iMaxAmmo2 = -1; + else if ( !stricmp( token, "357" )) II->iMaxAmmo2 = DESERT_MAX_CARRY; + else if ( !stricmp( token, "9mm" )) II->iMaxAmmo2 = GLOCK_MAX_CARRY; + else if ( !stricmp( token, "12mm" )) II->iMaxAmmo2 = GLOCK_MAX_CARRY; + else if ( !stricmp( token, "762" )) II->iMaxAmmo2 = M40A1_MAX_CARRY; + else if ( !stricmp( token, "m203" )) II->iMaxAmmo2 = M203_GRENADE_MAX_CARRY; + else if ( !stricmp( token, "nuke" )) II->iMaxAmmo2 = ROCKET_MAX_CARRY; + else if ( !stricmp( token, "rockets" )) II->iMaxAmmo2 = ROCKET_MAX_CARRY; + else if ( !stricmp( token, "556" )) II->iMaxAmmo2 = GLOCK_MAX_CARRY; + else if ( !stricmp( token, "buckshot" )) II->iMaxAmmo2 = BUCKSHOT_MAX_CARRY; + else if ( !stricmp( token, "grenade" )) II->iMaxAmmo1 = M203_GRENADE_MAX_CARRY;//don't change this! + else if ( !stricmp( token, "bolts" )) II->iMaxAmmo2 = BOLT_MAX_CARRY; + + char *psz = new char[16]; + strcpy(psz, token); + II->pszAmmo2 = const_cast(psz); + } + else if ( !stricmp( token, "defaultammo" )) + { + pfile = COM_ParseFile(pfile, token); + m_iDefaultAmmo = RandomRange((char*)STRING(ALLOC_STRING(token))).Random(); + } + else if ( !stricmp( token, "defaultammo2" )) + { + pfile = COM_ParseFile(pfile, token); + m_iDefaultAmmo2 = RandomRange((char*)STRING(ALLOC_STRING(token))).Random(); + } + else if ( !stricmp( token, "weight" )) + { + pfile = COM_ParseFile(pfile, token); + II->iWeight = atoi(token); + } + else if ( !stricmp( token, "item_flags" )) + { + pfile = COM_ParseFile(pfile, token); + II->iFlags = atoi(token); + } + pfile = COM_ParseFile(pfile, token); + } + return 1; +} + +//========================================================= +// Set Animation at ACT +//========================================================= +int CBasePlayerWeapon :: PlaySequence( Activity activity, float fps ) +{ + if(!m_pPlayer->pev->viewmodel) return -1; + UTIL_SetModel( ENT(pev), STRING( m_pPlayer->pev->viewmodel )); + int iSequence = LookupActivity( activity ); + if(iSequence != -1) SendWeaponAnim(iSequence, fps); + else DevMsg("Warning: %s not found\n", activity_map[activity - 1].name); + + return iSequence; +} + +//========================================================= +// Set Animation at name +//========================================================= +int CBasePlayerWeapon :: SetAnimation( char *name, float fps ) +{ + if(!m_pPlayer->pev->viewmodel) return -1; + UTIL_SetModel( ENT(pev), STRING( m_pPlayer->pev->viewmodel )); + int iSequence = LookupSequence( name ); + if(iSequence != -1) SendWeaponAnim(iSequence, fps); + else DevMsg("Warning: Sequence \"%s\" not found\n", name); + + return iSequence; +} + +//========================================================= +// Found sequence at act or name (generic method) +//========================================================= +int CBasePlayerWeapon :: SetAnimation( Activity activity, float fps ) +{ + int iSequence, m_iAnimCount; + const char **pAnimsList = NULL; + + if(!m_pPlayer->pev->viewmodel) return -1; + UTIL_SetModel( ENT(pev), STRING( m_pPlayer->pev->viewmodel )); + + //try to playing ACT sequence + iSequence = LookupActivity( activity ); + if(iSequence != -1) + { + SendWeaponAnim(iSequence, fps); //activity method + return iSequence; + } + + //ACT not found, translate name to ACT + switch ( activity ) + { + case ACT_VM_IDLE1: + pAnimsList = NAME_VM_IDLE1; + m_iAnimCount = ARRAYSIZE( NAME_VM_IDLE1 ); + break; + case ACT_VM_IDLE2: + pAnimsList = NAME_VM_IDLE2; + m_iAnimCount = ARRAYSIZE( NAME_VM_IDLE2 ); + break; + case ACT_VM_IDLE3: + pAnimsList = NAME_VM_IDLE3; + m_iAnimCount = ARRAYSIZE( NAME_VM_IDLE3 ); + break; + case ACT_VM_IDLE_EMPTY: + pAnimsList = NAME_VM_IDLE_EMPTY; + m_iAnimCount = ARRAYSIZE( NAME_VM_IDLE_EMPTY ); + break; + case ACT_VM_RANGE_ATTACK1: + pAnimsList = NAME_VM_RANGE_ATTACK1; + m_iAnimCount = ARRAYSIZE( NAME_VM_RANGE_ATTACK1 ); + break; + case ACT_VM_RANGE_ATTACK2: + pAnimsList = NAME_VM_RANGE_ATTACK2; + m_iAnimCount = ARRAYSIZE( NAME_VM_RANGE_ATTACK2 ); + break; + case ACT_VM_RANGE_ATTACK3: + pAnimsList = NAME_VM_RANGE_ATTACK3; + m_iAnimCount = ARRAYSIZE( NAME_VM_RANGE_ATTACK3 ); + break; + case ACT_VM_MELEE_ATTACK1: + pAnimsList = NAME_VM_MELEE_ATTACK1; + m_iAnimCount = ARRAYSIZE( NAME_VM_MELEE_ATTACK1 ); + break; + case ACT_VM_MELEE_ATTACK2: + pAnimsList = NAME_VM_MELEE_ATTACK2; + m_iAnimCount = ARRAYSIZE( NAME_VM_MELEE_ATTACK2 ); + break; + case ACT_VM_MELEE_ATTACK3: + pAnimsList = NAME_VM_MELEE_ATTACK3; + m_iAnimCount = ARRAYSIZE( NAME_VM_MELEE_ATTACK3 ); + break; + case ACT_VM_PUMP: + pAnimsList = NAME_VM_PUMP; + m_iAnimCount = ARRAYSIZE( NAME_VM_PUMP ); + break; + case ACT_VM_START_RELOAD: + pAnimsList = NAME_VM_START_RELOAD; + m_iAnimCount = ARRAYSIZE( NAME_VM_START_RELOAD ); + break; + case ACT_VM_RELOAD: + pAnimsList = NAME_VM_RELOAD; + m_iAnimCount = ARRAYSIZE( NAME_VM_RELOAD ); + break; + case ACT_VM_DEPLOY: + pAnimsList = NAME_VM_DEPLOY; + m_iAnimCount = ARRAYSIZE( NAME_VM_DEPLOY ); + break; + case ACT_VM_DEPLOY_EMPTY: + pAnimsList = NAME_VM_DEPLOY_EMPTY; + m_iAnimCount = ARRAYSIZE( NAME_VM_DEPLOY_EMPTY ); + break; + case ACT_VM_HOLSTER: + pAnimsList = NAME_VM_HOLSTER; + m_iAnimCount = ARRAYSIZE( NAME_VM_HOLSTER ); + break; + case ACT_VM_HOLSTER_EMPTY: + pAnimsList = NAME_VM_HOLSTER_EMPTY; + m_iAnimCount = ARRAYSIZE( NAME_VM_HOLSTER_EMPTY ); + break; + default: + m_iAnimCount = 0; + break; + } + + //lookup all names + for(int i = 0; i < m_iAnimCount; i++ ) + { + iSequence = LookupSequence( pAnimsList[i] ); + if(iSequence != -1) + { + SendWeaponAnim(iSequence, fps); //names method + return iSequence; + } + } + + return -1; //no matches found +} + +//========================================================= +// Set Weapon Anim +//========================================================= +void CBasePlayerWeapon :: SendWeaponAnim( int sequence, float fps ) +{ + dstudiohdr_t *pstudiohdr; + dstudioseqdesc_t *pseqdesc; + pstudiohdr = (dstudiohdr_t *)GET_MODEL_PTR( ENT(pev) ); + + //calculate additional body for special effects + pev->body = (pev->body % NUM_HANDS) + NUM_HANDS * m_iBody; + + MESSAGE_BEGIN( MSG_ONE, SVC_WEAPONANIM, NULL, m_pPlayer->pev ); + WRITE_BYTE( sequence ); + WRITE_BYTE( pev->body ); + MESSAGE_END(); + + if(pstudiohdr) + { + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + (int)sequence; + if(fps) pseqdesc->fps = fps; + } + + m_pPlayer->pev->weaponanim = sequence; + SetNextIdle(SequenceDuration()); +} + +//========================================================= +// generic base functions +//========================================================= +BOOL CBasePlayerWeapon :: DefaultDeploy( Activity sequence ) +{ + if ( SUIT ) pev->body = TRUE; + else pev->body = FALSE; + + m_iClientSkin = -1;//reset last skin info for new weapon + m_iClientBody = -1;//reset last skin info for new weapon + + m_pPlayer->pev->viewmodel = iViewModel(); + m_pPlayer->pev->weaponmodel = iWorldModel(); + strcpy( m_pPlayer->m_szAnimExtention, szAnimExt() ); + + if(SetAnimation( sequence ) != -1) + { + SetNextAttack(SequenceDuration() + 0.5);//delay before idle playing + return TRUE; + } + + //animation not found + return FALSE; +} + +BOOL CBasePlayerWeapon :: DefaultHolster( Activity sequence ) +{ + m_fInReload = FALSE; + int iResult = 0; + + iResult = SetAnimation( sequence ); + if(iResult == -1) return 0; + + SetNextAttack(SequenceDuration() + 0.1);//delay before switching + if(m_pSpot)//disable laser dot + { + EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/spot_off.wav", 1, ATTN_NORM); + m_pSpot->Killed(); + m_pSpot = NULL; + } + ZoomReset(); + m_iSkin = 0;//reset screen + + if(iAttack1() == FLASHLIGHT || iAttack2() == FLASHLIGHT) ClearBits(m_pPlayer->pev->effects, EF_DIMLIGHT); + + if ( (iFlags() & ITEM_FLAG_EXHAUSTIBLE) && (m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] <= 0) ) + { + // no more ammo! + m_pPlayer->pev->weapons &= ~(1<pev->viewmodel = iStringNull; + m_pPlayer->pev->weaponmodel = iStringNull; + SetThink( DestroyItem ); + SetNextThink( 0.5 ); + } + + //animation not found + return 1; +} + +void CBasePlayerWeapon :: DefaultIdle( void ) +{ + // weapon have clip and ammo or just have ammo + if ((iMaxClip() && m_iClip) || m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] > 0 || iMaxAmmo1() == -1) + { + //play random idle animation + int iAnim; + float flRand = RANDOM_FLOAT(0, 1.2); + if (flRand < 0.2) iAnim = ACT_VM_IDLE1; + else if ( 0.4 > flRand && flRand > 0.2 ) iAnim = ACT_VM_IDLE2; + else if ( 0.8 > flRand && flRand > 0.5 ) iAnim = ACT_VM_IDLE3; + else + { + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + RANDOM_LONG(10, 15); + return;//don't execute SetAnimation + } + SetAnimation((Activity)iAnim); + } + else + { + SetAnimation( ACT_VM_IDLE_EMPTY ); + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + RANDOM_LONG(20, 45); + } +} + +BOOL CBasePlayerWeapon :: DefaultReload( Activity sequence ) +{ + if (m_cActiveRocket && m_iSpot ) return FALSE; + if (m_flNextPrimaryAttack > UTIL_WeaponTimeBase()) return FALSE; + if (m_flNextSecondaryAttack > UTIL_WeaponTimeBase()) return FALSE; + if(m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] > 0)//have ammo? + { + if(iMaxClip() == m_iClip) return FALSE; + if (m_iStepReload == 0)//try to playing step reload + { + if(SetAnimation( ACT_VM_START_RELOAD ) != -1) + { + //found anim, continue + m_iStepReload = 1; + m_fInReload = TRUE;//disable reload button + return TRUE;//start reload cycle. See also ItemPostFrame + } + else // init default reload + { + int i = min(iMaxClip() - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]); + if (i == 0) return FALSE; + int iResult = -1; + ZoomReset();//reset zoom + if(m_iClip <= 0)//empty clip ? + { + //iResult is error code + iResult = SetAnimation( ACT_VM_RELOAD_EMPTY ); + m_iStepReload = EMPTY_RELOAD;//it's empty reload + } + if(iResult == -1) + { + SetAnimation( sequence ); + m_iStepReload = NORMAL_RELOAD;//it's not empty reload + } + if(m_pSpot)m_pSpot->Suspend(SequenceDuration());//suspend laserdot + SetNextAttack(SequenceDuration()); + m_fInReload = TRUE;//disable reload button + + return TRUE;//that's all right + } + } + else if (m_iStepReload == 1)//continue step reload + { + if (m_flTimeWeaponIdle > UTIL_WeaponTimeBase()) return FALSE; + // was waiting for gun to move to side + SetAnimation( sequence ); + m_iStepReload = 2; + } + else if (m_iStepReload == 2)//continue step reload + { + // Add them to the clip + m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]--; + m_iClip++; + m_iStepReload = 1; + if(m_iClip == iMaxClip()) m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.1; + } + return TRUE; + } + return FALSE; +} + +BOOL CBasePlayerWeapon :: PlayEmptySound( void ) +{ + if (m_iPlayEmptySound) + { + if(EmptySnd()) EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, STRING(EmptySnd()), 1, ATTN_NORM); + m_iPlayEmptySound = 0; + return TRUE; + } + return FALSE; +} +//========================================================= +// Get Atatck Info by AmmoInfo +//========================================================= +Vector CBasePlayerWeapon :: GetCurrentSpread( const char *ammo ) +{ + int iBullet = GetBulletType( ammo ); + if( iBullet == BULLET_9MM) return VECTOR_CONE_2DEGREES; + else if( iBullet == BULLET_357) return VECTOR_CONE_2DEGREES; + else if( iBullet == BULLET_556) return VECTOR_CONE_3DEGREES; + else if( iBullet == BULLET_762) return VECTOR_CONE_1DEGREES; + else if( iBullet == BULLET_12MM) return VECTOR_CONE_15DEGREES; + else if( iBullet == BULLET_BUCKSHOT) return VECTOR_CONE_10DEGREES; + + return VECTOR_CONE_0DEGREES; +} + +int CBasePlayerWeapon :: GetBulletType( const char *ammo ) +{ + if( !stricmp( ammo, "9mm" )) return BULLET_9MM; + else if( !stricmp( ammo, "357" )) return BULLET_357; + else if( !stricmp( ammo, "556" )) return BULLET_556; + else if( !stricmp( ammo, "762" )) return BULLET_762; + else if( !stricmp( ammo, "12mm" )) return BULLET_12MM; + else if( !stricmp( ammo, "buckshot" )) return BULLET_BUCKSHOT; + + return BULLET_NONE; +} + +int CBasePlayerWeapon :: GetAmmoType( const char *ammo ) +{ + const char *ammoname; + ammoname = m_pPlayer->GetAmmoName(m_iPrimaryAmmoType); + if(!stricmp( ammo, ammoname )) return 1; + ammoname = m_pPlayer->GetAmmoName(m_iSecondaryAmmoType); + if(!stricmp( ammo, ammoname )) return 2; + return 0; +} + +int CBasePlayerWeapon :: ReturnAmmoIndex( const char *ammo ) +{ + const char *ammoname; + ammoname = m_pPlayer->GetAmmoName(m_iPrimaryAmmoType); + if(!stricmp( ammo, ammoname )) return m_iPrimaryAmmoType; + ammoname = m_pPlayer->GetAmmoName(m_iSecondaryAmmoType); + if(!stricmp( ammo, ammoname )) return m_iSecondaryAmmoType; + return 0; +} + +int CBasePlayerWeapon :: GetCurrentAttack( const char *ammo, int firemode ) +{ + int iResult; + if( !stricmp( ammo, "none" )) //no ammo + { + //just play animation and sound + if(!firemode) iResult = PlayRangeAttack();//anim is present ? + else iResult = PlayMeleeAttack(); + + SetPlayerEffects( ammo, firemode ); + PlayAttackSound( firemode ); + } + else if( !stricmp( ammo, "9mm" )) iResult = Shoot ( ammo, GetCurrentSpread(ammo), firemode ); + else if( !stricmp( ammo, "357" )) iResult = Shoot ( ammo, GetCurrentSpread(ammo), firemode ); + else if( !stricmp( ammo, "556" )) iResult = Shoot ( ammo, GetCurrentSpread(ammo), firemode ); + else if( !stricmp( ammo, "762" )) iResult = Shoot ( ammo, GetCurrentSpread(ammo), firemode ); + else if( !stricmp( ammo, "12mm" )) iResult = Shoot ( ammo, GetCurrentSpread(ammo), firemode ); + else if( !stricmp( ammo, "buckshot" )) + { + if(firemode) iResult = Shoot ( ammo, GetCurrentSpread(ammo), m_iClip == 1 ? 0 : 1, 2 );//HACK + else iResult = Shoot ( ammo, GetCurrentSpread(ammo) ); + } + else if( !stricmp( ammo, "rockets" )) iResult = Launch( ammo, firemode ); + else if( !stricmp( ammo, "m203" )) iResult = Launch( ammo, firemode ); + else if( !stricmp( ammo, "nuke" )) iResult = Launch( ammo, firemode ); + else if( !stricmp( ammo, "bolts" )) iResult = Launch( ammo, firemode ); + else if( !stricmp( ammo, "uranium" )) iResult = -1; + else if( !stricmp( ammo, "grenade" )) + { + if ( !m_flHoldTime && m_pPlayer->m_rgAmmo[ ReturnAmmoIndex( ammo ) ] > 0 ) + { + m_flHoldTime = UTIL_WeaponTimeBase();//set hold time + m_iOnControl = 3; //grenade "on control" + SetAnimation( ACT_VM_START_CHARGE ); //pinpull + } + iResult = -1; + } + return iResult; +} + +//========================================================= +// Play Action +//========================================================= +int CBasePlayerWeapon :: PlayCurrentAttack( int action, int firemode ) +{ + int iResult = 0; + if(pev->button)firemode = m_iBody;//enable switching firemode + + if(action == ZOOM) iResult = 1;//See void ZoomUpdate( void ); for details + else if(action == AMMO1) iResult = GetCurrentAttack( pszAmmo1(), firemode ); + else if(action == AMMO2) iResult = GetCurrentAttack( pszAmmo2(), firemode ); + else if(action == LASER_DOT) + { + m_iSpot = !m_iSpot; + if(!m_iSpot && m_pSpot)//disable laser dot + { + EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/spot_off.wav", 1, ATTN_NORM); + m_pSpot->Killed(); + m_pSpot = NULL; + m_iSkin = 0; //disable screen + } + iResult = 1; + } + else if(action == FLASHLIGHT) + { + if(FBitSet(m_pPlayer->pev->effects, EF_DIMLIGHT)) + { + ClearBits(m_pPlayer->pev->effects, EF_DIMLIGHT); + EMIT_SOUND_DYN( ENT(m_pPlayer->pev), CHAN_WEAPON, SOUND_FLASHLIGHT_OFF, 1.0, ATTN_NORM, 0, PITCH_NORM ); + } + else + { + SetBits(m_pPlayer->pev->effects, EF_DIMLIGHT); + EMIT_SOUND_DYN( ENT(m_pPlayer->pev), CHAN_WEAPON, SOUND_FLASHLIGHT_ON, 1.0, ATTN_NORM, 0, PITCH_NORM ); + } + iResult = 1; + } + else if( action == SWITCHMODE) + { + if(!m_iBody) + { + m_iBody = 1; + pev->button = 1;//enable custom firemode + SetAnimation(ACT_VM_TURNON); + } + else + { + SetAnimation( ACT_VM_TURNOFF ); + pev->button = 0;//disable custom firemode + m_iClientBody = m_iBody = 0;//make delay before change + } + iResult = 1; + } + else if( action == SWING) + { + if (!Swing(TRUE)) Swing (FALSE); + iResult = 1; + } + else if( action == NONE) return -1; + else //just command + { + char command[64]; + if(!strncmp( STRING(action), "fire ", 5)) + { + char *target = (char*)STRING(action); + target = target + 5;//remove "fire " + UTIL_FireTargets( target, m_pPlayer, this, USE_TOGGLE );//activate target + } + else + { + sprintf( command, "%s\n", STRING(action)); + SERVER_COMMAND( command ); + } + //just play animation and sound + if(!firemode) iResult = PlayRangeAttack();//anim is present ? + else iResult = PlayMeleeAttack(); + + m_pPlayer->SetAnimation( PLAYER_ATTACK1 );//player animation + PlayAttackSound( firemode ); + } + return iResult; +} + +int CBasePlayerWeapon :: UseAmmo( const char *ammo, int count ) +{ + int ammoType = GetAmmoType( ammo ); + + if( !ammoType ) return -1;//uncnown ammo ? + else if( ammoType == 1)//it's primary ammo + { + if(iMaxClip() == -1 && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] > 0)//may be we have clip ? + { + if( m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] < count) count = 1; + m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= count; + } + else if(m_iClip > 0)//have clip + { + if( m_iClip < count) count = 1; + m_iClip -= count; + } + else return 0;//ammo is out + } + else if( ammoType == 2 ) //it's secondary ammo + { + if(m_pPlayer->m_rgAmmo[m_iSecondaryAmmoType] > 0) //we have ammo ? + { + if( m_pPlayer->m_rgAmmo[m_iSecondaryAmmoType] < count) count = 1; + m_pPlayer->m_rgAmmo[m_iSecondaryAmmoType] -= count; + } + else return 0;//ammo is out + } + return 1;//thats all right, mr.freeman +} + +void CBasePlayerWeapon :: SetPlayerEffects( const char *ammo, int firemode ) +{ + if( !stricmp( ammo, "9mm" )) + { + if(firemode)//add silencer + { + m_pPlayer->m_iWeaponVolume = QUIET_GUN_VOLUME; + m_pPlayer->m_iWeaponFlash = DIM_GUN_FLASH; + } + else + { + m_pPlayer->m_iWeaponVolume = LOUD_GUN_VOLUME; + m_pPlayer->m_iWeaponFlash = BRIGHT_GUN_FLASH; + } + } + else if( !stricmp( ammo, "762" )) //sniper rifle + { + m_pPlayer->m_iWeaponVolume = QUIET_GUN_VOLUME; + m_pPlayer->m_iWeaponFlash = NO_GUN_FLASH; + } + else if( !stricmp( ammo, "buckshot" )) + { + if(firemode) //double barrels attack + { + m_pPlayer->m_iWeaponVolume = LOUD_GUN_VOLUME; + m_pPlayer->m_iWeaponFlash = BRIGHT_GUN_FLASH; + } + else + { + m_pPlayer->m_iWeaponVolume = NORMAL_GUN_VOLUME; + m_pPlayer->m_iWeaponFlash = NORMAL_GUN_FLASH; + } + } + else + { + m_pPlayer->m_iWeaponVolume = LOUD_GUN_VOLUME; + m_pPlayer->m_iWeaponFlash = BRIGHT_GUN_FLASH; + } + m_pPlayer->SetAnimation( PLAYER_ATTACK1 );//player animation +} + +void CBasePlayerWeapon :: PlayAttackSound( int firemode ) +{ + if(firemode) //play sfx random sounds + { + if(sfxcnt()) + { + int sfxsound = RANDOM_LONG(0, sfxcnt() - 1); + EMIT_SOUND(ENT(pev), CHAN_BODY, STRING(SfxSound(sfxsound)), 1.0, ATTN_NORM); + } + } + else //play normal random fire sound + { + if(sndcnt()) + { + int firesound = RANDOM_LONG(0, sndcnt() - 1); + EMIT_SOUND(ENT(pev), CHAN_BODY, STRING(FireSound(firesound)), 1.0, ATTN_NORM); + } + } +} + +//========================================================= +// Shoot & Launch functions +//========================================================= +int CBasePlayerWeapon :: Shoot ( const char *ammo, Vector vecSpread, int firemode, int cShots ) +{ + if ( m_pPlayer->pev->waterlevel != 3)//have ammo and player not underwater + { + if(!UseAmmo( ammo, cShots )) return 0; + + SetPlayerEffects( ammo, firemode ); + PlayAttackSound( firemode ); + + //viewmodel animation + ZoomReset(); + if(!PlayEmptyFire()) + { + if(!firemode)PlayRangeAttack(); + else PlayMeleeAttack(); + } + + float flDistance;//set max distance + + Vector vecSrc = m_pPlayer->GetGunPosition( ); + Vector vecAiming = gpGlobals->v_forward; + Vector vecDir; + + // FIXME: wrote GetBulletShellModel + int model = PRECACHE_MODEL( "models/shell9mm.mdl" ); + + //eject brass + for( int i = 0; cShots > i; i++ ) + { + SFX_EjectBrass( vecSrc, m_pPlayer->pev->velocity, RANDOM_FLOAT( 0, 360 ), model, BOUNCE_SHELL ); + } + if( !stricmp( ammo, "buckshot" ))//HACK + { + flDistance = 2048; + cShots *= 4; + } + else flDistance = MAP_SIZE; + FireBullets( cShots, vecSrc, vecAiming, vecSpread, flDistance, (Bullet)GetBulletType(ammo), 4, 0, m_pPlayer->pev ); + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + RANDOM_FLOAT ( 10, 15 ); + return 1; + } + return 0; +} + +int CBasePlayerWeapon :: Launch ( const char *ammo, int type ) +{ + if(!UseAmmo(ammo)) return 0; + float fps = 0;//custom animation speed for handgrenade + + //viewmodel animation + ZoomReset(); + + if ( !stricmp( ammo, "m203" )) + { + // we don't add in player velocity anymore. + UTIL_MakeVectors( m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle ); + CGrenade::ShootContact( m_pPlayer->pev, m_pPlayer->pev->origin + m_pPlayer->pev->view_ofs + gpGlobals->v_forward * 16, gpGlobals->v_forward * 800 ); + } + else if( !stricmp( ammo, "rockets" )) + { + UTIL_MakeVectors( m_pPlayer->pev->v_angle ); + Vector vecSrc = m_pPlayer->GetGunPosition( ) + gpGlobals->v_forward * 16 + gpGlobals->v_right * 8 + gpGlobals->v_up * -8; + + CRpgRocket *pRocket = CRpgRocket::Create ( vecSrc, m_pPlayer->pev->v_angle, m_pPlayer, this ); + UTIL_MakeVectors( m_pPlayer->pev->v_angle );// RpgRocket::Create stomps on globals, so remake. + pRocket->pev->velocity = pRocket->pev->velocity + gpGlobals->v_forward * DotProduct( m_pPlayer->pev->velocity, gpGlobals->v_forward ); + } + else if( !stricmp( ammo, "bolts" )) + { + // we don't add in player velocity anymore. + Vector anglesAim = m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle; + UTIL_MakeVectors( anglesAim ); + + anglesAim.x = -anglesAim.x; + Vector vecSrc = m_pPlayer->GetGunPosition( ) - gpGlobals->v_up * 2; + Vector vecDir = gpGlobals->v_forward; + + CBolt *pBolt = CBolt::Create( vecSrc, anglesAim, m_pPlayer ); + if (m_pPlayer->pev->waterlevel == 3) pBolt->pev->velocity = vecDir * 1000; + else pBolt->pev->velocity = vecDir * 2000; + } + else if( !stricmp( ammo, "nuke" )) + { + UTIL_MakeVectors( m_pPlayer->pev->v_angle ); + Vector vecSrc = m_pPlayer->GetGunPosition( ) + gpGlobals->v_forward * 125 + gpGlobals->v_up * 2; + CWHRocket *pRocket = CWHRocket::Create ( vecSrc, m_pPlayer->pev->v_angle, m_pPlayer, this, type ); + } + else if( !stricmp( ammo, "grenade" )) + { + Vector angThrow = m_pPlayer->pev->v_angle + m_pPlayer->pev->punchangle; + + if ( angThrow.x < 0 ) angThrow.x = -10 + angThrow.x * ( ( 90 - 10 ) / 90.0 ); + else angThrow.x = -10 + angThrow.x * ( ( 90 + 10 ) / 90.0 ); + + float flVel = ( 90 - angThrow.x ) * 4; + if ( flVel > 500 ) flVel = 500; + + UTIL_MakeVectors( angThrow ); + Vector vecSrc = m_pPlayer->pev->origin + m_pPlayer->pev->view_ofs + gpGlobals->v_forward * 16; + Vector vecThrow = gpGlobals->v_forward * flVel + m_pPlayer->pev->velocity; + + // alway explode 3 seconds after the pin was pulled + float time = m_flHoldTime - UTIL_WeaponTimeBase() + 3.0; + if (time < 0) time = 0; + + CGrenade::ShootTimed( m_pPlayer->pev, vecSrc, vecThrow, time ); + fps = flVel * 0.08; + if(fps < 12) fps = 12; + + m_flHoldTime = 0; + m_iOnControl = 4; + } + + m_pPlayer->m_iWeaponVolume = LOUD_GUN_VOLUME; + m_pPlayer->m_iWeaponFlash = BRIGHT_GUN_FLASH; + + m_pPlayer->m_iExtraSoundTypes = bits_SOUND_DANGER; + m_pPlayer->m_flStopExtraSoundTime = UTIL_WeaponTimeBase() + 0.2; + + // player "shoot" animation + m_pPlayer->SetAnimation( PLAYER_ATTACK1 ); + + //play random shooting animation + if(!PlayEmptyFire()) PlayMeleeAttack( fps ); + + return 1; +} + +int CBasePlayerWeapon::Swing( int fFirst ) +{ + int fDidHit = FALSE; + BOOL bHit = FALSE; + TraceResult tr; + TraceResult m_trHit; + + if ( m_flTimeUpdate > UTIL_WeaponTimeBase() ) return fDidHit; + + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 3; + + UTIL_MakeVectors (m_pPlayer->pev->v_angle); + Vector vecSrc = m_pPlayer->GetGunPosition( ); + Vector vecEnd = vecSrc + gpGlobals->v_forward * 32; + + UTIL_TraceLine( vecSrc, vecEnd, dont_ignore_monsters, ENT( m_pPlayer->pev ), &tr ); + + if ( tr.flFraction >= 1.0 ) + { + UTIL_TraceHull( vecSrc, vecEnd, dont_ignore_monsters, head_hull, ENT( m_pPlayer->pev ), &tr ); + if ( tr.flFraction < 1.0 ) + { + // Calculate the point of intersection of the line (or hull) and the object we hit + // This is and approximation of the "best" intersection + CBaseEntity *pHit = CBaseEntity::Instance( tr.pHit ); + if ( !pHit || pHit->IsBSPModel() ) + UTIL_FindHullIntersection( vecSrc, tr, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, m_pPlayer->edict() ); + vecEnd = tr.vecEndPos; // This is the point on the actual surface (the hull could have hit space) + } + } + + if ( tr.flFraction >= 1.0) + { + if(fFirst) + { + switch( (g_iSwing++) % 3 ) + { + case 0: SetAnimation ( ACT_VM_MELEE_ATTACK1 ); break; + case 1: SetAnimation ( ACT_VM_MELEE_ATTACK2 ); break; + case 2: SetAnimation ( ACT_VM_MELEE_ATTACK3 ); break; + } + + if(EmptySnd()) EMIT_SOUND_DYN(ENT(m_pPlayer->pev), CHAN_WEAPON, STRING(EmptySnd()), 1, ATTN_NORM, 0, 94 + RANDOM_LONG(0,0xF)); + m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.5; + m_pPlayer->SetAnimation( PLAYER_ATTACK1 ); + } + } + else + { + // hit + fDidHit = TRUE; + CBaseEntity *pEntity = CBaseEntity::Instance(tr.pHit); + + switch( (g_iSwing++) % 3 ) + { + case 0: SetAnimation ( ACT_VM_RANGE_ATTACK1 ); break; + case 1: SetAnimation ( ACT_VM_RANGE_ATTACK2 ); break; + case 2: SetAnimation ( ACT_VM_RANGE_ATTACK3 ); break; + } + + // player "shoot" animation + m_pPlayer->SetAnimation( PLAYER_ATTACK1 ); + + ClearMultiDamage( ); + pEntity->TraceAttack(m_pPlayer->pev, CROWBAR_DMG, gpGlobals->v_forward, &tr, DMG_CLUB ); + ApplyMultiDamage( m_pPlayer->pev, m_pPlayer->pev ); + + m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.25; + + // play thwack, smack, or dong sound + float flVol = 1.0; + int fHitWorld = TRUE; + + if (pEntity) + { + if ( pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE ) + { + if(sndcnt())//HitBody sound + { + int firesound = RANDOM_LONG(0, sndcnt() - 1); + EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, STRING(FireSound(firesound)), 1, ATTN_NORM); + } + m_pPlayer->m_iWeaponVolume = CROWBAR_BODYHIT_VOLUME; + if ( !pEntity->IsAlive() ) + { + m_flNextPrimaryAttack = m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.5; + return TRUE; + } + else flVol = 0.1; + fHitWorld = FALSE; + } + } + + if (fHitWorld) + { + float fvolbar = TEXTURETYPE_PlaySound(&tr, vecSrc, vecSrc + (vecEnd-vecSrc)*2, BULLET_CROWBAR); + + if ( IsMultiplayer() ) fvolbar = 1; + + // also play crowbar strike + if(sndcnt())//CBAR Hit sound + { + int sfxsound = RANDOM_LONG(0, sfxcnt() - 1); + EMIT_SOUND_DYN(ENT(m_pPlayer->pev), CHAN_ITEM, STRING(SfxSound(sfxsound)), fvolbar, ATTN_NORM, 0, 98 + RANDOM_LONG(0,3)); + } + } + + // delay the decal a bit + m_trHit = tr; + + m_pPlayer->m_iWeaponVolume = flVol * CROWBAR_WALLHIT_VOLUME; + + DecalGunshot( &m_trHit, BULLET_CROWBAR ); + m_flTimeUpdate = UTIL_WeaponTimeBase() + 0.2; + } + return fDidHit; +} + +//========================================================= +// Zoom In\Out +//========================================================= +void CBasePlayerWeapon::ZoomUpdate( void ) +{ + if((iAttack1() == ZOOM && m_pPlayer->pev->button & IN_ATTACK) || (iAttack2() == ZOOM && m_pPlayer->pev->button & IN_ATTACK2)) + { + if(m_iZoom == 0) + { + if (m_flHoldTime > UTIL_WeaponTimeBase()) return; + m_iZoom = 1; + EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/zoom.wav", 1, ATTN_NORM); + m_flTimeUpdate = UTIL_WeaponTimeBase() + 0.8; + } + if(m_iZoom == 1) + { + m_pPlayer->m_iFOV = 50; + m_pPlayer->pev->viewmodel = NULL; + m_iZoom = 2;//ready to zooming, wait for 0.8 secs + } + if (m_iZoom == 2 && m_pPlayer->m_iFOV > MAX_ZOOM) + { + if (m_flTimeUpdate < UTIL_WeaponTimeBase()) + { + m_pPlayer->m_iFOV--; + m_flTimeUpdate = UTIL_WeaponTimeBase() + 0.02; + } + } + if(m_iZoom == 3) ZoomReset(); + } + else if(m_iZoom > 1) m_iZoom = 3; + + MESSAGE_BEGIN( MSG_ONE, gmsgZoomHUD, NULL, m_pPlayer->pev ); + WRITE_BYTE( m_iZoom ); + MESSAGE_END(); +} + +void CBasePlayerWeapon::ZoomReset( void ) +{ + //return viewmodel + if(m_iZoom) + { + m_pPlayer->pev->viewmodel = iViewModel(); + m_flHoldTime = UTIL_WeaponTimeBase() + 0.5; + m_pPlayer->m_iFOV = 90; + m_iZoom = 0;//clear zoom + MESSAGE_BEGIN( MSG_ONE, gmsgZoomHUD, NULL, m_pPlayer->pev ); + WRITE_BYTE( m_iZoom ); + MESSAGE_END(); + m_pPlayer->UpdateClientData();//update client data manually + } +} + +//========================================================= +// Virtual base functions +//========================================================= +void CBasePlayerWeapon :: Deploy( void ) +{ + m_iStepReload = 0; + if(iMaxClip() && m_iClip <= 0) //weapon have clip and clip is empty ? + { + if(!DefaultDeploy(ACT_VM_DEPLOY_EMPTY)) //try to playing "deploy_empty" anim + DefaultDeploy(ACT_VM_DEPLOY); //custom animation not found, play standard animation + } + else DefaultDeploy(ACT_VM_DEPLOY); //just playing standard anim +} + +void CBasePlayerWeapon :: Holster( void ) +{ + if(iMaxClip() && m_iClip <= 0) //weapon have clip and clip is empty ? + { + if(!DefaultHolster(ACT_VM_HOLSTER_EMPTY))//try to playing "holster_empty" anim + DefaultHolster(ACT_VM_HOLSTER); + } + else DefaultHolster(ACT_VM_HOLSTER); //just playing standard anim +} + +//========================================================= +// NOT USED:HoldDown weapon +//========================================================= +int CBasePlayerWeapon::FoundAlly( void ) +{ + CBaseEntity *pEntity = UTIL_FindEntityForward( m_pPlayer ); + if ( pEntity ) + { + CBaseMonster *pMonster = pEntity->MyMonsterPointer(); + if ( pMonster ) + { + int m_class = pMonster->Classify(); + if( m_class == CLASS_PLAYER_ALLY || m_class == CLASS_HUMAN_PASSIVE) + { + return 1; + } + } + } + return 0; +} + +//========================================================= +// Weapon Frame - main function +//========================================================= +void CBasePlayerWeapon::ItemPreFrame( void ) +{ + +} + +void CBasePlayerWeapon::ItemPostFrame( void ) +{ + if(!b_restored) + { + m_iClientSkin = -1;//reset last skin info for new weapon + m_iClientBody = -1;//reset last skin info for new weapon + if(iMaxClip() && !m_iClip) SetAnimation(ACT_VM_IDLE_EMPTY); //restore last animation + else SetAnimation( ACT_VM_IDLE1 ); + b_restored = TRUE; + } + if(!m_iSpot && m_pSpot)//disable laser dot + { + m_iSkin = 0; //disable screen + EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/spot_off.wav", 1, ATTN_NORM); + m_pSpot->Killed(); + m_pSpot = NULL; + } + if(m_iSpot && !m_pSpot)//enable laser dot + { + m_pSpot = CLaserSpot::CreateSpot(); + EMIT_SOUND(ENT(m_pPlayer->pev), CHAN_ITEM, "weapons/spot_on.wav", 1, ATTN_NORM); + m_iSkin = 1; //enable screen + } + if(m_pSpot) m_pSpot->Update( m_pPlayer ); + + ZoomUpdate();//update zoom + + if ( !m_iStepReload ) m_fInReload = FALSE;//reload done + if ( m_flTimeUpdate < UTIL_WeaponTimeBase() ) PostIdle(); //catch all + if (m_fInReload && m_pPlayer->m_flNextAttack <= UTIL_WeaponTimeBase()) + { + if(m_iStepReload > 2) + { + // complete the reload. + int j = min( iMaxClip() - m_iClip, m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]); + + // Add them to the clip + m_iClip += j; + m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType] -= j; + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + 0.01;//play PostReload + //m_iStepReload = 1; + m_fInReload = FALSE; + } + } + + if ((m_pPlayer->pev->button & IN_ATTACK2) && CanAttack( m_flNextSecondaryAttack ) && !m_iOnControl) + { + SecondaryAttack(); + m_pPlayer->pev->button &= ~IN_ATTACK2; + } + else if ((m_pPlayer->pev->button & IN_ATTACK) && CanAttack( m_flNextPrimaryAttack )) + { + if(m_iOnControl == 1)//destroy controllable rocket + { + m_iOnControl = 2;//destroy nuke + m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 1.5; + return; + } + PrimaryAttack(); + m_pPlayer->pev->button &= ~IN_ATTACK; + } + else if ( m_pPlayer->pev->button & IN_RELOAD && iMaxClip() != WEAPON_NOCLIP && !m_fInReload ) + { + // reload when reload is pressed, or if no buttons are down and weapon is empty. + Reload(); + } + else if ( !(m_pPlayer->pev->button & (IN_ATTACK|IN_ATTACK2) ) ) + { + //play sequence holster/deploy if player find or lose suit + if( ((SUIT) && !PLAYER_HAS_SUIT) || (!(SUIT) && PLAYER_HAS_SUIT)) + { + m_pPlayer->m_pActiveItem->Holster( ); + m_pPlayer->QueueItem(this); + if (m_pPlayer->m_pActiveItem) m_pPlayer->m_pActiveItem->Deploy(); + } + if ( !CanDeploy() && m_flNextPrimaryAttack < gpGlobals->time ) + { + // weapon isn't useable, switch. + if ( !(iFlags() & ITEM_FLAG_NOAUTOSWITCHEMPTY) && g_pGameRules->GetNextBestWeapon( m_pPlayer, this ) ) + { + m_flNextPrimaryAttack = gpGlobals->time + 0.3; + return; + } + } + else + { + // weapon is useable. Reload if empty and weapon has waited as long as it has to after firing + if ( m_iClip == 0 && !(iFlags() & ITEM_FLAG_NOAUTORELOAD) && m_flNextPrimaryAttack < gpGlobals->time ) + { + Reload(); + return; + } + } + m_iPlayEmptySound = 1;//reset empty sound + if(iFlags() & ITEM_FLAG_USEAUTOAIM) m_pPlayer->GetAutoaimVector( AUTOAIM_5DEGREES ); + + if(m_flTimeWeaponIdle < UTIL_WeaponTimeBase())//step reload + { + if (m_iStepReload > 0 ) + { + if (m_iClip != iMaxClip() && m_pPlayer->m_rgAmmo[m_iPrimaryAmmoType]) Reload(); + else + { + // reload debounce has timed out + if(IsEmptyReload()) + { + if(SetAnimation( ACT_VM_PUMP_EMPTY ) == -1) + SetAnimation( ACT_VM_PUMP ); + } + else SetAnimation( ACT_VM_PUMP ); + if ( iFlags() & ITEM_FLAG_HIDEAMMO ) m_iBody = 0;//reset ammo + PostReload();//post effects + m_iStepReload = 0; + } + } + else if ( m_iOnControl > 3 )//item deploy + { + m_iOnControl = 0; + if ( m_pPlayer->m_rgAmmo[ m_iPrimaryAmmoType ] ) + { + SetAnimation( ACT_VM_DEPLOY ); + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + RANDOM_LONG( 10, 15 ); + } + else Holster(); + } + else WeaponIdle(); + } + } +} + +int CBasePlayerWeapon::UpdateClientData( CBasePlayer *pPlayer ) +{ + BOOL bSend = FALSE; + int state = 0; + + //update weapon body + if( m_iClientBody != m_iBody) + { + pev->body = (pev->body % NUM_HANDS) + NUM_HANDS * m_iBody; //calculate body + MESSAGE_BEGIN( MSG_ONE, gmsgSetBody, NULL, m_pPlayer->pev ); + WRITE_BYTE( pev->body ); //weaponmodel body + MESSAGE_END(); + m_iClientBody = m_iBody;//synched + } + + //update weapon skin + if( m_iClientSkin != m_iSkin) + { + pev->skin = m_iSkin; //calculate skin + MESSAGE_BEGIN( MSG_ONE, gmsgSetSkin, NULL, m_pPlayer->pev ); + WRITE_BYTE( pev->skin ); //weaponmodel skin. + MESSAGE_END(); + m_iClientSkin = m_iSkin;//synched + } + + if ( pPlayer->m_pActiveItem == this ) + { + if ( pPlayer->m_fOnTarget ) state = 0x40;//weapon is ontarget + else state = 1; + } + + // Forcing send of all data! + if ( !pPlayer->m_fWeapon ) bSend = TRUE; + + // This is the current or last weapon, so the state will need to be updated + if ( this == pPlayer->m_pActiveItem || this == pPlayer->m_pClientActiveItem ) + { + if ( pPlayer->m_pActiveItem != pPlayer->m_pClientActiveItem ) bSend = TRUE; + } + + // If the ammo, state, or fov has changed, update the weapon + if ( m_iClip != m_iClientClip || state != m_iClientWeaponState || pPlayer->m_iFOV != pPlayer->m_iClientFOV ) + { + bSend = TRUE; + } + + if ( bSend ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgCurWeapon, NULL, pPlayer->pev ); + WRITE_BYTE( state ); + WRITE_BYTE( m_iId ); + WRITE_BYTE( m_iClip ); + MESSAGE_END(); + + m_iClientClip = m_iClip; + m_iClientWeaponState = state; + pPlayer->m_fWeapon = TRUE; + } + + if ( m_pNext ) m_pNext->UpdateClientData( pPlayer ); + return 1; +} + +void CBasePlayerWeapon :: SetNextThink( float delay ) +{ + m_fNextThink = UTIL_WeaponTimeBase() + delay; + pev->nextthink = m_fNextThink; +} + +//========================================================= +// Attach\drop operations +//========================================================= +void CBasePlayerWeapon::DestroyItem( void ) +{ + // if attached to a player, remove. + if ( m_pPlayer ) m_pPlayer->RemovePlayerItem( this ); + Drop(); +} + +void CBasePlayerWeapon::Drop( void ) +{ + SetTouch( NULL ); + SetThink(Remove); + SetNextThink( 0.1 ); +} + +void CBasePlayerWeapon::AttachToPlayer ( CBasePlayer *pPlayer ) +{ + pev->movetype = MOVETYPE_FOLLOW; + pev->solid = SOLID_NOT; + pev->aiment = pPlayer->edict(); + pev->effects = EF_NODRAW; + pev->modelindex = 0;// server won't send down to clients if modelindex == 0 + pev->targetname = iStringNull; //don't try remove this weapon from map + pev->model = iStringNull; + pev->owner = pPlayer->edict(); + SetNextThink( 0.1 ); + SetTouch( NULL ); + SetThink(NULL); +} + +int CBasePlayerWeapon::AddDuplicate( CBasePlayerWeapon *pOriginal ) +{ + if(iFlags() & ITEM_FLAG_NODUPLICATE) return FALSE; + + if ( m_iDefaultAmmo || m_iDefaultAmmo2 ) + return ExtractAmmo( (CBasePlayerWeapon *)pOriginal ); + return ExtractClipAmmo( (CBasePlayerWeapon *)pOriginal ); +} + +int CBasePlayerWeapon::AddToPlayer( CBasePlayer *pPlayer ) +{ + m_pPlayer = pPlayer;//Give global pointer to player + pPlayer->pev->weapons |= (1<GetAmmoIndex( pszAmmo1() ); + m_iSecondaryAmmoType = pPlayer->GetAmmoIndex( pszAmmo2() ); + } + + MESSAGE_BEGIN(MSG_ONE, gmsgWeapPickup, NULL, pPlayer->pev); + WRITE_BYTE(m_iId); + MESSAGE_END(); + + return AddWeapon(); +} + +//========================================================= +// Ammo base operations +//========================================================= +BOOL CBasePlayerWeapon :: AddPrimaryAmmo( int iCount, char *szName, int iMaxClip, int iMaxCarry ) +{ + int iIdAmmo; + if (iMaxClip < 1) + { + m_iClip = -1; + iIdAmmo = m_pPlayer->GiveAmmo( iCount, szName, iMaxCarry ); + } + else if (m_iClip == 0 ) //add ammo in clip only for new weapons + { + int i; + i = min( m_iClip + iCount, iMaxClip ) - m_iClip; + m_iClip += i; + iIdAmmo = m_pPlayer->GiveAmmo( iCount - i, szName, iMaxCarry ); + } + else iIdAmmo = m_pPlayer->GiveAmmo( iCount, szName, iMaxCarry ); + + if (iIdAmmo > 0) + { + m_iPrimaryAmmoType = iIdAmmo; + if (m_pPlayer->HasPlayerItem( this )) EMIT_SOUND(ENT(pev), CHAN_ITEM, "weapons/glock/clip_in.wav", 1, ATTN_NORM); + } + return iIdAmmo > 0 ? TRUE : FALSE; +} + +BOOL CBasePlayerWeapon :: AddSecondaryAmmo( int iCount, char *szName, int iMax ) +{ + int iIdAmmo; + + iIdAmmo = m_pPlayer->GiveAmmo( iCount, szName, iMax ); + + if (iIdAmmo > 0) + { + m_iSecondaryAmmoType = iIdAmmo; + EMIT_SOUND(ENT(pev), CHAN_ITEM, "weapons/glock/clip_in.wav", 1, ATTN_NORM); + } + return iIdAmmo > 0 ? TRUE : FALSE; +} + +int CBasePlayerWeapon::ExtractAmmo( CBasePlayerWeapon *pWeapon ) +{ + int iReturn; + if ( pszAmmo1() != NULL ) + { + iReturn = pWeapon->AddPrimaryAmmo( m_iDefaultAmmo, (char *)pszAmmo1(), iMaxClip(), iMaxAmmo1() ); + m_iDefaultAmmo = 0; + } + + if ( pszAmmo2() != NULL ) + { + iReturn = pWeapon->AddSecondaryAmmo( m_iDefaultAmmo2, (char *)pszAmmo2(), iMaxAmmo2() ); + } + return iReturn; +} + +int CBasePlayerWeapon::ExtractClipAmmo( CBasePlayerWeapon *pWeapon ) +{ + int iAmmo; + + if ( m_iClip == WEAPON_NOCLIP ) iAmmo = 0;// guns with no clips always come empty if they are second-hand + else iAmmo = m_iClip; + return pWeapon->m_pPlayer->GiveAmmo( iAmmo, (char *)pszAmmo1(), iMaxAmmo1() ); +} + +void CBasePlayerWeapon :: SetObjectCollisionBox( void ) +{ + pev->absmin = pev->origin + Vector(-24, -24, 0); + pev->absmax = pev->origin + Vector(24, 24, 16); +} \ No newline at end of file diff --git a/server/ents/baseweapon.h b/server/ents/baseweapon.h new file mode 100644 index 00000000..a2531ab0 --- /dev/null +++ b/server/ents/baseweapon.h @@ -0,0 +1,362 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#ifndef WEAPONS_H +#define WEAPONS_H + +#include "basebeams.h" +#include "player.h" +#include "bullets.h" +#include "sfx.h" +#include "baserockets.h" +#include "damage.h" +#include "defaults.h" + +class CLaserSpot; + +//weapon flags +#define ITEM_FLAG_SELECTONEMPTY 1 //this weapon can choose without ammo +#define ITEM_FLAG_NOAUTORELOAD 2 //only manual reload +#define ITEM_FLAG_NOAUTOSWITCHEMPTY 4 //don't switch from this weapon +#define ITEM_FLAG_LIMITINWORLD 8 //limit in world +#define ITEM_FLAG_EXHAUSTIBLE 16 // A player can totally exhaust their ammo supply and lose this weapon +#define ITEM_FLAG_NODUPLICATE 32 // player can't give this weapon again +#define ITEM_FLAG_USEAUTOAIM 64 // weapon uses autoaim +#define ITEM_FLAG_HIDEAMMO 128 // weapon uses autoaim + +#define SUIT m_pPlayer->m_iHideHUD & ITEM_SUIT +#define PLAYER_HAS_SUIT pev->body == 1 +#define NUM_HANDS 2 //number of hands: barney and gordon +#define MAX_SHOOTSOUNDS 3 //max of four random shoot sounds + +enum { + NONE = 0, + AMMO1, //fire primary ammo + AMMO2, //fire seondary ammo + LASER_DOT, //enable laser dot + ZOOM, //enable zoom + FLASHLIGHT, //enable flashlight + SWITCHMODE, //play two beetwen anims and change body + SWING, //crowbar swing +}; + +#define BATTACK_FIREMODE0 0 //both attack firemode 0 +#define PATTACK_FIREMODE1 1 //primary attack firemode 1 +#define SATTACK_FIREMODE1 2 //secondary attack firemode 2 + +#define EMPTY_RELOAD 3 +#define NORMAL_RELOAD 4 + +typedef struct +{ + int iSlot; // hud slot + int iPosition; // hud position + int iViewModel; // path to viewmodel + int iWorldModel; // path to worldmodel + char szAnimExt[16]; // player anim postfix + const char *pszAmmo1; // ammo 1 type + int iMaxAmmo1; // max ammo 1 + const char *pszAmmo2; // ammo 2 type + int iMaxAmmo2; // max ammo 2 + const char *pszName; // weapon name + int iMaxClip; // clip size + int iId; // unikal weapon Id number + int iFlags; // weapon flags + int iWeight; // for autoselect weapon + int attack1; // attack1 type + int attack2; // attack2 type + float fNextAttack; // nextattack + float fNextAttack2; // next secondary attack + int firesound[MAX_SHOOTSOUNDS]; // firesounds + int sfxsound[MAX_SHOOTSOUNDS]; // sfxsound + int sndcount; // fire sound count + int sfxcount; // sfx sound count + int emptysnd; // empty sound + RandomRange punchangle1; // punchangle 1 attack + RandomRange punchangle2; // punchangle 2 attack + RandomRange recoil1; // recoil 1 attack + RandomRange recoil2; // recoil 2 attack +} ItemInfo; + +typedef struct +{ + const char *pszName; + int iMaxCarry; + int iId; +} AmmoInfo; + +class CBasePlayerWeapon : public CBaseAnimating +{ + DECLARE_CLASS( CBasePlayerWeapon, CBaseAnimating ); +public: + virtual void SetObjectCollisionBox( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() | FCAP_ACROSS_TRANSITION; } + virtual int AddToPlayer( CBasePlayer *pPlayer ); // return TRUE if the item you want the item added to the player inventory + virtual int AddDuplicate( CBasePlayerWeapon *pItem ); // return TRUE if you want your duplicate removed from world + void EXPORT DestroyItem( void ); + void EXPORT DefaultTouch( CBaseEntity *pOther ); // default weapon touch + void EXPORT FallThink ( void ); // when an item is first spawned, this think is run to determine when the object has hit the ground. + void EXPORT Materialize( void ); // make a weapon visible and tangible + void EXPORT AttemptToMaterialize( void ); // the weapon desires to become visible and tangible, if the game rules allow for it + CBaseEntity* Respawn ( void );// copy a weapon + void CheckRespawn( void ); + virtual int GetItemInfo(ItemInfo *p); // get weapon default info + virtual BOOL CanDeploy( void ); + virtual BOOL CanHolster( void ) { return ( m_iOnControl == 0 ); }; // can this weapon be put away right now? + virtual void SetNextThink( float delay ); + virtual void Precache( void ); + void EXPORT ItemTouch( CBaseEntity *pOther ); + virtual void Drop( void ); + virtual void AttachToPlayer ( CBasePlayer *pPlayer ); + virtual void Spawn(void); + virtual int UpdateClientData( CBasePlayer *pPlayer ); // sends hud info to client dll, if things have changed + virtual CBasePlayerWeapon *GetWeaponPtr( void ) { return (CBasePlayerWeapon *)this; }; + int FoundAlly( void ); + inline BOOL CanAttack( float attack_time ) { return ( attack_time <= gpGlobals->time ) ? TRUE : FALSE; } + + //ammo operations + virtual int ExtractAmmo( CBasePlayerWeapon *pWeapon ); // Return TRUE if you can add ammo to yourself when picked up + virtual int ExtractClipAmmo( CBasePlayerWeapon *pWeapon ); // Return TRUE if you can add ammo to yourself when picked up + virtual int AddWeapon( void ) { ExtractAmmo( this ); return TRUE; }; // Return TRUE if you want to add yourself to the player + BOOL AddPrimaryAmmo( int iCount, char *szName, int iMaxClip, int iMaxCarry ); + BOOL AddSecondaryAmmo( int iCount, char *szName, int iMaxCarry ); + int GetAmmoType( const char *ammo );//determine ammo type primary or secondary + int ReturnAmmoIndex( const char *ammo );//return primary or secondary ammo index + int UseAmmo( const char *ammo, int count = 1 ); //Determine and use ammo type + + //parse weapon script files + int ParseWeaponData( ItemInfo *II, char *file ); //parse weapon_*.txt + int ParseWeaponFile( ItemInfo *II, const char *pfile ); //parse WeaponData {} + int ParsePrimaryAttack( ItemInfo *II, char *pfile ); //parse PrimaryAttack {} + int ParseSecondaryAttack( ItemInfo *II, char *pfile ); //parse SeconadryAttack {} + int ParseSoundData( ItemInfo *II, char *pfile ); //parse SoundData {} + //HudData parses on client side + void ResetParse( ItemInfo *II ); //reset ItemInfo + void GenerateID( void );//generate unicum ID number for each weapon + + + //Play Animation methods + int SetAnimation( Activity activity, float fps = 0 ); + int SetAnimation( char *name, float fps = 0 ); + int PlaySequence( Activity activity, float fps = 0 ); + void SetPlayerEffects( const char *ammo, int firemode ); + void PlayAttackSound( int firemode ); + void SendWeaponAnim( int sequence, float fps = 0 ); + float SetNextAttack( float delay ) { return m_pPlayer->m_flNextAttack = gpGlobals->time + delay; } + float SetNextIdle( float delay ) { return m_flTimeWeaponIdle = gpGlobals->time + delay; } + float SequenceDuration( void ) { return CBaseAnimating :: SequenceDuration( m_pPlayer->pev->weaponanim ); } + int GetNumBodies( void ) + { + dstudiohdr_t *pstudiohdr = (dstudiohdr_t *)(GET_MODEL_PTR( ENT(pev) )); + if (pstudiohdr) + { + dstudiobodyparts_t *pbodypart = (dstudiobodyparts_t *)((byte *)pstudiohdr + pstudiohdr->bodypartindex) + 2; + return pbodypart->nummodels; + } + return 0; + } + + //main frame + virtual void ItemPreFrame( void ); // called each frame by the player PreThink + virtual void ItemPostFrame( void ); // called each frame by the player PostThink + + //global weapon info struct (refresh on save\load) + static ItemInfo ItemInfoArray[ MAX_WEAPONS ]; + static AmmoInfo AmmoInfoArray[ MAX_AMMO_SLOTS ]; + + CBasePlayer *m_pPlayer; + CBasePlayerWeapon *m_pNext; + int m_iId; //Weapon unical Id (0 - MAX_WEAPONS) // WEAPON_??? + + //don't save this + CLaserSpot *m_pSpot; //LTD spot + + //virtual methods for ItemInfo acess + int iItemPosition(void) { return ItemInfoArray[ m_iId ].iPosition; } + int iItemSlot( void ) { return ItemInfoArray[ m_iId ].iSlot + 1; } + int iViewModel( void ) { return ItemInfoArray[ m_iId ].iViewModel; } + int iWorldModel( void ) { return ItemInfoArray[ m_iId ].iWorldModel; } + int iMaxAmmo1( void ) { return ItemInfoArray[ m_iId ].iMaxAmmo1; } + int iMaxAmmo2( void ) { return ItemInfoArray[ m_iId ].iMaxAmmo2; } + int iMaxClip( void ) { return ItemInfoArray[ m_iId ].iMaxClip; } + int iWeight( void ) { return ItemInfoArray[ m_iId ].iWeight; } + int iFlags( void ) { return ItemInfoArray[ m_iId ].iFlags; } + int iAttack1( void ) { return ItemInfoArray[ m_iId ].attack1; } + int FireSound( int i ) { return ItemInfoArray[ m_iId ].firesound[i]; } + int SfxSound( int i ) { return ItemInfoArray[ m_iId ].sfxsound[i]; } + int EmptySnd( void ) { return ItemInfoArray[ m_iId ].emptysnd; } + int sndcnt( void ) { return ItemInfoArray[ m_iId ].sndcount; } + int sfxcnt( void ) { return ItemInfoArray[ m_iId ].sfxcount; } + int iAttack2( void ) { return ItemInfoArray[ m_iId ].attack2; } + char *szAnimExt( void ) { return ItemInfoArray[ m_iId ].szAnimExt; } + float fNextAttack1( void ){ return ItemInfoArray[ m_iId ].fNextAttack; } + float fNextAttack2( void ){ return ItemInfoArray[ m_iId ].fNextAttack2; } + float fPunchAngle1( void ){ return ItemInfoArray[ m_iId ].punchangle1.Random(); } + float fPunchAngle2( void ){ return ItemInfoArray[ m_iId ].punchangle2.Random(); } + float fRecoil1( void ) { return ItemInfoArray[ m_iId ].recoil1.Random(); } + float fRecoil2( void ) { return ItemInfoArray[ m_iId ].recoil2.Random(); } + const char *pszAmmo1( void ) { return ItemInfoArray[ m_iId ].pszAmmo1; } + const char *pszName( void ) { return ItemInfoArray[ m_iId ].pszName; } + const char *pszAmmo2( void ) { return ItemInfoArray[ m_iId ].pszAmmo2; } + + BOOL PlayEmptySound( void );//universal empty sound + + //Default functions + BOOL DefaultDeploy( Activity sequence ); + BOOL DefaultHolster( Activity sequence ); + BOOL DefaultReload( Activity sequence ); + void DefaultIdle( void ); + int Shoot ( const char *ammo, Vector vecSpread, int firemode = 0, int cShots = 1 ); //bullet shoot + int Launch ( const char *ammo, int type = 0 ); //rocket launch + int Swing( int fFirst ); //crowbar swing + + void ZoomUpdate( void ); + void ZoomReset( void ); + + // virtaul weapon functions + int PlayCurrentAttack( int action, int firemode ); + int GetCurrentAttack( const char *ammo, int firemode ); + Vector GetCurrentSpread( const char *ammo ); + int GetBulletType( const char *ammo ); + + inline int PlayRangeAttack( float fps = 0 ) + { + //play random "range" animation + float flRand = RANDOM_FLOAT(0.0, 1.0); + if( flRand < 0.5 && SetAnimation( ACT_VM_RANGE_ATTACK1, fps ) == -1) return 0; + if( flRand > 0.8 && SetAnimation( ACT_VM_RANGE_ATTACK2, fps ) == -1) + { + if(SetAnimation( ACT_VM_RANGE_ATTACK1, fps ) == -1) return 0; + } + if( flRand < 0.8 && flRand > 0.5 && SetAnimation( ACT_VM_RANGE_ATTACK3, fps ) == -1) + { + if(SetAnimation( ACT_VM_RANGE_ATTACK1, fps ) == -1) return 0; + } + return 1; + } + + inline int PlayMeleeAttack( float fps = 0 ) + { + //play random "range" animation + float flRand = RANDOM_FLOAT(0.0, 1.0); + if( flRand < 0.5 && SetAnimation( ACT_VM_MELEE_ATTACK1, fps ) == -1) return 0; + if( flRand > 0.8 && SetAnimation( ACT_VM_MELEE_ATTACK2, fps ) == -1) + { + if(SetAnimation( ACT_VM_MELEE_ATTACK1, fps ) == -1) return 0; + } + if( flRand < 0.8 && flRand > 0.5 && SetAnimation( ACT_VM_MELEE_ATTACK3, fps ) == -1) + { + if(SetAnimation( ACT_VM_MELEE_ATTACK1, fps ) == -1) return 0; + } + return 1; + } + + inline int PlayEmptyFire( float fps = 0 ) + { + if(iMaxClip() && !m_iClip)//last round + { + return SetAnimation( ACT_VM_SHOOT_EMPTY, fps ) == -1 ? FALSE : TRUE; + } + return 0; + } + + //GENERIC WEAPON FUNCTIONS + virtual void PrimaryPostAttack( void ) {} + virtual void SecondaryPostAttack( void ) {} + virtual void PostReload( void ) {} + inline int IsEmptyReload( void ) { return m_iStepReload == EMPTY_RELOAD ? TRUE : FALSE; } + + virtual void PrimaryAttack( void ) // do "+ATTACK" + { + int iResult = PlayCurrentAttack( iAttack1(), FBitSet(pev->impulse, PATTACK_FIREMODE1) ? 1 : 0 ); + if(iResult == 1) + { + m_pPlayer->pev->punchangle.x = fPunchAngle1(); + m_pPlayer->pev->velocity = m_pPlayer->pev->velocity - gpGlobals->v_forward * fRecoil1(); + if ( (iFlags() & ITEM_FLAG_HIDEAMMO ) && m_iClip < GetNumBodies() && m_iClip ) m_iBody++; + + PrimaryPostAttack(); //run post effects + if ( m_flNextPrimaryAttack < UTIL_WeaponTimeBase() ) + m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + fNextAttack1() + 0.02; + m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + fNextAttack1(); + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + RANDOM_LONG(10, 15); + } + else if(iResult == 0) + { + PlayEmptySound( ); + m_flNextPrimaryAttack = UTIL_WeaponTimeBase() + 0.5; + } + m_iStepReload = 0;//reset reload + } + virtual void SecondaryAttack( void ) // do "+ATTACK2" + { + int iResult = PlayCurrentAttack( iAttack2(), FBitSet(pev->impulse, SATTACK_FIREMODE1) ? 1 : 0 ); + if(iResult == 1) + { + m_pPlayer->pev->punchangle.x = fPunchAngle2(); + m_pPlayer->pev->velocity = m_pPlayer->pev->velocity - gpGlobals->v_forward * fRecoil2(); + + SecondaryPostAttack(); //run post effects + if ( m_flNextSecondaryAttack < UTIL_WeaponTimeBase() ) + m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + fNextAttack2() + 0.02; + m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + fNextAttack2(); + m_flTimeWeaponIdle = UTIL_WeaponTimeBase() + RANDOM_LONG(10, 15); + } + else if(iResult == 0) + { + PlayEmptySound( ); + m_flNextSecondaryAttack = UTIL_WeaponTimeBase() + 0.5; + } + m_iStepReload = 0;//reset reload + } + + virtual void Reload( void ){ DefaultReload( ACT_VM_RELOAD ); } // do "+RELOAD" + virtual void PostIdle( void ) {} // calling every frame + virtual void WeaponIdle( void ) // called when no buttons pressed + { + if( !stricmp( pszAmmo1(), "grenade" )) + { + if(m_flHoldTime) Launch( pszAmmo1()); + else DefaultIdle(); + } + else DefaultIdle(); + } + virtual void Deploy( void ); // deploy function + virtual void Holster( void ); // holster function + + //weapon saved variables + float m_flNextPrimaryAttack; // soonest time ItemPostFrame will call PrimaryAttack + float m_flNextSecondaryAttack; // soonest time ItemPostFrame will call SecondaryAttack + float m_flTimeWeaponIdle; // soonest time ItemPostFrame will call WeaponIdle + float m_flTimeUpdate; // special time for additional effects + int m_iPrimaryAmmoType; // "primary" ammo index into players m_rgAmmo[] + int m_iSecondaryAmmoType; // "secondary" ammo index into players m_rgAmmo[] + int m_iDefaultAmmo; // how much primary ammo you get + int m_iDefaultAmmo2; // how much secondary ammo you get + int m_cActiveRocket; // how many rockets is now active ? + int m_iOnControl; // controllable rocket is on control now + int m_iStepReload; // reload state + int m_iClip; // current clip state + int m_iBody; // viewmodel body + int m_iSkin; // viewmodel skin + int m_iSpot; // enable laser dot + int m_iZoom; // zoom current level + + //weapon nonsaved variables + float m_flHoldTime; // button holdtime + int m_iClientClip; // the last version of m_iClip sent to hud dll + int m_iClientWeaponState; // the last version of the weapon state sent to hud dll (is current weapon, is on target) + int m_iPlayEmptySound; // trigger for empty sound + int m_fInReload; // Are we in the middle of a reload; + int m_iClientSkin; // synch server and client skin + int m_iClientBody; // synch server and client body + int b_restored; // restore body and skin +}; + +#endif // WEAPONS_H diff --git a/server/ents/baseworld.cpp b/server/ents/baseworld.cpp new file mode 100644 index 00000000..f4f08140 --- /dev/null +++ b/server/ents/baseworld.cpp @@ -0,0 +1,59 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "client.h" + + +void CWorld :: Precache( void ) +{ + g_pWorld = this; + m_pLinkList = NULL; + InitWorld(); +} + +void CWorld :: KeyValue( KeyValueData *pkvd ) +{ + if ( FStrEq(pkvd->szKeyName, "skyname") ) + { + // Sent over net now. + CVAR_SET_STRING( "sv_skyname", pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if ( FStrEq(pkvd->szKeyName, "chaptertitle") ) + { + pev->netname = ALLOC_STRING( pkvd->szValue ); + CVAR_SET_FLOAT( "sv_newunit", 1 ); //new chapter + pkvd->fHandled = TRUE; + } + else if ( FStrEq(pkvd->szKeyName, "gametitle") ) + { + SetBits( pev->spawnflags, SF_START_ON ); + pkvd->fHandled = TRUE; + } +} + +void CWorld :: Spawn( void ) +{ + g_fGameOver = FALSE; + Precache(); +} + +void CWorld :: PostActivate( void ) +{ + //run post messages + if ( pev->netname ) + { + UTIL_ShowMessageAll( STRING(pev->netname) ); + pev->netname = iStringNull; + } + if ( pev->spawnflags & SF_START_ON ) + { + SERVER_COMMAND( "gametitle\n" ); + ClearBits( pev->spawnflags, SF_START_ON ); + } +} +LINK_ENTITY_TO_CLASS( worldspawn, CWorld ); \ No newline at end of file diff --git a/server/ents/baseworld.h b/server/ents/baseworld.h new file mode 100644 index 00000000..46b53573 --- /dev/null +++ b/server/ents/baseworld.h @@ -0,0 +1,20 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#ifndef BASEWORLD_H +#define BASEWORLD_H + +class CWorld : public CBaseEntity +{ +public: + void Spawn( void ); + void Precache( void ); + void KeyValue( KeyValueData *pkvd ); + void PostActivate( void ); +}; + +extern BOOL g_startSuit; +extern CWorld *g_pWorld; + +#endif //BASEWORLD_H \ No newline at end of file diff --git a/server/game/game.cpp b/server/game/game.cpp new file mode 100644 index 00000000..e609f437 --- /dev/null +++ b/server/game/game.cpp @@ -0,0 +1,44 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#include "extdll.h" +#include "utils.h" +#include "game.h" + +// Register your console variables here +// This gets called one time when the game is initialied +void GameDLLInit( void ) +{ + // register cvars here: + CVAR_REGISTER( "sv_soundlist", "0", 0, "show server sound list" ); + + CVAR_REGISTER( "mp_teamplay", "0", CVAR_SERVERINFO, "sets to 1 to indicate teamplay" ); + CVAR_REGISTER( "mp_fraglimit", "0", CVAR_SERVERINFO, "limit of frags for current server" ); + CVAR_REGISTER( "mp_timelimit", "0", CVAR_SERVERINFO, "server timelimit" ); + + CVAR_REGISTER( "mp_fragsleft", "0", CVAR_SERVERINFO, "counter that indicated how many frags remaining" ); + CVAR_REGISTER( "mp_timeleft", "0" , CVAR_SERVERINFO, "counter that indicated how many time remaining" ); + + CVAR_REGISTER( "mp_friendlyfire", "0", CVAR_SERVERINFO, "enables firedlyfire for teamplay" ); + CVAR_REGISTER( "mp_falldamage", "0", CVAR_SERVERINFO, "falldamage multiplier" ); + CVAR_REGISTER( "mp_weaponstay", "0", CVAR_SERVERINFO, "weapon leave stays on ground" ); + CVAR_REGISTER( "mp_forcerespawn", "1", CVAR_SERVERINFO, "force client respawn after his death" ); + CVAR_REGISTER( "mp_flashlight", "0", CVAR_SERVERINFO, "attempt to use flashlight in multiplayer" ); + CVAR_REGISTER( "mp_autocrosshair", "1", CVAR_SERVERINFO, "enables auto-aim in multiplayer" ); + CVAR_REGISTER( "decalfrequency", "30", CVAR_SERVERINFO, "how many decals can be spawned" ); + CVAR_REGISTER( "mp_teamlist", "hgrunt,scientist", CVAR_SERVERINFO, "names of default teams" ); + CVAR_REGISTER( "mp_teamoverride", "1", 0, "can ovveride teams from map settings ?" ); + CVAR_REGISTER( "mp_defaultteam", "0", 0, "use default team instead ?" ); + CVAR_REGISTER( "mp_chattime", "10", CVAR_SERVERINFO, "time beetween messages" ); +} \ No newline at end of file diff --git a/server/game/game.h b/server/game/game.h new file mode 100644 index 00000000..b6a8e8b2 --- /dev/null +++ b/server/game/game.h @@ -0,0 +1,26 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ + +#ifndef GAME_H +#define GAME_H + +// cvar flags +#define CVAR_ARCHIVE BIT(0) // set to cause it to be saved to vars.rc +#define CVAR_USERINFO BIT(1) // added to userinfo when changed +#define CVAR_SERVERINFO BIT(2) // added to serverinfo when changed + +extern void GameDLLInit( void ); + +#endif // GAME_H diff --git a/server/game/gamerules.cpp b/server/game/gamerules.cpp new file mode 100644 index 00000000..e39ba126 --- /dev/null +++ b/server/game/gamerules.cpp @@ -0,0 +1,164 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +//========================================================= +// GameRules.cpp +//========================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" +#include "baseweapon.h" +#include "gamerules.h" +#include "teamplay_gamerules.h" +#include "game.h" + +extern edict_t *EntSelectSpawnPoint( CBaseEntity *pPlayer ); + +DLL_GLOBAL CGameRules* g_pGameRules = NULL; +extern DLL_GLOBAL BOOL g_fGameOver; +extern int gmsgDeathMsg; // client dll messages +extern int gmsgMOTD; + +int g_teamplay = 0; + +//========================================================= +//========================================================= +BOOL CGameRules::CanHaveAmmo( CBasePlayer *pPlayer, const char *pszAmmoName, int iMaxCarry ) +{ + int iAmmoIndex; + + if ( pszAmmoName ) + { + iAmmoIndex = pPlayer->GetAmmoIndex( pszAmmoName ); + + if ( iAmmoIndex > -1 ) + { + if ( pPlayer->AmmoInventory( iAmmoIndex ) < iMaxCarry ) + { + // player has room for more of this type of ammo + return TRUE; + } + } + } + + return FALSE; +} + +//========================================================= +//========================================================= +edict_t *CGameRules :: GetPlayerSpawnSpot( CBasePlayer *pPlayer ) +{ + edict_t *pentSpawnSpot = EntSelectSpawnPoint( pPlayer ); + + pPlayer->pev->origin = VARS(pentSpawnSpot)->origin + Vector(0,0,1); + pPlayer->pev->v_angle = g_vecZero; + pPlayer->pev->velocity = g_vecZero; + pPlayer->pev->angles = VARS(pentSpawnSpot)->angles; + pPlayer->pev->punchangle = g_vecZero; + pPlayer->pev->fixangle = TRUE; + + //LRC + if (pentSpawnSpot->v.spawnflags & 1) // the START WITH SUIT flag + { + g_startSuit = TRUE; + } + + return pentSpawnSpot; +} + +//========================================================= +//========================================================= +BOOL CGameRules::CanHavePlayerItem( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ) +{ + // only living players can have items + if ( pPlayer->pev->deadflag != DEAD_NO ) + return FALSE; + + if ( pWeapon->pszAmmo1() ) + { + if ( !CanHaveAmmo( pPlayer, pWeapon->pszAmmo1(), pWeapon->iMaxAmmo1() ) ) + { + // we can't carry anymore ammo for this gun. We can only + // have the gun if we aren't already carrying one of this type + if ( pPlayer->HasPlayerItem( pWeapon ) ) + { + return FALSE; + } + } + } + else + { + // weapon doesn't use ammo, don't take another if you already have it. + if ( pPlayer->HasPlayerItem( pWeapon ) ) + { + return FALSE; + } + } + + // note: will fall through to here if GetItemInfo doesn't fill the struct! + return TRUE; +} + +//========================================================= +// load the SkillData struct with the proper values based on the skill level. +//========================================================= +void CGameRules::RefreshSkillData ( void ) +{ +} + +//========================================================= +// instantiate the proper game rules object +//========================================================= + +CGameRules *InstallGameRules( void ) +{ + SERVER_COMMAND( "exec game.rc\n" ); + SERVER_EXECUTE( ); + + ALERT( at_console, "InstallGameRules\n" ); + + if ( !gpGlobals->deathmatch ) + { + // generic half-life + g_teamplay = 0; + return new CHalfLifeRules; + } + else + { + if( CVAR_GET_FLOAT( "mp_teamplay" ) > 0 ) + { + // teamplay + + g_teamplay = 1; + return new CHalfLifeTeamplay; + } + if ((int)gpGlobals->deathmatch == 1) + { + // vanilla deathmatch + g_teamplay = 0; + return new CHalfLifeMultiplay; + } + else + { + // vanilla deathmatch?? + g_teamplay = 0; + return new CHalfLifeMultiplay; + } + } +} + + + diff --git a/server/game/gamerules.h b/server/game/gamerules.h new file mode 100644 index 00000000..f834a1e2 --- /dev/null +++ b/server/game/gamerules.h @@ -0,0 +1,307 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +//========================================================= +// GameRules +//========================================================= + +class CBasePlayerWeapon; +class CBasePlayer; +class CItem; +class CBasePlayerAmmo; + +// weapon respawning return codes +enum +{ + GR_NONE = 0, + + GR_WEAPON_RESPAWN_YES, + GR_WEAPON_RESPAWN_NO, + + GR_AMMO_RESPAWN_YES, + GR_AMMO_RESPAWN_NO, + + GR_ITEM_RESPAWN_YES, + GR_ITEM_RESPAWN_NO, + + GR_PLR_DROP_GUN_ALL, + GR_PLR_DROP_GUN_ACTIVE, + GR_PLR_DROP_GUN_NO, + + GR_PLR_DROP_AMMO_ALL, + GR_PLR_DROP_AMMO_ACTIVE, + GR_PLR_DROP_AMMO_NO, +}; + +// Player relationship return codes +enum +{ + GR_NOTTEAMMATE = 0, + GR_TEAMMATE, + GR_ENEMY, + GR_ALLY, + GR_NEUTRAL, +}; + +class CGameRules +{ +public: + virtual void RefreshSkillData( void );// fill skill data struct with proper values + virtual void Think( void ) = 0;// GR_Think - runs every server frame, should handle any timer tasks, periodic events, etc. + virtual BOOL IsAllowedToSpawn( CBaseEntity *pEntity ) = 0; // Can this item spawn (eg monsters don't spawn in deathmatch). + + virtual BOOL FAllowFlashlight( void ) = 0;// Are players allowed to switch on their flashlight? + virtual BOOL FShouldSwitchWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ) = 0;// should the player switch to this weapon? + virtual BOOL GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pCurrentWeapon ) = 0;// I can't use this weapon anymore, get me the next best one. + +// Functions to verify the single/multiplayer status of a game + virtual BOOL IsMultiplayer( void ) = 0;// is this a multiplayer game? (either coop or deathmatch) + virtual BOOL IsDeathmatch( void ) = 0;//is this a deathmatch game? + virtual BOOL IsTeamplay( void ) { return FALSE; };// is this deathmatch game being played with team rules? + virtual BOOL IsCoOp( void ) = 0;// is this a coop game? + +// Client connection/disconnection + virtual BOOL ClientConnected( edict_t *pEntity, const char *userinfo ) = 0; // a client just connected to the server (player hasn't spawned yet) + virtual void InitHUD( CBasePlayer *pl ) = 0; // the client dll is ready for updating + virtual void ClientDisconnected( edict_t *pClient ) = 0;// a client just disconnected from the server + virtual void UpdateGameMode( CBasePlayer *pPlayer ) {} // the client needs to be informed of the current game mode + +// Client damage rules + virtual float FlPlayerFallDamage( CBasePlayer *pPlayer ) = 0;// this client just hit the ground after a fall. How much damage? + virtual BOOL FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker ) {return TRUE;};// can this player take damage from this attacker? + virtual BOOL ShouldAutoAim( CBasePlayer *pPlayer, edict_t *target ) { return TRUE; } + +// Client spawn/respawn control + virtual void PlayerSpawn( CBasePlayer *pPlayer ) = 0;// called by CBasePlayer::Spawn just before releasing player into the game + virtual void PlayerThink( CBasePlayer *pPlayer ) = 0; // called by CBasePlayer::PreThink every frame, before physics are run and after keys are accepted + virtual BOOL FPlayerCanRespawn( CBasePlayer *pPlayer ) = 0;// is this player allowed to respawn now? + virtual float FlPlayerSpawnTime( CBasePlayer *pPlayer ) = 0;// When in the future will this player be able to spawn? + virtual edict_t *GetPlayerSpawnSpot( CBasePlayer *pPlayer );// Place this player on their spawnspot and face them the proper direction. + + virtual BOOL AllowAutoTargetCrosshair( void ) { return TRUE; }; + virtual BOOL ClientCommand( CBasePlayer *pPlayer, const char *pcmd ) { return FALSE; }; // handles the user commands; returns TRUE if command handled properly + virtual void ClientUserInfoChanged( CBasePlayer *pPlayer, char *infobuffer ) {} // the player has changed userinfo; can change it now + +// Client kills/scoring + virtual int IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ) = 0;// how many points do I award whoever kills this player? + virtual void PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ) = 0;// Called each time a player dies + virtual void DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor )= 0;// Call this from within a GameRules class to report an obituary. +// Weapon retrieval + virtual BOOL CanHavePlayerItem( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon );// The player is touching an CBasePlayerWeapon, do I give it to him? + virtual void PlayerGotWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ) = 0;// Called each time a player picks up a weapon from the ground + +// Weapon spawn/respawn control + virtual int WeaponShouldRespawn( CBasePlayerWeapon *pWeapon ) = 0;// should this weapon respawn? + virtual float FlWeaponRespawnTime( CBasePlayerWeapon *pWeapon ) = 0;// when may this weapon respawn? + virtual float FlWeaponTryRespawn( CBasePlayerWeapon *pWeapon ) = 0; // can i respawn now, and if not, when should i try again? + virtual Vector VecWeaponRespawnSpot( CBasePlayerWeapon *pWeapon ) = 0;// where in the world should this weapon respawn? + +// Ammo retrieval + virtual BOOL CanHaveAmmo( CBasePlayer *pPlayer, const char *pszAmmoName, int iMaxCarry );// can this player take more of this ammo? + +// Healthcharger respawn control + virtual float FlHealthChargerRechargeTime( void ) = 0;// how long until a depleted HealthCharger recharges itself? + virtual float FlHEVChargerRechargeTime( void ) { return 0; }// how long until a depleted HealthCharger recharges itself? + +// What happens to a dead player's weapons + virtual int DeadPlayerWeapons( CBasePlayer *pPlayer ) = 0;// what do I do with a player's weapons when he's killed? + +// What happens to a dead player's ammo + virtual int DeadPlayerAmmo( CBasePlayer *pPlayer ) = 0;// Do I drop ammo when the player dies? How much? + +// Teamplay stuff + virtual const char *GetTeamID( CBaseEntity *pEntity ) = 0;// what team is this entity on? + virtual int PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ) = 0;// What is the player's relationship with this entity? + virtual int GetTeamIndex( const char *pTeamName ) { return -1; } + virtual const char *GetIndexedTeamName( int teamIndex ) { return ""; } + virtual BOOL IsValidTeam( const char *pTeamName ) { return TRUE; } + virtual void ChangePlayerTeam( CBasePlayer *pPlayer, const char *pTeamName, BOOL bKill, BOOL bGib ) {} + virtual const char *SetDefaultPlayerTeam( CBasePlayer *pPlayer ) { return ""; } + +// Sounds + virtual BOOL PlayTextureSounds( void ) { return TRUE; } + virtual BOOL PlayFootstepSounds( CBasePlayer *pl, float fvol ) { return TRUE; } + +// Monsters + virtual BOOL FAllowMonsters( void ) = 0;//are monsters allowed + + // Immediately end a multiplayer game + virtual void EndMultiplayerGame( void ) {} +}; + +extern CGameRules *InstallGameRules( void ); + + +//========================================================= +// CHalfLifeRules - rules for the single player Half-Life +// game. +//========================================================= +class CHalfLifeRules : public CGameRules +{ +public: + CHalfLifeRules ( void ); + +// GR_Think + virtual void Think( void ); + virtual BOOL IsAllowedToSpawn( CBaseEntity *pEntity ); + virtual BOOL FAllowFlashlight( void ) { return TRUE; }; + + virtual BOOL FShouldSwitchWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ); + virtual BOOL GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pCurrentWeapon ); + +// Functions to verify the single/multiplayer status of a game + virtual BOOL IsMultiplayer( void ); + virtual BOOL IsDeathmatch( void ); + virtual BOOL IsCoOp( void ); + +// Client connection/disconnection + virtual BOOL ClientConnected( edict_t *pEntity, const char *userinfo ); + virtual void InitHUD( CBasePlayer *pl ); // the client dll is ready for updating + virtual void ClientDisconnected( edict_t *pClient ); + +// Client damage rules + virtual float FlPlayerFallDamage( CBasePlayer *pPlayer ); + +// Client spawn/respawn control + virtual void PlayerSpawn( CBasePlayer *pPlayer ); + virtual void PlayerThink( CBasePlayer *pPlayer ); + virtual BOOL FPlayerCanRespawn( CBasePlayer *pPlayer ); + virtual float FlPlayerSpawnTime( CBasePlayer *pPlayer ); + + virtual BOOL AllowAutoTargetCrosshair( void ); + +// Client kills/scoring + virtual int IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ); + virtual void PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ); + virtual void DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ); + +// Weapon retrieval + virtual void PlayerGotWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ); + +// Weapon spawn/respawn control + virtual int WeaponShouldRespawn( CBasePlayerWeapon *pWeapon ); + virtual float FlWeaponRespawnTime( CBasePlayerWeapon *pWeapon ); + virtual float FlWeaponTryRespawn( CBasePlayerWeapon *pWeapon ); + virtual Vector VecWeaponRespawnSpot( CBasePlayerWeapon *pWeapon ); + +// Healthcharger respawn control + virtual float FlHealthChargerRechargeTime( void ); + +// What happens to a dead player's weapons + virtual int DeadPlayerWeapons( CBasePlayer *pPlayer ); + +// What happens to a dead player's ammo + virtual int DeadPlayerAmmo( CBasePlayer *pPlayer ); + +// Monsters + virtual BOOL FAllowMonsters( void ); + +// Teamplay stuff + virtual const char *GetTeamID( CBaseEntity *pEntity ) {return "";}; + virtual int PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ); +}; + +//========================================================= +// CHalfLifeMultiplay - rules for the basic half life multiplayer +// competition +//========================================================= +class CHalfLifeMultiplay : public CGameRules +{ +public: + CHalfLifeMultiplay(); + +// GR_Think + virtual void Think( void ); + virtual void RefreshSkillData( void ); + virtual BOOL IsAllowedToSpawn( CBaseEntity *pEntity ); + virtual BOOL FAllowFlashlight( void ); + + virtual BOOL FShouldSwitchWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ); + virtual BOOL GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pCurrentWeapon ); + +// Functions to verify the single/multiplayer status of a game + virtual BOOL IsMultiplayer( void ); + virtual BOOL IsDeathmatch( void ); + virtual BOOL IsCoOp( void ); + +// Client connection/disconnection + // If ClientConnected returns FALSE, the connection is rejected and the user is provided the reason specified in + // svRejectReason + // Only the client's name and remote address are provided to the dll for verification. + virtual BOOL ClientConnected( edict_t *pEntity, const char *userinfo ); + virtual void InitHUD( CBasePlayer *pl ); // the client dll is ready for updating + virtual void ClientDisconnected( edict_t *pClient ); + virtual void UpdateGameMode( CBasePlayer *pPlayer ); // the client needs to be informed of the current game mode + +// Client damage rules + virtual float FlPlayerFallDamage( CBasePlayer *pPlayer ); + virtual BOOL FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker ); + +// Client spawn/respawn control + virtual void PlayerSpawn( CBasePlayer *pPlayer ); + virtual void PlayerThink( CBasePlayer *pPlayer ); + virtual BOOL FPlayerCanRespawn( CBasePlayer *pPlayer ); + virtual float FlPlayerSpawnTime( CBasePlayer *pPlayer ); + virtual edict_t *GetPlayerSpawnSpot( CBasePlayer *pPlayer ); + + virtual BOOL AllowAutoTargetCrosshair( void ); + virtual BOOL ClientCommand( CBasePlayer *pPlayer, const char *pcmd ); + +// Client kills/scoring + virtual int IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ); + virtual void PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ); + virtual void DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ); + +// Weapon retrieval + virtual void PlayerGotWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ); + virtual BOOL CanHavePlayerItem( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon );// The player is touching an CBasePlayerWeapon, do I give it to him? + +// Weapon spawn/respawn control + virtual int WeaponShouldRespawn( CBasePlayerWeapon *pWeapon ); + virtual float FlWeaponRespawnTime( CBasePlayerWeapon *pWeapon ); + virtual float FlWeaponTryRespawn( CBasePlayerWeapon *pWeapon ); + virtual Vector VecWeaponRespawnSpot( CBasePlayerWeapon *pWeapon ); + +// Healthcharger respawn control + virtual float FlHealthChargerRechargeTime( void ); + virtual float FlHEVChargerRechargeTime( void ); + +// What happens to a dead player's weapons + virtual int DeadPlayerWeapons( CBasePlayer *pPlayer ); + +// What happens to a dead player's ammo + virtual int DeadPlayerAmmo( CBasePlayer *pPlayer ); + +// Teamplay stuff + virtual const char *GetTeamID( CBaseEntity *pEntity ) {return "";} + virtual int PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ); + + virtual BOOL PlayTextureSounds( void ) { return FALSE; } + virtual BOOL PlayFootstepSounds( CBasePlayer *pl, float fvol ); + +// Monsters + virtual BOOL FAllowMonsters( void ); + + // Immediately end a multiplayer game + virtual void EndMultiplayerGame( void ) { GoToIntermission(); } + +protected: + virtual void ChangeLevel( void ); + virtual void GoToIntermission( void ); + float m_flIntermissionEndTime; + BOOL m_iEndIntermissionButtonHit; + void SendMOTDToClient( edict_t *client ); +}; + +extern DLL_GLOBAL CGameRules* g_pGameRules; diff --git a/server/game/lights.cpp b/server/game/lights.cpp new file mode 100644 index 00000000..e3567fe4 --- /dev/null +++ b/server/game/lights.cpp @@ -0,0 +1,280 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +/* + +===== lights.cpp ======================================================== + + spawn and think functions for editor-placed lights + +*/ + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" + +class CLight : public CBaseLogic +{ +public: + void KeyValue( KeyValueData* pkvd ); + void Spawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void Think( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + int GetStyle( void ) { return m_iszCurrentStyle; }; + void SetStyle( int iszPattern ); + void SetCorrectStyle( void ); + +private: + int m_iOnStyle; // style to use while on + int m_iOffStyle; // style to use while off + int m_iTurnOnStyle; // style to use while turning on + int m_iTurnOffStyle; // style to use while turning off + int m_iTurnOnTime; // time taken to turn on + int m_iTurnOffTime; // time taken to turn off + int m_iszPattern; // custom style to use while on + int m_iszCurrentStyle; // current style string +}; +LINK_ENTITY_TO_CLASS( light, CLight ); +LINK_ENTITY_TO_CLASS( light_spot, CLight ); + +TYPEDESCRIPTION CLight::m_SaveData[] = +{ + DEFINE_FIELD( CLight, m_iState, FIELD_INTEGER ), + DEFINE_FIELD( CLight, m_iszPattern, FIELD_STRING ), + DEFINE_FIELD( CLight, m_iszCurrentStyle, FIELD_STRING ), + DEFINE_FIELD( CLight, m_iOnStyle, FIELD_INTEGER ), + DEFINE_FIELD( CLight, m_iOffStyle, FIELD_INTEGER ), + DEFINE_FIELD( CLight, m_iTurnOnStyle, FIELD_INTEGER ), + DEFINE_FIELD( CLight, m_iTurnOffStyle, FIELD_INTEGER ), + DEFINE_FIELD( CLight, m_iTurnOnTime, FIELD_INTEGER ), + DEFINE_FIELD( CLight, m_iTurnOffTime, FIELD_INTEGER ), +}; +IMPLEMENT_SAVERESTORE( CLight, CBaseLogic ); + +// +// Cache user-entity-field values until spawn is called. +// +void CLight :: KeyValue( KeyValueData* pkvd) +{ + if (FStrEq(pkvd->szKeyName, "m_iOnStyle")) + { + m_iOnStyle = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iOffStyle")) + { + m_iOffStyle = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iTurnOnStyle")) + { + m_iTurnOnStyle = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iTurnOffStyle")) + { + m_iTurnOffStyle = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iTurnOnTime")) + { + m_iTurnOnTime = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iTurnOffTime")) + { + m_iTurnOffTime = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "pitch")) + { + pev->angles.x = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "pattern")) + { + m_iszPattern = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else CBaseEntity::KeyValue( pkvd ); +} + +void CLight :: SetStyle ( int iszPattern ) +{ + if (m_iStyle < 32) // if it's using a global style, don't change it + return; + m_iszCurrentStyle = iszPattern; + LIGHT_STYLE(m_iStyle, (char *)STRING( iszPattern )); +} + +void CLight :: SetCorrectStyle ( void ) +{ + if (m_iStyle >= 32) + { + switch (m_iState) + { + case STATE_ON: + if (m_iszPattern) // custom styles have priority over standard ones + SetStyle( m_iszPattern ); + else if (m_iOnStyle) + SetStyle(GetStdLightStyle(m_iOnStyle)); + else SetStyle(MAKE_STRING("m")); + break; + case STATE_OFF: + if (m_iOffStyle) + SetStyle(GetStdLightStyle(m_iOffStyle)); + else SetStyle(MAKE_STRING("a")); + break; + case STATE_TURN_ON: + if (m_iTurnOnStyle) + SetStyle(GetStdLightStyle(m_iTurnOnStyle)); + else SetStyle(MAKE_STRING("a")); + break; + case STATE_TURN_OFF: + if (m_iTurnOffStyle) + SetStyle(GetStdLightStyle(m_iTurnOffStyle)); + else SetStyle(MAKE_STRING("m")); + break; + } + } + else m_iszCurrentStyle = GetStdLightStyle( m_iStyle ); +} + +void CLight :: Think( void ) +{ + switch (GetState()) + { + case STATE_TURN_ON: + m_iState = STATE_ON; + UTIL_FireTargets( pev->target, this, this, USE_ON ); + break; + case STATE_TURN_OFF: + m_iState = STATE_OFF; + UTIL_FireTargets(pev->target, this, this, USE_OFF ); + break; + } + SetCorrectStyle(); +} + +void CLight :: Spawn( void ) +{ + if (FStringNull(pev->targetname)) + { // inert light + REMOVE_ENTITY(ENT(pev)); + return; + } + + if (FBitSet(pev->spawnflags,SF_LIGHT_START_OFF)) + m_iState = STATE_OFF; + else m_iState = STATE_ON; + SetCorrectStyle(); +} + +void CLight :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (m_iStyle >= 32) + { + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_ON || m_iState == STATE_TURN_ON) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + if (m_iTurnOnTime) + { + m_iState = STATE_TURN_ON; + SetNextThink( m_iTurnOnTime ); + } + else m_iState = STATE_ON; + } + else if(useType == USE_OFF) + { + if (m_iTurnOffTime) + { + m_iState = STATE_TURN_OFF; + SetNextThink( m_iTurnOffTime ); + } + else m_iState = STATE_OFF; + } + SetCorrectStyle(); + } +} + +class CEnvLight : public CLight +{ +public: + void KeyValue( KeyValueData* pkvd ); + void Spawn( void ); +}; +LINK_ENTITY_TO_CLASS( light_environment, CEnvLight ); + +void CEnvLight::KeyValue( KeyValueData* pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "_light")) + { + int r, g, b, v, j; + char szColor[64]; + j = sscanf( pkvd->szValue, "%d %d %d %d\n", &r, &g, &b, &v ); + if (j == 1) + { + g = b = r; + } + else if (j == 4) + { + r = r * (v / 255.0); + g = g * (v / 255.0); + b = b * (v / 255.0); + } + + // simulate qrad direct, ambient,and gamma adjustments, as well as engine scaling + r = pow( r / 114.0, 0.6 ) * 264; + g = pow( g / 114.0, 0.6 ) * 264; + b = pow( b / 114.0, 0.6 ) * 264; + + pkvd->fHandled = TRUE; + sprintf( szColor, "%d", r ); + CVAR_SET_STRING( "sv_skycolor_r", szColor ); + sprintf( szColor, "%d", g ); + CVAR_SET_STRING( "sv_skycolor_g", szColor ); + sprintf( szColor, "%d", b ); + CVAR_SET_STRING( "sv_skycolor_b", szColor ); + } + else if (FStrEq(pkvd->szKeyName, "pitch")) + { + pev->angles.x = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } +} + + +void CEnvLight :: Spawn( void ) +{ + char szVector[64]; + UTIL_MakeAimVectors( pev->angles ); + + sprintf( szVector, "%f", gpGlobals->v_forward.x ); + CVAR_SET_STRING( "sv_skyvec_x", szVector ); + sprintf( szVector, "%f", gpGlobals->v_forward.y ); + CVAR_SET_STRING( "sv_skyvec_y", szVector ); + sprintf( szVector, "%f", gpGlobals->v_forward.z ); + CVAR_SET_STRING( "sv_skyvec_z", szVector ); + + CLight::Spawn( ); +} \ No newline at end of file diff --git a/server/game/multiplay_gamerules.cpp b/server/game/multiplay_gamerules.cpp new file mode 100644 index 00000000..9211c6da --- /dev/null +++ b/server/game/multiplay_gamerules.cpp @@ -0,0 +1,1345 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// teamplay_gamerules.cpp +// +#include + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" +#include "baseweapon.h" +#include "gamerules.h" + +#include "game.h" +#include "defaults.h" + +extern DLL_GLOBAL CGameRules *g_pGameRules; +extern DLL_GLOBAL BOOL g_fGameOver; +extern int gmsgDeathMsg; // client dll messages +extern int gmsgScoreInfo; +extern int gmsgMOTD; +extern int gmsgServerName; + +extern int g_teamplay; + +#define ITEM_RESPAWN_TIME 30 +#define WEAPON_RESPAWN_TIME 20 +#define AMMO_RESPAWN_TIME 20 + +float g_flIntermissionStartTime = 0; + +//********************************************************* +// Rules for the half-life multiplayer game. +//********************************************************* + +CHalfLifeMultiplay :: CHalfLifeMultiplay() +{ + RefreshSkillData(); + m_flIntermissionEndTime = 0; + g_flIntermissionStartTime = 0; + + // 11/8/98 + // Modified by YWB: Server .cfg file is now a cvar, so that + // server ops can run multiple game servers, with different server .cfg files, + // from a single installed directory. + // Mapcyclefile is already a cvar. + + // 3/31/99 + // Added lservercfg file cvar, since listen and dedicated servers should not + // share a single config file. (sjb) + if ( IS_DEDICATED_SERVER() ) + { + // dedicated server + char *servercfgfile = (char *)CVAR_GET_STRING( "servercfgfile" ); + + if ( servercfgfile && servercfgfile[0] ) + { + char szCommand[256]; + + ALERT( at_debug, "Executing dedicated server config file\n" ); + sprintf( szCommand, "exec %s\n", servercfgfile ); + SERVER_COMMAND( szCommand ); + } + } + else + { + // listen server + char *lservercfgfile = (char *)CVAR_GET_STRING( "lservercfgfile" ); + + if ( lservercfgfile && lservercfgfile[0] ) + { + char szCommand[256]; + + ALERT( at_debug, "Executing listen server config file\n" ); + sprintf( szCommand, "exec %s\n", lservercfgfile ); + SERVER_COMMAND( szCommand ); + } + } +} + +BOOL CHalfLifeMultiplay::ClientCommand( CBasePlayer *pPlayer, const char *pcmd ) +{ + return CGameRules::ClientCommand(pPlayer, pcmd); +} + +//========================================================= +//========================================================= +void CHalfLifeMultiplay::RefreshSkillData( void ) +{ +} + +// longest the intermission can last, in seconds +#define MAX_INTERMISSION_TIME 120 + +//========================================================= +//========================================================= +void CHalfLifeMultiplay :: Think ( void ) +{ + ///// Check game rules ///// + static int last_frags; + static int last_time; + + int frags_remaining = 0; + int time_remaining = 0; + + if ( g_fGameOver ) // someone else quit the game already + { + // bounds check + int time = (int)CVAR_GET_FLOAT( "mp_chattime" ); + if ( time < 1 ) + CVAR_SET_STRING( "mp_chattime", "1" ); + else if ( time > MAX_INTERMISSION_TIME ) + CVAR_SET_STRING( "mp_chattime", UTIL_dtos1( MAX_INTERMISSION_TIME ) ); + + m_flIntermissionEndTime = g_flIntermissionStartTime + CVAR_GET_FLOAT( "mp_chattime" ); + + // check to see if we should change levels now + if ( m_flIntermissionEndTime < gpGlobals->time ) + { + if ( m_iEndIntermissionButtonHit // check that someone has pressed a key, or the max intermission time is over + || ( ( g_flIntermissionStartTime + MAX_INTERMISSION_TIME ) < gpGlobals->time) ) + ChangeLevel(); // intermission is over + } + + return; + } + + float flTimeLimit = CVAR_GET_FLOAT( "mp_timelimit" ) * 60; + float flFragLimit = CVAR_GET_FLOAT( "mp_fraglimit" ); + + time_remaining = (int)(flTimeLimit ? ( flTimeLimit - gpGlobals->time ) : 0); + + if ( flTimeLimit != 0 && gpGlobals->time >= flTimeLimit ) + { + GoToIntermission(); + return; + } + + if ( flFragLimit ) + { + int bestfrags = 9999; + int remain; + + // check if any player is over the frag limit + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *pPlayer = UTIL_PlayerByIndex( i ); + + if ( pPlayer && pPlayer->pev->frags >= flFragLimit ) + { + GoToIntermission(); + return; + } + + + if ( pPlayer ) + { + remain = flFragLimit - pPlayer->pev->frags; + if ( remain < bestfrags ) + { + bestfrags = remain; + } + } + + } + frags_remaining = bestfrags; + } + + // Updates when frags change + if ( frags_remaining != last_frags ) + { + g_engfuncs.pfnCVarSetString( "mp_fragsleft", UTIL_VarArgs( "%i", frags_remaining ) ); + } + + // Updates once per second + if( CVAR_GET_FLOAT( "mp_timeleft" ) != last_time ) + { + g_engfuncs.pfnCVarSetString( "mp_timeleft", UTIL_VarArgs( "%i", time_remaining ) ); + } + + last_frags = frags_remaining; + last_time = time_remaining; +} + + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay::IsMultiplayer( void ) +{ + return TRUE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay::IsDeathmatch( void ) +{ + return TRUE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay::IsCoOp( void ) +{ + return gpGlobals->coop; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay::FShouldSwitchWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ) +{ + if ( !pWeapon->CanDeploy() ) + { + // that weapon can't deploy anyway. + return FALSE; + } + + if ( !pPlayer->m_pActiveItem ) + { + // player doesn't have an active item! + return TRUE; + } + + if ( !pPlayer->m_pActiveItem->CanHolster() ) + { + // can't put away the active item. + return FALSE; + } + + if ( pWeapon->iWeight() > pPlayer->m_pActiveItem->iWeight() ) + { + return TRUE; + } + + return FALSE; +} + +BOOL CHalfLifeMultiplay :: GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pCurrentWeapon ) +{ + + CBasePlayerWeapon *pCheck; + CBasePlayerWeapon *pBest;// this will be used in the event that we don't find a weapon in the same category. + int iBestWeight; + int i; + + iBestWeight = -1;// no weapon lower than -1 can be autoswitched to + pBest = NULL; + + if ( !pCurrentWeapon->CanHolster() ) + { + // can't put this gun away right now, so can't switch. + return FALSE; + } + + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + pCheck = pPlayer->m_rgpPlayerItems[ i ]; + + while ( pCheck ) + { + if ( pCheck->iWeight() > -1 && pCheck->iWeight() == pCurrentWeapon->iWeight() && pCheck != pCurrentWeapon ) + { + // this weapon is from the same category. + if ( pCheck->CanDeploy() ) + { + if ( pPlayer->SwitchWeapon( pCheck ) ) + { + return TRUE; + } + } + } + else if ( pCheck->iWeight() > iBestWeight && pCheck != pCurrentWeapon )// don't reselect the weapon we're trying to get rid of + { + //ALERT ( at_console, "Considering %s\n", STRING( pCheck->pev->classname ) ); + // we keep updating the 'best' weapon just in case we can't find a weapon of the same weight + // that the player was using. This will end up leaving the player with his heaviest-weighted + // weapon. + if ( pCheck->CanDeploy() ) + { + // if this weapon is useable, flag it as the best + iBestWeight = pCheck->iWeight(); + pBest = pCheck; + } + } + + pCheck = pCheck->m_pNext; + } + } + + // if we make it here, we've checked all the weapons and found no useable + // weapon in the same catagory as the current weapon. + + // if pBest is null, we didn't find ANYTHING. Shouldn't be possible- should always + // at least get the crowbar, but ya never know. + if ( !pBest ) + { + return FALSE; + } + + pPlayer->SwitchWeapon( pBest ); + + return TRUE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay :: ClientConnected( edict_t *pEntity, const char *userinfo ) +{ + return TRUE; +} + +extern int gmsgSayText; +extern int gmsgGameMode; + +void CHalfLifeMultiplay :: UpdateGameMode( CBasePlayer *pPlayer ) +{ + MESSAGE_BEGIN( MSG_ONE, gmsgGameMode, NULL, pPlayer->edict() ); + WRITE_BYTE( 0 ); // game mode none + MESSAGE_END(); +} + +void CHalfLifeMultiplay :: InitHUD( CBasePlayer *pl ) +{ + // notify other clients of player joining the game + UTIL_ClientPrintAll( HUD_PRINTNOTIFY, UTIL_VarArgs( "%s has joined the game\n", + ( pl->pev->netname && STRING(pl->pev->netname)[0] != 0 ) ? STRING(pl->pev->netname) : "unconnected" ) ); + + UpdateGameMode( pl ); + + // sending just one score makes the hud scoreboard active; otherwise + // it is just disabled for single play + MESSAGE_BEGIN( MSG_ONE, gmsgScoreInfo, NULL, pl->edict() ); + WRITE_BYTE( ENTINDEX(pl->edict()) ); + WRITE_SHORT( 0 ); + WRITE_SHORT( 0 ); + WRITE_SHORT( 0 ); + WRITE_SHORT( 0 ); + MESSAGE_END(); + + SendMOTDToClient( pl->edict() ); + + // loop through all active players and send their score info to the new client + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + { + // FIXME: Probably don't need to cast this just to read m_iDeaths + CBasePlayer *plr = (CBasePlayer *)UTIL_PlayerByIndex( i ); + + if ( plr ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgScoreInfo, NULL, pl->edict() ); + WRITE_BYTE( i ); // client number + WRITE_SHORT( plr->pev->frags ); + WRITE_SHORT( plr->m_iDeaths ); + WRITE_SHORT( 0 ); + WRITE_SHORT( GetTeamIndex( plr->m_szTeamName ) + 1 ); + MESSAGE_END(); + } + } + + if ( g_fGameOver ) + { + MESSAGE_BEGIN( MSG_ONE, SVC_INTERMISSION, NULL, pl->edict() ); + MESSAGE_END(); + } +} + +//========================================================= +//========================================================= +void CHalfLifeMultiplay :: ClientDisconnected( edict_t *pClient ) +{ + if ( pClient ) + { + CBasePlayer *pPlayer = (CBasePlayer *)CBaseEntity::Instance( pClient ); + + if ( pPlayer ) + { + UTIL_FireTargets( "game_playerleave", pPlayer, pPlayer, USE_TOGGLE ); + pPlayer->RemoveAllItems( TRUE );// destroy all of the players weapons and items + } + } +} + +//========================================================= +//========================================================= +float CHalfLifeMultiplay :: FlPlayerFallDamage( CBasePlayer *pPlayer ) +{ + int iFallDamage = (int)CVAR_GET_FLOAT( "mp_falldamage" ); + + switch ( iFallDamage ) + { + case 1://progressive + pPlayer->m_flFallVelocity -= PLAYER_MAX_SAFE_FALL_SPEED; + return pPlayer->m_flFallVelocity * DAMAGE_FOR_FALL_SPEED; + break; + default: + case 0:// fixed + return 10; + break; + } +} + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay::FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker ) +{ + return TRUE; +} + +//========================================================= +//========================================================= +void CHalfLifeMultiplay :: PlayerThink( CBasePlayer *pPlayer ) +{ + if ( g_fGameOver ) + { + // check for button presses + if ( pPlayer->m_afButtonPressed & ( IN_DUCK | IN_ATTACK | IN_ATTACK2 | IN_USE | IN_JUMP ) ) + m_iEndIntermissionButtonHit = TRUE; + + // clear attack/use commands from player + pPlayer->m_afButtonPressed = 0; + pPlayer->pev->button = 0; + pPlayer->m_afButtonReleased = 0; + } +} + +//========================================================= +//========================================================= +void CHalfLifeMultiplay :: PlayerSpawn( CBasePlayer *pPlayer ) +{ + BOOL addDefault; + CBaseEntity *pWeaponEntity = NULL; + + pPlayer->m_iHideHUD |= ITEM_SUIT; + + addDefault = TRUE; + + while ( pWeaponEntity = UTIL_FindEntityByClassname( pWeaponEntity, "game_player_equip" )) + { + pWeaponEntity->Touch( pPlayer ); + addDefault = FALSE; + } + + if ( addDefault ) + { + pPlayer->GiveNamedItem( "weapon_crowbar" ); + pPlayer->GiveNamedItem( "weapon_glock" ); + pPlayer->GiveAmmo( 68, "9mm", _9MM_MAX_CARRY );// 4 full reloads + } +} + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay :: FPlayerCanRespawn( CBasePlayer *pPlayer ) +{ + return TRUE; +} + +//========================================================= +//========================================================= +float CHalfLifeMultiplay :: FlPlayerSpawnTime( CBasePlayer *pPlayer ) +{ + return gpGlobals->time;//now! +} + +BOOL CHalfLifeMultiplay :: AllowAutoTargetCrosshair( void ) +{ + return CVAR_GET_FLOAT( "mp_autocrosshair" ) ? TRUE : FALSE; +} + +//========================================================= +// IPointsForKill - how many points awarded to anyone +// that kills this player? +//========================================================= +int CHalfLifeMultiplay :: IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ) +{ + return 1; +} + + +//========================================================= +// PlayerKilled - someone/something killed this player +//========================================================= +void CHalfLifeMultiplay :: PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ) +{ + DeathNotice( pVictim, pKiller, pInflictor ); + + pVictim->m_iDeaths += 1; + + + UTIL_FireTargets( "game_playerdie", pVictim, pVictim, USE_TOGGLE ); + CBasePlayer *peKiller = NULL; + CBaseEntity *ktmp = CBaseEntity::Instance( pKiller ); + if ( ktmp && (ktmp->Classify() == CLASS_PLAYER) ) + peKiller = (CBasePlayer*)ktmp; + + if ( pVictim->pev == pKiller ) + { // killed self + pKiller->frags -= 1; + } + else if ( ktmp && ktmp->IsPlayer() ) + { + // if a player dies in a deathmatch game and the killer is a client, award the killer some points + pKiller->frags += IPointsForKill( peKiller, pVictim ); + + UTIL_FireTargets( "game_playerkill", ktmp, ktmp, USE_TOGGLE ); + } + else + { // killed by the world +//MH pKiller->frags -= 1; this should be victim (we don't want to give the world frags) + pVictim->pev->frags -= 1; +//END + } + + // update the scores + // killed scores + MESSAGE_BEGIN( MSG_ALL, gmsgScoreInfo ); + WRITE_BYTE( ENTINDEX(pVictim->edict()) ); + WRITE_SHORT( pVictim->pev->frags ); + WRITE_SHORT( pVictim->m_iDeaths ); + WRITE_SHORT( 0 ); + WRITE_SHORT( GetTeamIndex( pVictim->m_szTeamName ) + 1 ); + MESSAGE_END(); + + // killers score, if it's a player + CBaseEntity *ep = CBaseEntity::Instance( pKiller ); + if ( ep && ep->Classify() == CLASS_PLAYER ) + { + CBasePlayer *PK = (CBasePlayer*)ep; + + MESSAGE_BEGIN( MSG_ALL, gmsgScoreInfo ); + WRITE_BYTE( ENTINDEX(PK->edict()) ); + WRITE_SHORT( PK->pev->frags ); + WRITE_SHORT( PK->m_iDeaths ); + WRITE_SHORT( 0 ); + WRITE_SHORT( GetTeamIndex( PK->m_szTeamName) + 1 ); + MESSAGE_END(); + + // let the killer paint another decal as soon as he'd like. + PK->m_flNextDecalTime = gpGlobals->time; + } +} + +//========================================================= +// Deathnotice. +//========================================================= +void CHalfLifeMultiplay::DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pevInflictor ) +{ + // Work out what killed the player, and send a message to all clients about it + CBaseEntity *Killer = CBaseEntity::Instance( pKiller ); + + const char *killer_weapon_name = "world"; // by default, the player is killed by the world + int killer_index = 0; + + // Hack to fix name change + char *tau = "tau_cannon"; + char *gluon = "gluon gun"; + + if ( pKiller->flags & FL_CLIENT ) + { + killer_index = ENTINDEX(ENT(pKiller)); + + if ( pevInflictor ) + { + if ( pevInflictor == pKiller ) + { + // If the inflictor is the killer, then it must be their current weapon doing the damage + CBasePlayer *pPlayer = (CBasePlayer*)CBaseEntity::Instance( pKiller ); + + if ( pPlayer->m_pActiveItem ) + { + killer_weapon_name = pPlayer->m_pActiveItem->pszName(); + } + } + else + { + killer_weapon_name = STRING( pevInflictor->classname ); // it's just that easy + } + } + } + else + { + killer_weapon_name = STRING( pevInflictor->classname ); + } + + // strip the monster_* or weapon_* from the inflictor's classname + if ( strncmp( killer_weapon_name, "weapon_", 7 ) == 0 ) + killer_weapon_name += 7; + else if ( strncmp( killer_weapon_name, "monster_", 8 ) == 0 ) + killer_weapon_name += 8; + else if ( strncmp( killer_weapon_name, "func_", 5 ) == 0 ) + killer_weapon_name += 5; + + MESSAGE_BEGIN( MSG_ALL, gmsgDeathMsg ); + WRITE_BYTE( killer_index ); // the killer + WRITE_BYTE( ENTINDEX(pVictim->edict()) ); // the victim + WRITE_STRING( killer_weapon_name ); // what they were killed by (should this be a string?) + MESSAGE_END(); + + // replace the code names with the 'real' names + if ( !strcmp( killer_weapon_name, "egon" ) ) + killer_weapon_name = gluon; + else if ( !strcmp( killer_weapon_name, "gauss" ) ) + killer_weapon_name = tau; + + if ( pVictim->pev == pKiller ) + { + // killed self + } + else if ( pKiller->flags & FL_CLIENT ) + { + } + else + { + // killed by the world + } + + // Print a standard message + // TODO: make this go direct to console + return; // just remove for now +/* + char szText[ 128 ]; + + if ( pKiller->flags & FL_MONSTER ) + { + // killed by a monster + strcpy ( szText, STRING( pVictim->pev->netname ) ); + strcat ( szText, " was killed by a monster.\n" ); + return; + } + + if ( pKiller == pVictim->pev ) + { + strcpy ( szText, STRING( pVictim->pev->netname ) ); + strcat ( szText, " commited suicide.\n" ); + } + else if ( pKiller->flags & FL_CLIENT ) + { + strcpy ( szText, STRING( pKiller->netname ) ); + + strcat( szText, " : " ); + strcat( szText, killer_weapon_name ); + strcat( szText, " : " ); + + strcat ( szText, STRING( pVictim->pev->netname ) ); + strcat ( szText, "\n" ); + } + else if ( FClassnameIs ( pKiller, "worldspawn" ) ) + { + strcpy ( szText, STRING( pVictim->pev->netname ) ); + strcat ( szText, " fell or drowned or something.\n" ); + } + else if ( pKiller->solid == SOLID_BSP ) + { + strcpy ( szText, STRING( pVictim->pev->netname ) ); + strcat ( szText, " was mooshed.\n" ); + } + else + { + strcpy ( szText, STRING( pVictim->pev->netname ) ); + strcat ( szText, " died mysteriously.\n" ); + } + + UTIL_ClientPrintAll( szText ); +*/ +} + +//========================================================= +// PlayerGotWeapon - player has grabbed a weapon that was +// sitting in the world +//========================================================= +void CHalfLifeMultiplay :: PlayerGotWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ) +{ +} + +//========================================================= +// FlWeaponRespawnTime - what is the time in the future +// at which this weapon may spawn? +//========================================================= +float CHalfLifeMultiplay :: FlWeaponRespawnTime( CBasePlayerWeapon *pWeapon ) +{ + if( CVAR_GET_FLOAT( "mp_weaponstay" ) > 0 ) + { + // make sure it's only certain weapons + if ( !(pWeapon->iFlags() & ITEM_FLAG_LIMITINWORLD) ) + { + return gpGlobals->time + 0; // weapon respawns almost instantly + } + } + + return gpGlobals->time + WEAPON_RESPAWN_TIME; +} + +// when we are within this close to running out of entities, items +// marked with the ITEM_FLAG_LIMITINWORLD will delay their respawn +#define ENTITY_INTOLERANCE 100 + +//========================================================= +// FlWeaponRespawnTime - Returns 0 if the weapon can respawn +// now, otherwise it returns the time at which it can try +// to spawn again. +//========================================================= +float CHalfLifeMultiplay :: FlWeaponTryRespawn( CBasePlayerWeapon *pWeapon ) +{ + if ( pWeapon && pWeapon->m_iId && (pWeapon->iFlags() & ITEM_FLAG_LIMITINWORLD) ) + { + if ( gpGlobals->numEntities < (gpGlobals->maxEntities - ENTITY_INTOLERANCE) ) + return 0; + + // we're past the entity tolerance level, so delay the respawn + return FlWeaponRespawnTime( pWeapon ); + } + + return 0; +} + +//========================================================= +// VecWeaponRespawnSpot - where should this weapon spawn? +// Some game variations may choose to randomize spawn locations +//========================================================= +Vector CHalfLifeMultiplay :: VecWeaponRespawnSpot( CBasePlayerWeapon *pWeapon ) +{ + return pWeapon->pev->origin; +} + +//========================================================= +// WeaponShouldRespawn - any conditions inhibiting the +// respawning of this weapon? +//========================================================= +int CHalfLifeMultiplay :: WeaponShouldRespawn( CBasePlayerWeapon *pWeapon ) +{ + if ( pWeapon->pev->spawnflags & SF_NORESPAWN ) + { + return GR_WEAPON_RESPAWN_NO; + } + + return GR_WEAPON_RESPAWN_YES; +} + +//========================================================= +// CanHaveWeapon - returns FALSE if the player is not allowed +// to pick up this weapon +//========================================================= +BOOL CHalfLifeMultiplay::CanHavePlayerItem( CBasePlayer *pPlayer, CBasePlayerWeapon *pItem ) +{ + if( CVAR_GET_FLOAT( "mp_weaponstay" ) > 0 ) + { + if ( pItem->iFlags() & ITEM_FLAG_LIMITINWORLD ) + return CGameRules::CanHavePlayerItem( pPlayer, pItem ); + + // check if the player already has this weapon + for ( int i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + CBasePlayerWeapon *it = pPlayer->m_rgpPlayerItems[i]; + + while ( it != NULL ) + { + if ( it->m_iId == pItem->m_iId ) + { + return FALSE; + } + + it = it->m_pNext; + } + } + } + + return CGameRules::CanHavePlayerItem( pPlayer, pItem ); +} + +//========================================================= +//========================================================= +float CHalfLifeMultiplay::FlHealthChargerRechargeTime( void ) +{ + return 60; +} + + +float CHalfLifeMultiplay::FlHEVChargerRechargeTime( void ) +{ + return 30; +} + +//========================================================= +BOOL CHalfLifeMultiplay::IsAllowedToSpawn( CBaseEntity *pEntity ) +{ + return TRUE; +} + +//========================================================= + +//========================================================= +//========================================================= +int CHalfLifeMultiplay::DeadPlayerWeapons( CBasePlayer *pPlayer ) +{ + return GR_PLR_DROP_GUN_ACTIVE; +} + +//========================================================= +//========================================================= +int CHalfLifeMultiplay::DeadPlayerAmmo( CBasePlayer *pPlayer ) +{ + return GR_PLR_DROP_AMMO_ACTIVE; +} + +edict_t *CHalfLifeMultiplay::GetPlayerSpawnSpot( CBasePlayer *pPlayer ) +{ + edict_t *pentSpawnSpot = CGameRules::GetPlayerSpawnSpot( pPlayer ); + if ( IsMultiplayer()) + { + UTIL_FireTargets( pentSpawnSpot->v.target, pPlayer, pPlayer, USE_TOGGLE ); + } + + return pentSpawnSpot; +} + + +//========================================================= +//========================================================= +int CHalfLifeMultiplay::PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ) +{ + // half life deathmatch has only enemies + return GR_NOTTEAMMATE; +} + +BOOL CHalfLifeMultiplay :: PlayFootstepSounds( CBasePlayer *pl, float fvol ) +{ + if( CVAR_GET_FLOAT( "mp_footsteps" ) == 0 ) + return FALSE; + + if ( pl->IsOnLadder() || pl->pev->velocity.Length2D() > 220 ) + return TRUE; // only make step sounds in multiplayer if the player is moving fast enough + + return FALSE; +} + +BOOL CHalfLifeMultiplay :: FAllowFlashlight( void ) +{ + return CVAR_GET_FLOAT( "mp_flashlight" ) ? TRUE : FALSE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeMultiplay :: FAllowMonsters( void ) +{ + return 0; +} + +//========================================================= +//======== CHalfLifeMultiplay private functions =========== +#define INTERMISSION_TIME 6 + +void CHalfLifeMultiplay :: GoToIntermission( void ) +{ + if ( g_fGameOver ) + return; // intermission has already been triggered, so ignore. + + MESSAGE_BEGIN(MSG_ALL, SVC_INTERMISSION); + MESSAGE_END(); + + // bounds check + int time = (int)CVAR_GET_FLOAT( "mp_chattime" ); + if ( time < 1 ) + CVAR_SET_STRING( "mp_chattime", "1" ); + else if ( time > MAX_INTERMISSION_TIME ) + CVAR_SET_STRING( "mp_chattime", UTIL_dtos1( MAX_INTERMISSION_TIME ) ); + + m_flIntermissionEndTime = gpGlobals->time + ((int)CVAR_GET_FLOAT( "mp_chattime" )); + g_flIntermissionStartTime = gpGlobals->time; + + g_fGameOver = TRUE; + m_iEndIntermissionButtonHit = FALSE; +} + +#define MAX_RULE_BUFFER 1024 + +typedef struct mapcycle_item_s +{ + struct mapcycle_item_s *next; + + char mapname[ 32 ]; + int minplayers, maxplayers; + char rulebuffer[ MAX_RULE_BUFFER ]; +} mapcycle_item_t; + +typedef struct mapcycle_s +{ + struct mapcycle_item_s *items; + struct mapcycle_item_s *next_item; +} mapcycle_t; + +/* +============== +DestroyMapCycle + +Clean up memory used by mapcycle when switching it +============== +*/ +void DestroyMapCycle( mapcycle_t *cycle ) +{ + mapcycle_item_t *p, *n, *start; + p = cycle->items; + if ( p ) + { + start = p; + p = p->next; + while ( p != start ) + { + n = p->next; + delete p; + p = n; + } + + delete cycle->items; + } + cycle->items = NULL; + cycle->next_item = NULL; +} + +static char com_token[ 1500 ]; + +/* +============== +COM_TokenWaiting + +Returns 1 if additional data is waiting to be processed on this line +============== +*/ +int COM_TokenWaiting( char *buffer ) +{ + char *p; + + p = buffer; + while ( *p && *p!='\n') + { + if ( !isspace( *p ) || isalnum( *p ) ) + return 1; + + p++; + } + + return 0; +} + + + +/* +============== +ReloadMapCycleFile + + +Parses mapcycle.txt file into mapcycle_t structure +============== +*/ +int ReloadMapCycleFile( char *filename, mapcycle_t *cycle ) +{ + char szBuffer[ MAX_RULE_BUFFER ]; + char szMap[ 32 ]; + int length; + char *pFileList; + char *aFileList = pFileList = (char*)LOAD_FILE( filename, &length ); + int hasbuffer; + mapcycle_item_s *item, *newlist = NULL, *next; + + if ( pFileList && length ) + { + // the first map name in the file becomes the default + while ( 1 ) + { + hasbuffer = 0; + memset( szBuffer, 0, MAX_RULE_BUFFER ); + + pFileList = COM_Parse( pFileList ); + if ( strlen( com_token ) <= 0 ) + break; + + strcpy( szMap, com_token ); + + // Any more tokens on this line? + if ( COM_TokenWaiting( pFileList ) ) + { + pFileList = COM_Parse( pFileList ); + if ( strlen( com_token ) > 0 ) + { + hasbuffer = 1; + strcpy( szBuffer, com_token ); + } + } + + // Check map + if ( IS_MAP_VALID( szMap ) ) + { + // Create entry + char *s; + + item = new mapcycle_item_s; + + strcpy( item->mapname, szMap ); + + item->minplayers = 0; + item->maxplayers = 0; + + memset( item->rulebuffer, 0, MAX_RULE_BUFFER ); + + if ( hasbuffer ) + { + s = g_engfuncs.pfnInfoKeyValue( szBuffer, "minplayers" ); + if ( s && s[0] ) + { + item->minplayers = atoi( s ); + item->minplayers = max( item->minplayers, 0 ); + item->minplayers = min( item->minplayers, gpGlobals->maxClients ); + } + s = g_engfuncs.pfnInfoKeyValue( szBuffer, "maxplayers" ); + if ( s && s[0] ) + { + item->maxplayers = atoi( s ); + item->maxplayers = max( item->maxplayers, 0 ); + item->maxplayers = min( item->maxplayers, gpGlobals->maxClients ); + } + + // Remove keys + // + g_engfuncs.pfnInfo_RemoveKey( szBuffer, "minplayers" ); + g_engfuncs.pfnInfo_RemoveKey( szBuffer, "maxplayers" ); + + strcpy( item->rulebuffer, szBuffer ); + } + + item->next = cycle->items; + cycle->items = item; + } + else + { + ALERT( at_console, "Skipping %s from mapcycle, not a valid map\n", szMap ); + } + + } + + FREE_FILE( aFileList ); + } + + // Fixup circular list pointer + item = cycle->items; + + // Reverse it to get original order + while ( item ) + { + next = item->next; + item->next = newlist; + newlist = item; + item = next; + } + cycle->items = newlist; + item = cycle->items; + + // Didn't parse anything + if ( !item ) + { + return 0; + } + + while ( item->next ) + { + item = item->next; + } + item->next = cycle->items; + + cycle->next_item = item->next; + + return 1; +} + +/* +============== +CountPlayers + +Determine the current # of active players on the server for map cycling logic +============== +*/ +int CountPlayers( void ) +{ + int num = 0; + + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *pEnt = UTIL_PlayerByIndex( i ); + + if ( pEnt ) + { + num = num + 1; + } + } + + return num; +} + +/* +============== +ExtractCommandString + +Parse commands/key value pairs to issue right after map xxx command is issued on server + level transition +============== +*/ +void ExtractCommandString( char *s, char *szCommand ) +{ + // Now make rules happen + char pkey[512]; + char value[512]; // use two buffers so compares + // work without stomping on each other + char *o; + + if ( *s == '\\' ) + s++; + + while (1) + { + o = pkey; + while ( *s != '\\' ) + { + if ( !*s ) + return; + *o++ = *s++; + } + *o = 0; + s++; + + o = value; + + while (*s != '\\' && *s) + { + if (!*s) + return; + *o++ = *s++; + } + *o = 0; + + strcat( szCommand, pkey ); + if ( strlen( value ) > 0 ) + { + strcat( szCommand, " " ); + strcat( szCommand, value ); + } + strcat( szCommand, "\n" ); + + if (!*s) + return; + s++; + } +} + +/* +============== +ChangeLevel + +Server is changing to a new level, check mapcycle.txt for map name and setup info +============== +*/ +void CHalfLifeMultiplay :: ChangeLevel( void ) +{ + static char szPreviousMapCycleFile[ 256 ]; + static mapcycle_t mapcycle; + + char szNextMap[32]; + char szFirstMapInList[32]; + char szCommands[ 1500 ]; + char szRules[ 1500 ]; + int minplayers = 0, maxplayers = 0; + strcpy( szFirstMapInList, "hldm1" ); // the absolute default level is hldm1 + + int curplayers; + BOOL do_cycle = TRUE; + + // find the map to change to + char *mapcfile = (char*)CVAR_GET_STRING( "mapcyclefile" ); + ASSERT( mapcfile != NULL ); + + szCommands[ 0 ] = '\0'; + szRules[ 0 ] = '\0'; + + curplayers = CountPlayers(); + + // Has the map cycle filename changed? + if ( stricmp( mapcfile, szPreviousMapCycleFile ) ) + { + strcpy( szPreviousMapCycleFile, mapcfile ); + + DestroyMapCycle( &mapcycle ); + + if ( !ReloadMapCycleFile( mapcfile, &mapcycle ) || ( !mapcycle.items ) ) + { + ALERT( at_console, "Unable to load map cycle file %s\n", mapcfile ); + do_cycle = FALSE; + } + } + + if ( do_cycle && mapcycle.items ) + { + BOOL keeplooking = FALSE; + BOOL found = FALSE; + mapcycle_item_s *item; + + // Assume current map + strcpy( szNextMap, STRING(gpGlobals->mapname) ); + strcpy( szFirstMapInList, STRING(gpGlobals->mapname) ); + + // Traverse list + for ( item = mapcycle.next_item; item->next != mapcycle.next_item; item = item->next ) + { + keeplooking = FALSE; + + ASSERT( item != NULL ); + + if ( item->minplayers != 0 ) + { + if ( curplayers >= item->minplayers ) + { + found = TRUE; + minplayers = item->minplayers; + } + else + { + keeplooking = TRUE; + } + } + + if ( item->maxplayers != 0 ) + { + if ( curplayers <= item->maxplayers ) + { + found = TRUE; + maxplayers = item->maxplayers; + } + else + { + keeplooking = TRUE; + } + } + + if ( keeplooking ) + continue; + + found = TRUE; + break; + } + + if ( !found ) + { + item = mapcycle.next_item; + } + + // Increment next item pointer + mapcycle.next_item = item->next; + + // Perform logic on current item + strcpy( szNextMap, item->mapname ); + + ExtractCommandString( item->rulebuffer, szCommands ); + strcpy( szRules, item->rulebuffer ); + } + + if ( !IS_MAP_VALID(szNextMap) ) + { + strcpy( szNextMap, szFirstMapInList ); + } + + g_fGameOver = TRUE; + + ALERT( at_console, "CHANGE LEVEL: %s\n", szNextMap ); + if ( minplayers || maxplayers ) + { + ALERT( at_console, "PLAYER COUNT: min %i max %i current %i\n", minplayers, maxplayers, curplayers ); + } + if ( strlen( szRules ) > 0 ) + { + ALERT( at_console, "RULES: %s\n", szRules ); + } + + CHANGE_LEVEL( szNextMap, NULL ); + if ( strlen( szCommands ) > 0 ) + { + SERVER_COMMAND( szCommands ); + } +} + +#define MAX_MOTD_CHUNK 60 +#define MAX_MOTD_LENGTH 1536 // (MAX_MOTD_CHUNK * 4) + +void CHalfLifeMultiplay :: SendMOTDToClient( edict_t *client ) +{ + // read from the MOTD.txt file + int length, char_count = 0; + char *pFileList; + char *aFileList = pFileList = (char*)LOAD_FILE ((char *)CVAR_GET_STRING( "motdfile" ), &length ); + + // send the server name + MESSAGE_BEGIN( MSG_ONE, gmsgServerName, NULL, client ); + WRITE_STRING( CVAR_GET_STRING("hostname") ); + MESSAGE_END(); + + // Send the message of the day + // read it chunk-by-chunk, and send it in parts + + while ( pFileList && *pFileList && char_count < MAX_MOTD_LENGTH ) + { + char chunk[MAX_MOTD_CHUNK+1]; + + if ( strlen( pFileList ) < MAX_MOTD_CHUNK ) + { + strcpy( chunk, pFileList ); + } + else + { + strncpy( chunk, pFileList, MAX_MOTD_CHUNK ); + chunk[MAX_MOTD_CHUNK] = 0; // strncpy doesn't always append the null terminator + } + + char_count += strlen( chunk ); + if ( char_count < MAX_MOTD_LENGTH ) + pFileList = aFileList + char_count; + else + *pFileList = 0; + + MESSAGE_BEGIN( MSG_ONE, gmsgMOTD, NULL, client ); + WRITE_BYTE( *pFileList ? FALSE : TRUE ); // FALSE means there is still more message to come + WRITE_STRING( chunk ); + MESSAGE_END(); + } + + FREE_FILE( aFileList ); +} + + diff --git a/server/game/singleplay_gamerules.cpp b/server/game/singleplay_gamerules.cpp new file mode 100644 index 00000000..3214ff72 --- /dev/null +++ b/server/game/singleplay_gamerules.cpp @@ -0,0 +1,264 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// teamplay_gamerules.cpp +// +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" +#include "baseweapon.h" +#include "gamerules.h" + +extern DLL_GLOBAL CGameRules *g_pGameRules; +extern DLL_GLOBAL BOOL g_fGameOver; +extern int gmsgDeathMsg; // client dll messages +extern int gmsgScoreInfo; +extern int gmsgMOTD; + +//========================================================= +//========================================================= +CHalfLifeRules::CHalfLifeRules( void ) +{ + RefreshSkillData(); +} + +//========================================================= +//========================================================= +void CHalfLifeRules::Think ( void ) +{ +} + +//========================================================= +//========================================================= +BOOL CHalfLifeRules::IsMultiplayer( void ) +{ + return FALSE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeRules::IsDeathmatch ( void ) +{ + return FALSE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeRules::IsCoOp( void ) +{ + return FALSE; +} + + +//========================================================= +//========================================================= +BOOL CHalfLifeRules::FShouldSwitchWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ) +{ + if ( !pPlayer->m_pActiveItem ) + { + // player doesn't have an active item! + return TRUE; + } + + if ( !pPlayer->m_pActiveItem->CanHolster() ) + { + return FALSE; + } + + return TRUE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeRules :: GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pCurrentWeapon ) +{ + return FALSE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeRules :: ClientConnected( edict_t *pEntity, const char *userinfo ) +{ + return TRUE; +} + +void CHalfLifeRules :: InitHUD( CBasePlayer *pl ) +{ +} + +//========================================================= +//========================================================= +void CHalfLifeRules :: ClientDisconnected( edict_t *pClient ) +{ +} + +//========================================================= +//========================================================= +float CHalfLifeRules::FlPlayerFallDamage( CBasePlayer *pPlayer ) +{ + // subtract off the speed at which a player is allowed to fall without being hurt, + // so damage will be based on speed beyond that, not the entire fall + pPlayer->m_flFallVelocity -= PLAYER_MAX_SAFE_FALL_SPEED; + return pPlayer->m_flFallVelocity * DAMAGE_FOR_FALL_SPEED; +} + +//========================================================= +//========================================================= +void CHalfLifeRules :: PlayerSpawn( CBasePlayer *pPlayer ) +{ + //if (g_startSuit) + pPlayer->m_iHideHUD |= ITEM_SUIT; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeRules :: AllowAutoTargetCrosshair( void ) +{ + return TRUE; +} + +//========================================================= +//========================================================= +void CHalfLifeRules :: PlayerThink( CBasePlayer *pPlayer ) +{ +} + + +//========================================================= +//========================================================= +BOOL CHalfLifeRules :: FPlayerCanRespawn( CBasePlayer *pPlayer ) +{ + return TRUE; +} + +//========================================================= +//========================================================= +float CHalfLifeRules :: FlPlayerSpawnTime( CBasePlayer *pPlayer ) +{ + return gpGlobals->time;//now! +} + +//========================================================= +// IPointsForKill - how many points awarded to anyone +// that kills this player? +//========================================================= +int CHalfLifeRules :: IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ) +{ + return 1; +} + +//========================================================= +// PlayerKilled - someone/something killed this player +//========================================================= +void CHalfLifeRules :: PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ) +{ +} + +//========================================================= +// Deathnotice +//========================================================= +void CHalfLifeRules::DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ) +{ +} + +//========================================================= +// PlayerGotWeapon - player has grabbed a weapon that was +// sitting in the world +//========================================================= +void CHalfLifeRules :: PlayerGotWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pWeapon ) +{ +} + +//========================================================= +// FlWeaponRespawnTime - what is the time in the future +// at which this weapon may spawn? +//========================================================= +float CHalfLifeRules :: FlWeaponRespawnTime( CBasePlayerWeapon *pWeapon ) +{ + return -1; +} + +//========================================================= +// FlWeaponRespawnTime - Returns 0 if the weapon can respawn +// now, otherwise it returns the time at which it can try +// to spawn again. +//========================================================= +float CHalfLifeRules :: FlWeaponTryRespawn( CBasePlayerWeapon *pWeapon ) +{ + return 0; +} + +//========================================================= +// VecWeaponRespawnSpot - where should this weapon spawn? +// Some game variations may choose to randomize spawn locations +//========================================================= +Vector CHalfLifeRules :: VecWeaponRespawnSpot( CBasePlayerWeapon *pWeapon ) +{ + return pWeapon->pev->origin; +} + +//========================================================= +BOOL CHalfLifeRules::IsAllowedToSpawn( CBaseEntity *pEntity ) +{ + return TRUE; +} + +//========================================================= + +//========================================================= +// WeaponShouldRespawn - any conditions inhibiting the +// respawning of this weapon? +//========================================================= +int CHalfLifeRules :: WeaponShouldRespawn( CBasePlayerWeapon *pWeapon ) +{ + return GR_WEAPON_RESPAWN_NO; +} + +//========================================================= +//========================================================= +float CHalfLifeRules::FlHealthChargerRechargeTime( void ) +{ + return 0;// don't recharge +} + +//========================================================= +//========================================================= +int CHalfLifeRules::DeadPlayerWeapons( CBasePlayer *pPlayer ) +{ + return GR_PLR_DROP_GUN_NO; +} + +//========================================================= +//========================================================= +int CHalfLifeRules::DeadPlayerAmmo( CBasePlayer *pPlayer ) +{ + return GR_PLR_DROP_AMMO_NO; +} + +//========================================================= +//========================================================= +int CHalfLifeRules::PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ) +{ + // why would a single player in half life need this? + return GR_NOTTEAMMATE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeRules :: FAllowMonsters( void ) +{ + return TRUE; +} diff --git a/server/game/sound.cpp b/server/game/sound.cpp new file mode 100644 index 00000000..2f18b5df --- /dev/null +++ b/server/game/sound.cpp @@ -0,0 +1,1948 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +//========================================================= +// sound.cpp +//========================================================= + +#include + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "baseweapon.h" +#include "player.h" +#include "talkmonster.h" +#include "gamerules.h" +#include "client.h" + +static char *memfgets( byte *pMemFile, int fileSize, int &filePos, char *pBuffer, int bufferSize ); + + +// ==================== GENERIC AMBIENT SOUND ====================================== + +// runtime pitch shift and volume fadein/out structure + +// NOTE: IF YOU CHANGE THIS STRUCT YOU MUST CHANGE THE SAVE/RESTORE VERSION NUMBER +// SEE BELOW (in the typedescription for the class) +typedef struct dynpitchvol +{ + // NOTE: do not change the order of these parameters + // NOTE: unless you also change order of rgdpvpreset array elements! + int preset; + + int pitchrun; // pitch shift % when sound is running 0 - 255 + int pitchstart; // pitch shift % when sound stops or starts 0 - 255 + int spinup; // spinup time 0 - 100 + int spindown; // spindown time 0 - 100 + + int volrun; // volume change % when sound is running 0 - 10 + int volstart; // volume change % when sound stops or starts 0 - 10 + int fadein; // volume fade in time 0 - 100 + int fadeout; // volume fade out time 0 - 100 + + // Low Frequency Oscillator + int lfotype; // 0) off 1) square 2) triangle 3) random + int lforate; // 0 - 1000, how fast lfo osciallates + + int lfomodpitch; // 0-100 mod of current pitch. 0 is off. + int lfomodvol; // 0-100 mod of current volume. 0 is off. + + int cspinup; // each trigger hit increments counter and spinup pitch + + + int cspincount; + + int pitch; + int spinupsav; + int spindownsav; + int pitchfrac; + + int vol; + int fadeinsav; + int fadeoutsav; + int volfrac; + + int lfofrac; + int lfomult; + + +} dynpitchvol_t; + +#define CDPVPRESETMAX 27 + +// presets for runtime pitch and vol modulation of ambient sounds + +dynpitchvol_t rgdpvpreset[CDPVPRESETMAX] = +{ +//pitch pstart spinup spindwn volrun volstrt fadein fadeout lfotype lforate modptch modvol cspnup +{1, 255, 75, 95, 95, 10, 1, 50, 95, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{2, 255, 85, 70, 88, 10, 1, 20, 88, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{3, 255, 100, 50, 75, 10, 1, 10, 75, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{4, 100, 100, 0, 0, 10, 1, 90, 90, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{5, 100, 100, 0, 0, 10, 1, 80, 80, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{6, 100, 100, 0, 0, 10, 1, 50, 70, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{7, 100, 100, 0, 0, 5, 1, 40, 50, 1, 50, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0}, +{8, 100, 100, 0, 0, 5, 1, 40, 50, 1, 150, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0}, +{9, 100, 100, 0, 0, 5, 1, 40, 50, 1, 750, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0}, +{10, 128, 100, 50, 75, 10, 1, 30, 40, 2, 8, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{11, 128, 100, 50, 75, 10, 1, 30, 40, 2, 25, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{12, 128, 100, 50, 75, 10, 1, 30, 40, 2, 70, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{13, 50, 50, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{14, 70, 70, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{15, 90, 90, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{16, 120, 120, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{17, 180, 180, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{18, 255, 255, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{19, 200, 75, 90, 90, 10, 1, 50, 90, 2, 100, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{20, 255, 75, 97, 90, 10, 1, 50, 90, 1, 40, 50, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{21, 100, 100, 0, 0, 10, 1, 30, 50, 3, 15, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{22, 160, 160, 0, 0, 10, 1, 50, 50, 3, 500, 25, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{23, 255, 75, 88, 0, 10, 1, 40, 0, 0, 0, 0, 0, 5, 0,0,0,0,0,0,0,0,0,0}, +{24, 200, 20, 95, 70, 10, 1, 70, 70, 3, 20, 50, 0, 0, 0,0,0,0,0,0,0,0,0,0}, +{25, 180, 100, 50, 60, 10, 1, 40, 60, 2, 90, 100, 100, 0, 0,0,0,0,0,0,0,0,0,0}, +{26, 60, 60, 0, 0, 10, 1, 40, 70, 3, 80, 20, 50, 0, 0,0,0,0,0,0,0,0,0,0}, +{27, 128, 90, 10, 10, 10, 1, 20, 40, 1, 5, 10, 20, 0, 0,0,0,0,0,0,0,0,0,0} +}; + +class CAmbientGeneric : public CBaseEntity +{ +public: + void KeyValue( KeyValueData* pkvd); + void Spawn( void ); +// void PostSpawn( void ); + void Precache( void ); + void EXPORT ToggleUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT StartPlayFrom( void ); + void EXPORT RampThink( void ); + void InitModulationParms(void); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + virtual int ObjectCaps( void ) { return (CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); } + + float m_flAttenuation; // attenuation value + dynpitchvol_t m_dpv; + + BOOL m_fActive; // only TRUE when the entity is playing a looping sound + BOOL m_fLooping; // TRUE when the sound played will loop + edict_t *m_pPlayFrom; //LRC - the entity to play from + int m_iChannel; //LRC - the channel to play from, for "play from X" sounds +}; + +LINK_ENTITY_TO_CLASS( ambient_generic, CAmbientGeneric ); +TYPEDESCRIPTION CAmbientGeneric::m_SaveData[] = +{ + DEFINE_FIELD( CAmbientGeneric, m_flAttenuation, FIELD_FLOAT ), + DEFINE_FIELD( CAmbientGeneric, m_fActive, FIELD_BOOLEAN ), + DEFINE_FIELD( CAmbientGeneric, m_fLooping, FIELD_BOOLEAN ), + DEFINE_FIELD( CAmbientGeneric, m_iChannel, FIELD_INTEGER ), //LRC + DEFINE_FIELD( CAmbientGeneric, m_pPlayFrom, FIELD_EDICT ), //LRC + + // HACKHACK - This is not really in the spirit of the save/restore design, but save this + // out as a binary data block. If the dynpitchvol_t is changed, old saved games will NOT + // load these correctly, so bump the save/restore version if you change the size of the struct + // The right way to do this is to split the input parms (read in keyvalue) into members and re-init this + // struct in Precache(), but it's unlikely that the struct will change, so it's not worth the time right now. + DEFINE_ARRAY( CAmbientGeneric, m_dpv, FIELD_CHARACTER, sizeof(dynpitchvol_t) ), +}; + +IMPLEMENT_SAVERESTORE( CAmbientGeneric, CBaseEntity ); + +// +// ambient_generic - general-purpose user-defined static sound +// +void CAmbientGeneric :: Spawn( void ) +{ +/* + -1 : "Default" + 0 : "Everywhere" + 200 : "Small Radius" + 125 : "Medium Radius" + 80 : "Large Radius" +*/ + + if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_EVERYWHERE) ) + { + m_flAttenuation = ATTN_NONE; + } + else if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_SMALLRADIUS) ) + { + m_flAttenuation = ATTN_IDLE; + } + else if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_MEDIUMRADIUS) ) + { + m_flAttenuation = ATTN_STATIC; + } + else if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_LARGERADIUS) ) + { + m_flAttenuation = ATTN_NORM; + } + else + {// if the designer didn't set a sound attenuation, default to one. + m_flAttenuation = ATTN_STATIC; + } + + char* szSoundFile = (char*) STRING(pev->message); + + if ( FStringNull( pev->message ) || strlen( szSoundFile ) < 1 ) + { + ALERT( at_error, "ambient_generic \"%s\" at (%f, %f, %f) has no sound file\n", + STRING(pev->targetname), pev->origin.x, pev->origin.y, pev->origin.z ); + SetNextThink( 0.1 ); + SetThink( Remove ); + return; + } + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + + // Set up think function for dynamic modification + // of ambient sound's pitch or volume. Don't + // start thinking yet. + + SetThink(RampThink); + DontThink(); + + // allow on/off switching via 'use' function. + + SetUse( ToggleUse ); + + m_fActive = FALSE; + + if ( FBitSet ( pev->spawnflags, AMBIENT_SOUND_NOT_LOOPING ) ) + m_fLooping = FALSE; + else + m_fLooping = TRUE; + Precache( ); +} + +// this function needs to be called when the game is loaded, not just when the entity spawns. +// Don't make this a PostSpawn function. +void CAmbientGeneric :: Precache( void ) +{ + char* szSoundFile = (char*) STRING(pev->message); + + if ( !FStringNull( pev->message ) && strlen( szSoundFile ) > 1 ) + { + if (*szSoundFile != '!') + PRECACHE_SOUND(szSoundFile); + } + // init all dynamic modulation parms + InitModulationParms(); + + if ( !FBitSet (pev->spawnflags, AMBIENT_SOUND_START_SILENT ) ) + { + // start the sound ASAP + if (m_fLooping) + m_fActive = TRUE; + } + + if (pev->target) + { + CBaseEntity *pTarget = UTIL_FindEntityByTargetname( NULL, STRING(pev->target)); + if (!pTarget) + { + ALERT(at_debug, "WARNING: ambient_generic \"%s\" can't find \"%s\", its entity to play from.\n", + STRING(pev->targetname), STRING(pev->target)); + } + else + m_pPlayFrom = ENT(pTarget->pev); + } + + if ( m_fActive ) + { + if (m_pPlayFrom) + { + SetThink(&CAmbientGeneric ::StartPlayFrom); //LRC +// EMIT_SOUND_DYN( m_pPlayFrom, m_iChannel, szSoundFile, //LRC +// (m_dpv.vol * 0.01), m_flAttenuation, SND_SPAWNING, m_dpv.pitch); + +// ALERT(at_console, "AMBGEN: spawn start\n"); + } + else + { + UTIL_EmitAmbientSound ( ENT(pev), pev->origin, szSoundFile, + (m_dpv.vol * 0.01), m_flAttenuation, SND_SPAWNING, m_dpv.pitch); + } + SetNextThink( 0.1 ); + } +} + +//LRC - for some reason, I can't get other entities to start playing sounds during Activate; +// this function is used to delay the effect until the first Think, which seems to fix the problem. +void CAmbientGeneric :: StartPlayFrom( void ) +{ + char* szSoundFile = (char*) STRING(pev->message); + + EMIT_SOUND_DYN( m_pPlayFrom, m_iChannel, szSoundFile, //LRC + (m_dpv.vol * 0.01), m_flAttenuation, SND_SPAWNING, m_dpv.pitch); + + SetThink(&CAmbientGeneric ::RampThink); + SetNextThink( 0.1 ); +} + +// RampThink - Think at 5hz if we are dynamically modifying +// pitch or volume of the playing sound. This function will +// ramp pitch and/or volume up or down, modify pitch/volume +// with lfo if active. + +void CAmbientGeneric :: RampThink( void ) +{ + char* szSoundFile = (char*) STRING(pev->message); + int pitch = m_dpv.pitch; + int vol = m_dpv.vol; + int flags = 0; + int fChanged = 0; // FALSE if pitch and vol remain unchanged this round + int prev; + + if (!m_dpv.spinup && !m_dpv.spindown && !m_dpv.fadein && !m_dpv.fadeout && !m_dpv.lfotype) + return; // no ramps or lfo, stop thinking + + // ============== + // pitch envelope + // ============== + if (m_dpv.spinup || m_dpv.spindown) + { + prev = m_dpv.pitchfrac >> 8; + + if (m_dpv.spinup > 0) + m_dpv.pitchfrac += m_dpv.spinup; + else if (m_dpv.spindown > 0) + m_dpv.pitchfrac -= m_dpv.spindown; + + pitch = m_dpv.pitchfrac >> 8; + + if (pitch > m_dpv.pitchrun) + { + pitch = m_dpv.pitchrun; + m_dpv.spinup = 0; // done with ramp up + } + + if (pitch < m_dpv.pitchstart) + { + pitch = m_dpv.pitchstart; + m_dpv.spindown = 0; // done with ramp down + + // shut sound off + if (m_pPlayFrom) + { + STOP_SOUND( m_pPlayFrom, m_iChannel, szSoundFile); //LRC + } + else + { + UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile, + 0, 0, SND_STOP, 0); + } + + // return without setting nextthink + return; + } + + if (pitch > 255) pitch = 255; + if (pitch < 1) pitch = 1; + + m_dpv.pitch = pitch; + + fChanged |= (prev != pitch); + flags |= SND_CHANGE_PITCH; + } + + // ================== + // amplitude envelope + // ================== + if (m_dpv.fadein || m_dpv.fadeout) + { + prev = m_dpv.volfrac >> 8; + + if (m_dpv.fadein > 0) + m_dpv.volfrac += m_dpv.fadein; + else if (m_dpv.fadeout > 0) + m_dpv.volfrac -= m_dpv.fadeout; + + vol = m_dpv.volfrac >> 8; + + if (vol > m_dpv.volrun) + { + vol = m_dpv.volrun; + m_dpv.fadein = 0; // done with ramp up + } + + if (vol < m_dpv.volstart) + { + vol = m_dpv.volstart; + m_dpv.fadeout = 0; // done with ramp down + + // shut sound off + if (m_pPlayFrom) + { + STOP_SOUND( m_pPlayFrom, m_iChannel, szSoundFile); //LRC + } + else + { + UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile, + 0, 0, SND_STOP, 0); + } + + // return without setting nextthink + return; + } + + if (vol > 100) vol = 100; + if (vol < 1) vol = 1; + + m_dpv.vol = vol; + + fChanged |= (prev != vol); + flags |= SND_CHANGE_VOL; + } + + // =================== + // pitch/amplitude LFO + // =================== + if (m_dpv.lfotype) + { + int pos; + + if (m_dpv.lfofrac > 0x6fffffff) + m_dpv.lfofrac = 0; + + // update lfo, lfofrac/255 makes a triangle wave 0-255 + m_dpv.lfofrac += m_dpv.lforate; + pos = m_dpv.lfofrac >> 8; + + if (m_dpv.lfofrac < 0) + { + m_dpv.lfofrac = 0; + m_dpv.lforate = abs(m_dpv.lforate); + pos = 0; + } + else if (pos > 255) + { + pos = 255; + m_dpv.lfofrac = (255 << 8); + m_dpv.lforate = -abs(m_dpv.lforate); + } + + switch(m_dpv.lfotype) + { + case LFO_SQUARE: + if (pos < 128) + m_dpv.lfomult = 255; + else + m_dpv.lfomult = 0; + + break; + case LFO_RANDOM: + if (pos == 255) + m_dpv.lfomult = RANDOM_LONG(0, 255); + break; + case LFO_TRIANGLE: + default: + m_dpv.lfomult = pos; + break; + } + + if (m_dpv.lfomodpitch) + { + prev = pitch; + + // pitch 0-255 + pitch += ((m_dpv.lfomult - 128) * m_dpv.lfomodpitch) / 100; + + if (pitch > 255) pitch = 255; + if (pitch < 1) pitch = 1; + + + fChanged |= (prev != pitch); + flags |= SND_CHANGE_PITCH; + } + + if (m_dpv.lfomodvol) + { + // vol 0-100 + prev = vol; + + vol += ((m_dpv.lfomult - 128) * m_dpv.lfomodvol) / 100; + + if (vol > 100) vol = 100; + if (vol < 0) vol = 0; + + fChanged |= (prev != vol); + flags |= SND_CHANGE_VOL; + } + + } + + // Send update to playing sound only if we actually changed + // pitch or volume in this routine. + + if (flags && fChanged) + { + if (pitch == PITCH_NORM) + pitch = PITCH_NORM + 1; // don't send 'no pitch' ! + + if (m_pPlayFrom) + { + EMIT_SOUND_DYN( m_pPlayFrom, m_iChannel, szSoundFile, (vol * 0.01), //LRC + m_flAttenuation, flags, pitch); + } + else + { + UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile, + (vol * 0.01), m_flAttenuation, flags, pitch); + } + } + + // update ramps at 5hz + SetNextThink( 0.2 ); + return; +} + +// Init all ramp params in preparation to +// play a new sound + +void CAmbientGeneric :: InitModulationParms(void) +{ + int pitchinc; + + m_dpv.volrun = pev->health * 10; // 0 - 100 + if (m_dpv.volrun > 100) m_dpv.volrun = 100; + if (m_dpv.volrun < 0) m_dpv.volrun = 0; + + // get presets + if (m_dpv.preset != 0 && m_dpv.preset <= CDPVPRESETMAX) + { + // load preset values + m_dpv = rgdpvpreset[m_dpv.preset - 1]; + + // fixup preset values, just like + // fixups in KeyValue routine. + if (m_dpv.spindown > 0) + m_dpv.spindown = (101 - m_dpv.spindown) * 64; + if (m_dpv.spinup > 0) + m_dpv.spinup = (101 - m_dpv.spinup) * 64; + + m_dpv.volstart *= 10; + m_dpv.volrun *= 10; + + if (m_dpv.fadein > 0) + m_dpv.fadein = (101 - m_dpv.fadein) * 64; + if (m_dpv.fadeout > 0) + m_dpv.fadeout = (101 - m_dpv.fadeout) * 64; + + m_dpv.lforate *= 256; + + m_dpv.fadeinsav = m_dpv.fadein; + m_dpv.fadeoutsav = m_dpv.fadeout; + m_dpv.spinupsav = m_dpv.spinup; + m_dpv.spindownsav = m_dpv.spindown; + } + + m_dpv.fadein = m_dpv.fadeinsav; + m_dpv.fadeout = 0; + + if (m_dpv.fadein) + m_dpv.vol = m_dpv.volstart; + else + m_dpv.vol = m_dpv.volrun; + + m_dpv.spinup = m_dpv.spinupsav; + m_dpv.spindown = 0; + + if (m_dpv.spinup) + m_dpv.pitch = m_dpv.pitchstart; + else + m_dpv.pitch = m_dpv.pitchrun; + + if (m_dpv.pitch == 0) + m_dpv.pitch = PITCH_NORM; + + m_dpv.pitchfrac = m_dpv.pitch << 8; + m_dpv.volfrac = m_dpv.vol << 8; + + m_dpv.lfofrac = 0; + m_dpv.lforate = abs(m_dpv.lforate); + + m_dpv.cspincount = 1; + + if (m_dpv.cspinup) + { + pitchinc = (255 - m_dpv.pitchstart) / m_dpv.cspinup; + + m_dpv.pitchrun = m_dpv.pitchstart + pitchinc; + if (m_dpv.pitchrun > 255) m_dpv.pitchrun = 255; + } + + if ((m_dpv.spinupsav || m_dpv.spindownsav || (m_dpv.lfotype && m_dpv.lfomodpitch)) + && (m_dpv.pitch == PITCH_NORM)) + m_dpv.pitch = PITCH_NORM + 1; // must never send 'no pitch' as first pitch + // if we intend to pitch shift later! +} + +// +// ToggleUse - turns an ambient sound on or off. If the +// ambient is a looping sound, mark sound as active (m_fActive) +// if it's playing, innactive if not. If the sound is not +// a looping sound, never mark it as active. +// +void CAmbientGeneric :: ToggleUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + char* szSoundFile = (char*) STRING(pev->message); + float fraction; + + if ( useType != USE_TOGGLE ) + { + if ( (m_fActive && useType == USE_ON) || (!m_fActive && useType == USE_OFF) ) + return; + } + // Directly change pitch if arg passed. Only works if sound is already playing. + + if (useType == USE_SET && m_fActive) // Momentary buttons will pass down a float in here + { + + fraction = value; + + if ( fraction > 1.0 ) + fraction = 1.0; + if (fraction < 0.0) + fraction = 0.01; + + m_dpv.pitch = fraction * 255; + + if (m_pPlayFrom) + { + EMIT_SOUND_DYN( m_pPlayFrom, m_iChannel, szSoundFile, 0, 0, SND_CHANGE_PITCH, m_dpv.pitch); + } + else + { + UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile, + 0, 0, SND_CHANGE_PITCH, m_dpv.pitch); + } + + return; + } + + // Toggle + + // m_fActive is TRUE only if a looping sound is playing. + + if ( m_fActive ) + {// turn sound off + + if (m_dpv.cspinup) + { + // Don't actually shut off. Each toggle causes + // incremental spinup to max pitch + + if (m_dpv.cspincount <= m_dpv.cspinup) + { + int pitchinc; + + // start a new spinup + m_dpv.cspincount++; + + pitchinc = (255 - m_dpv.pitchstart) / m_dpv.cspinup; + + m_dpv.spinup = m_dpv.spinupsav; + m_dpv.spindown = 0; + + m_dpv.pitchrun = m_dpv.pitchstart + pitchinc * m_dpv.cspincount; + if (m_dpv.pitchrun > 255) m_dpv.pitchrun = 255; + + SetNextThink( 0.1 ); + } + + } + else + { + m_fActive = FALSE; + + // HACKHACK - this makes the code in Precache() work properly after a save/restore + pev->spawnflags |= AMBIENT_SOUND_START_SILENT; + + if (m_dpv.spindownsav || m_dpv.fadeoutsav) + { + // spin it down (or fade it) before shutoff if spindown is set + m_dpv.spindown = m_dpv.spindownsav; + m_dpv.spinup = 0; + + m_dpv.fadeout = m_dpv.fadeoutsav; + m_dpv.fadein = 0; + SetNextThink( 0.1 ); + } + else if (m_pPlayFrom) + { + STOP_SOUND( m_pPlayFrom, m_iChannel, szSoundFile); + } + else + { + UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile, + 0, 0, SND_STOP, 0); + } + } + } + else + {// turn sound on + + // only toggle if this is a looping sound. If not looping, each + // trigger will cause the sound to play. If the sound is still + // playing from a previous trigger press, it will be shut off + // and then restarted. + + if (m_fLooping) + { + m_fActive = TRUE; + } + else if (m_pPlayFrom) + { + STOP_SOUND( m_pPlayFrom, m_iChannel, szSoundFile); //LRC + } + else + { + // shut sound off now - may be interrupting a long non-looping sound + UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile, + 0, 0, SND_STOP, 0); + } + + // init all ramp params for startup + + InitModulationParms(); + + if (m_pPlayFrom) + { + EMIT_SOUND_DYN( m_pPlayFrom, m_iChannel, szSoundFile, //LRC + (m_dpv.vol * 0.01), m_flAttenuation, 0, m_dpv.pitch); + } + else + { + UTIL_EmitAmbientSound(ENT(pev), pev->origin, szSoundFile, + (m_dpv.vol * 0.01), m_flAttenuation, 0, m_dpv.pitch); + } + + SetNextThink( 0.1 ); + } +} +// KeyValue - load keyvalue pairs into member data of the +// ambient generic. NOTE: called BEFORE spawn! + +void CAmbientGeneric :: KeyValue( KeyValueData *pkvd ) +{ + // NOTE: changing any of the modifiers in this code + // NOTE: also requires changing InitModulationParms code. + + // channel (e.g. CHAN_STATIC, etc) + if (FStrEq(pkvd->szKeyName, "channel")) + { + m_iChannel = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + + // preset + else if (FStrEq(pkvd->szKeyName, "preset")) + { + m_dpv.preset = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + + // pitchrun + else if (FStrEq(pkvd->szKeyName, "pitch")) + { + m_dpv.pitchrun = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + + if (m_dpv.pitchrun > 255) m_dpv.pitchrun = 255; + if (m_dpv.pitchrun < 0) m_dpv.pitchrun = 0; + } + + // pitchstart + else if (FStrEq(pkvd->szKeyName, "pitchstart")) + { + m_dpv.pitchstart = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + + if (m_dpv.pitchstart > 255) m_dpv.pitchstart = 255; + if (m_dpv.pitchstart < 0) m_dpv.pitchstart = 0; + } + + // spinup + else if (FStrEq(pkvd->szKeyName, "spinup")) + { + m_dpv.spinup = atoi(pkvd->szValue); + + if (m_dpv.spinup > 100) m_dpv.spinup = 100; + if (m_dpv.spinup < 0) m_dpv.spinup = 0; + + if (m_dpv.spinup > 0) + m_dpv.spinup = (101 - m_dpv.spinup) * 64; + m_dpv.spinupsav = m_dpv.spinup; + pkvd->fHandled = TRUE; + } + + // spindown + else if (FStrEq(pkvd->szKeyName, "spindown")) + { + m_dpv.spindown = atoi(pkvd->szValue); + + if (m_dpv.spindown > 100) m_dpv.spindown = 100; + if (m_dpv.spindown < 0) m_dpv.spindown = 0; + + if (m_dpv.spindown > 0) + m_dpv.spindown = (101 - m_dpv.spindown) * 64; + m_dpv.spindownsav = m_dpv.spindown; + pkvd->fHandled = TRUE; + } + + // volstart + else if (FStrEq(pkvd->szKeyName, "volstart")) + { + m_dpv.volstart = atoi(pkvd->szValue); + + if (m_dpv.volstart > 10) m_dpv.volstart = 10; + if (m_dpv.volstart < 0) m_dpv.volstart = 0; + + m_dpv.volstart *= 10; // 0 - 100 + + pkvd->fHandled = TRUE; + } + + // fadein + else if (FStrEq(pkvd->szKeyName, "fadein")) + { + m_dpv.fadein = atoi(pkvd->szValue); + + if (m_dpv.fadein > 100) m_dpv.fadein = 100; + if (m_dpv.fadein < 0) m_dpv.fadein = 0; + + if (m_dpv.fadein > 0) + m_dpv.fadein = (101 - m_dpv.fadein) * 64; + m_dpv.fadeinsav = m_dpv.fadein; + pkvd->fHandled = TRUE; + } + + // fadeout + else if (FStrEq(pkvd->szKeyName, "fadeout")) + { + m_dpv.fadeout = atoi(pkvd->szValue); + + if (m_dpv.fadeout > 100) m_dpv.fadeout = 100; + if (m_dpv.fadeout < 0) m_dpv.fadeout = 0; + + if (m_dpv.fadeout > 0) + m_dpv.fadeout = (101 - m_dpv.fadeout) * 64; + m_dpv.fadeoutsav = m_dpv.fadeout; + pkvd->fHandled = TRUE; + } + + // lfotype + else if (FStrEq(pkvd->szKeyName, "lfotype")) + { + m_dpv.lfotype = atoi(pkvd->szValue); + if (m_dpv.lfotype > 4) m_dpv.lfotype = LFO_TRIANGLE; + pkvd->fHandled = TRUE; + } + + // lforate + else if (FStrEq(pkvd->szKeyName, "lforate")) + { + m_dpv.lforate = atoi(pkvd->szValue); + + if (m_dpv.lforate > 1000) m_dpv.lforate = 1000; + if (m_dpv.lforate < 0) m_dpv.lforate = 0; + + m_dpv.lforate *= 256; + + pkvd->fHandled = TRUE; + } + // lfomodpitch + else if (FStrEq(pkvd->szKeyName, "lfomodpitch")) + { + m_dpv.lfomodpitch = atoi(pkvd->szValue); + if (m_dpv.lfomodpitch > 100) m_dpv.lfomodpitch = 100; + if (m_dpv.lfomodpitch < 0) m_dpv.lfomodpitch = 0; + + + pkvd->fHandled = TRUE; + } + + // lfomodvol + else if (FStrEq(pkvd->szKeyName, "lfomodvol")) + { + m_dpv.lfomodvol = atoi(pkvd->szValue); + if (m_dpv.lfomodvol > 100) m_dpv.lfomodvol = 100; + if (m_dpv.lfomodvol < 0) m_dpv.lfomodvol = 0; + + pkvd->fHandled = TRUE; + } + + // cspinup + else if (FStrEq(pkvd->szKeyName, "cspinup")) + { + m_dpv.cspinup = atoi(pkvd->szValue); + if (m_dpv.cspinup > 100) m_dpv.cspinup = 100; + if (m_dpv.cspinup < 0) m_dpv.cspinup = 0; + + pkvd->fHandled = TRUE; + } + else + CBaseEntity::KeyValue( pkvd ); +} + +// ==================== SENTENCE GROUPS, UTILITY FUNCTIONS ====================================== + +#define CSENTENCE_LRU_MAX 32 // max number of elements per sentence group + +// group of related sentences + +typedef struct sentenceg +{ + char szgroupname[CBSENTENCENAME_MAX]; + int count; + unsigned char rgblru[CSENTENCE_LRU_MAX]; + +} SENTENCEG; + +#define CSENTENCEG_MAX 200 // max number of sentence groups +// globals + +SENTENCEG rgsentenceg[CSENTENCEG_MAX]; +int fSentencesInit = FALSE; + +char gszallsentencenames[CVOXFILESENTENCEMAX][CBSENTENCENAME_MAX]; +int gcallsentences = 0; + +// randomize list of sentence name indices + +void USENTENCEG_InitLRU(unsigned char *plru, int count) +{ + int i, j, k; + unsigned char temp; + + if (!fSentencesInit) + return; + + if (count > CSENTENCE_LRU_MAX) + count = CSENTENCE_LRU_MAX; + + for (i = 0; i < count; i++) + plru[i] = (unsigned char) i; + + // randomize array + for (i = 0; i < (count * 4); i++) + { + j = RANDOM_LONG(0,count-1); + k = RANDOM_LONG(0,count-1); + temp = plru[j]; + plru[j] = plru[k]; + plru[k] = temp; + } +} + +// ignore lru. pick next sentence from sentence group. Go in order until we hit the last sentence, +// then repeat list if freset is true. If freset is false, then repeat last sentence. +// ipick is passed in as the requested sentence ordinal. +// ipick 'next' is returned. +// return of -1 indicates an error. + +int USENTENCEG_PickSequential(int isentenceg, char *szfound, int ipick, int freset) +{ + char *szgroupname; + unsigned char count; + char sznum[8]; + + if (!fSentencesInit) + return -1; + + if (isentenceg < 0) + return -1; + + szgroupname = rgsentenceg[isentenceg].szgroupname; + count = rgsentenceg[isentenceg].count; + + if (count == 0) + return -1; + + if (ipick >= count) + ipick = count-1; + + strcpy(szfound, "!"); + strcat(szfound, szgroupname); + sprintf(sznum, "%d", ipick); + strcat(szfound, sznum); + + if (ipick >= count) + { + if (freset) + // reset at end of list + return 0; + else + return count; + } + + return ipick + 1; +} + + + +// pick a random sentence from rootname0 to rootnameX. +// picks from the rgsentenceg[isentenceg] least +// recently used, modifies lru array. returns the sentencename. +// note, lru must be seeded with 0-n randomized sentence numbers, with the +// rest of the lru filled with -1. The first integer in the lru is +// actually the size of the list. Returns ipick, the ordinal +// of the picked sentence within the group. + +int USENTENCEG_Pick(int isentenceg, char *szfound) +{ + char *szgroupname; + unsigned char *plru; + unsigned char i; + unsigned char count; + char sznum[8]; + unsigned char ipick; + int ffound = FALSE; + + if (!fSentencesInit) + return -1; + + if (isentenceg < 0) + return -1; + + szgroupname = rgsentenceg[isentenceg].szgroupname; + count = rgsentenceg[isentenceg].count; + plru = rgsentenceg[isentenceg].rgblru; + + while (!ffound) + { + for (i = 0; i < count; i++) + if (plru[i] != 0xFF) + { + ipick = plru[i]; + plru[i] = 0xFF; + ffound = TRUE; + break; + } + + if (!ffound) + USENTENCEG_InitLRU(plru, count); + else + { + strcpy(szfound, "!"); + strcat(szfound, szgroupname); + sprintf(sznum, "%d", ipick); + strcat(szfound, sznum); + return ipick; + } + } + return -1; +} + +// ===================== SENTENCE GROUPS, MAIN ROUTINES ======================== + +// Given sentence group rootname (name without number suffix), +// get sentence group index (isentenceg). Returns -1 if no such name. + +int SENTENCEG_GetIndex(const char *szgroupname) +{ + int i; + + if (!fSentencesInit || !szgroupname) + return -1; + + // search rgsentenceg for match on szgroupname + + i = 0; + while (rgsentenceg[i].count) + { + if (!strcmp(szgroupname, rgsentenceg[i].szgroupname)) + return i; + i++; + } + + return -1; +} + +// given sentence group index, play random sentence for given entity. +// returns ipick - which sentence was picked to +// play from the group. Ipick is only needed if you plan on stopping +// the sound before playback is done (see SENTENCEG_Stop). + +int SENTENCEG_PlayRndI(edict_t *entity, int isentenceg, + float volume, float attenuation, int flags, int pitch) +{ + char name[64]; + int ipick; + + if (!fSentencesInit) + return -1; + + name[0] = 0; + + ipick = USENTENCEG_Pick(isentenceg, name); + if (ipick > 0 && name) + EMIT_SOUND_DYN(entity, CHAN_VOICE, name, volume, attenuation, flags, pitch); + return ipick; +} + +// same as above, but takes sentence group name instead of index + +int SENTENCEG_PlayRndSz(edict_t *entity, const char *szgroupname, + float volume, float attenuation, int flags, int pitch) +{ + char name[64]; + int ipick; + int isentenceg; + + if (!fSentencesInit) + return -1; + + name[0] = 0; + + isentenceg = SENTENCEG_GetIndex(szgroupname); + if (isentenceg < 0) + { + ALERT( at_debug, "No such sentence group %s\n", szgroupname ); + return -1; + } + + ipick = USENTENCEG_Pick(isentenceg, name); + if (ipick >= 0 && name[0]) + EMIT_SOUND_DYN(entity, CHAN_VOICE, name, volume, attenuation, flags, pitch); + + return ipick; +} + +// play sentences in sequential order from sentence group. Reset after last sentence. + +int SENTENCEG_PlaySequentialSz(edict_t *entity, const char *szgroupname, + float volume, float attenuation, int flags, int pitch, int ipick, int freset) +{ + char name[64]; + int ipicknext; + int isentenceg; + + if (!fSentencesInit) + return -1; + + name[0] = 0; + + isentenceg = SENTENCEG_GetIndex(szgroupname); + if (isentenceg < 0) + return -1; + + ipicknext = USENTENCEG_PickSequential(isentenceg, name, ipick, freset); + if (ipicknext >= 0 && name[0]) + EMIT_SOUND_DYN(entity, CHAN_VOICE, name, volume, attenuation, flags, pitch); + return ipicknext; +} + + +// for this entity, for the given sentence within the sentence group, stop +// the sentence. + +void SENTENCEG_Stop(edict_t *entity, int isentenceg, int ipick) +{ + char buffer[64]; + char sznum[8]; + + if (!fSentencesInit) + return; + + if (isentenceg < 0 || ipick < 0) + return; + + strcpy(buffer, "!"); + strcat(buffer, rgsentenceg[isentenceg].szgroupname); + sprintf(sznum, "%d", ipick); + strcat(buffer, sznum); + + STOP_SOUND(entity, CHAN_VOICE, buffer); +} + +// open sentences.txt, scan for groups, build rgsentenceg +// Should be called from world spawn, only works on the +// first call and is ignored subsequently. + +void SENTENCEG_Init() +{ + char buffer[512]; + char szgroup[64]; + int i, j; + int isentencegs; + + if (fSentencesInit) + return; + + memset(gszallsentencenames, 0, CVOXFILESENTENCEMAX * CBSENTENCENAME_MAX); + gcallsentences = 0; + + memset(rgsentenceg, 0, CSENTENCEG_MAX * sizeof(SENTENCEG)); + memset(buffer, 0, 512); + memset(szgroup, 0, 64); + isentencegs = -1; + + + int filePos = 0, fileSize; + byte *pMemFile = g_engfuncs.pfnLoadFile( "sound/sentences.txt", &fileSize ); + if ( !pMemFile ) + return; + + // for each line in the file... + while ( memfgets(pMemFile, fileSize, filePos, buffer, 511) != NULL ) + { + // skip whitespace + i = 0; + while(buffer[i] && buffer[i] == ' ') + i++; + + if (!buffer[i]) + continue; + + if (buffer[i] == '/' || !isalpha(buffer[i])) + continue; + + // get sentence name + j = i; + while (buffer[j] && buffer[j] != ' ') + j++; + + if (!buffer[j]) + continue; + + if (gcallsentences > CVOXFILESENTENCEMAX) + { + ALERT (at_error, "Too many sentences in sentences.txt!\n"); + break; + } + + // null-terminate name and save in sentences array + buffer[j] = 0; + const char *pString = buffer + i; + + if ( strlen( pString ) >= CBSENTENCENAME_MAX ) + ALERT( at_warning, "Sentence %s longer than %d letters\n", pString, CBSENTENCENAME_MAX-1 ); + + strcpy( gszallsentencenames[gcallsentences++], pString ); + + j--; + if (j <= i) + continue; + if (!isdigit(buffer[j])) + continue; + + // cut out suffix numbers + while (j > i && isdigit(buffer[j])) + j--; + + if (j <= i) + continue; + + buffer[j+1] = 0; + + // if new name doesn't match previous group name, + // make a new group. + + if (strcmp(szgroup, &(buffer[i]))) + { + // name doesn't match with prev name, + // copy name into group, init count to 1 + isentencegs++; + if (isentencegs >= CSENTENCEG_MAX) + { + ALERT (at_error, "Too many sentence groups in sentences.txt!\n"); + break; + } + + strcpy(rgsentenceg[isentencegs].szgroupname, &(buffer[i])); + rgsentenceg[isentencegs].count = 1; + + strcpy(szgroup, &(buffer[i])); + + continue; + } + else + { + //name matches with previous, increment group count + if (isentencegs >= 0) + rgsentenceg[isentencegs].count++; + } + } + + g_engfuncs.pfnFreeFile( pMemFile ); + + fSentencesInit = TRUE; + + // init lru lists + + i = 0; + + while (rgsentenceg[i].count && i < CSENTENCEG_MAX) + { + USENTENCEG_InitLRU(&(rgsentenceg[i].rgblru[0]), rgsentenceg[i].count); + i++; + } + +} + +// convert sentence (sample) name to !sentencenum, return !sentencenum + +int SENTENCEG_Lookup(const char *sample, char *sentencenum) +{ + char sznum[8]; + + int i; + // this is a sentence name; lookup sentence number + // and give to engine as string. + for (i = 0; i < gcallsentences; i++) + if (!stricmp(gszallsentencenames[i], sample+1)) + { + if (sentencenum) + { + strcpy(sentencenum, "!"); + sprintf(sznum, "%d", i); + strcat(sentencenum, sznum); + } + return i; + } + // sentence name not found! + return -1; +} + +void EMIT_SOUND_DYN(edict_t *entity, int channel, const char *sample, float volume, float attenuation, + int flags, int pitch) +{ + if (sample && *sample == '!') + { + char name[32]; + if (SENTENCEG_Lookup(sample, name) >= 0) + EMIT_SOUND_DYN2(entity, channel, name, volume, attenuation, flags, pitch); + else + ALERT( at_aiconsole, "Unable to find %s in sentences.txt\n", sample ); + } + else + EMIT_SOUND_DYN2(entity, channel, sample, volume, attenuation, flags, pitch); +} + +// play a specific sentence over the HEV suit speaker - just pass player entity, and !sentencename + +void EMIT_SOUND_SUIT(edict_t *entity, const char *sample) +{ + float fvol; + int pitch = PITCH_NORM; + + fvol = CVAR_GET_FLOAT("suitvolume"); + if (RANDOM_LONG(0,1)) + pitch = RANDOM_LONG(0,6) + 98; + + if (fvol > 0.05) + EMIT_SOUND_DYN(entity, CHAN_STATIC, sample, fvol, ATTN_NORM, 0, pitch); +} + +// play a sentence, randomly selected from the passed in group id, over the HEV suit speaker + +void EMIT_GROUPID_SUIT(edict_t *entity, int isentenceg) +{ + float fvol; + int pitch = PITCH_NORM; + + fvol = CVAR_GET_FLOAT("suitvolume"); + if (RANDOM_LONG(0,1)) + pitch = RANDOM_LONG(0,6) + 98; + + if (fvol > 0.05) + SENTENCEG_PlayRndI(entity, isentenceg, fvol, ATTN_NORM, 0, pitch); +} + +// play a sentence, randomly selected from the passed in groupname + +void EMIT_GROUPNAME_SUIT(edict_t *entity, const char *groupname) +{ + float fvol; + int pitch = PITCH_NORM; + + fvol = CVAR_GET_FLOAT("suitvolume"); + if (RANDOM_LONG(0,1)) + pitch = RANDOM_LONG(0,6) + 98; + + if (fvol > 0.05) + SENTENCEG_PlayRndSz(entity, groupname, fvol, ATTN_NORM, 0, pitch); +} + +// ===================== MATERIAL TYPE DETECTION, MAIN ROUTINES ======================== +// +// Used to detect the texture the player is standing on, map the +// texture name to a material type. Play footstep sound based +// on material type. + +int fTextureTypeInit = FALSE; + +#define CTEXTURESMAX 512 // max number of textures loaded + +int gcTextures = 0; +char grgszTextureName[CTEXTURESMAX][CBTEXTURENAMEMAX]; // texture names +char grgchTextureType[CTEXTURESMAX]; // parallel array of texture types + +// open materials.txt, get size, alloc space, +// save in array. Only works first time called, +// ignored on subsequent calls. + +static char *memfgets( byte *pMemFile, int fileSize, int &filePos, char *pBuffer, int bufferSize ) +{ + // Bullet-proofing + if ( !pMemFile || !pBuffer ) + return NULL; + + if ( filePos >= fileSize ) + return NULL; + + int i = filePos; + int last = fileSize; + + // fgets always NULL terminates, so only read bufferSize-1 characters + if ( last - filePos > (bufferSize-1) ) + last = filePos + (bufferSize-1); + + int stop = 0; + + // Stop at the next newline (inclusive) or end of buffer + while ( i < last && !stop ) + { + if ( pMemFile[i] == '\n' ) + stop = 1; + i++; + } + + + // If we actually advanced the pointer, copy it over + if ( i != filePos ) + { + // We read in size bytes + int size = i - filePos; + // copy it out + memcpy( pBuffer, pMemFile + filePos, sizeof(byte)*size ); + + // If the buffer isn't full, terminate (this is always true) + if ( size < bufferSize ) + pBuffer[size] = 0; + + // Update file pointer + filePos = i; + return pBuffer; + } + + // No data read, bail + return NULL; +} + + +void TEXTURETYPE_Init() +{ + char buffer[512]; + int i, j; + byte *pMemFile; + int fileSize, filePos; + + if (fTextureTypeInit) + return; + + memset(&(grgszTextureName[0][0]), 0, CTEXTURESMAX * CBTEXTURENAMEMAX); + memset(grgchTextureType, 0, CTEXTURESMAX); + + gcTextures = 0; + memset(buffer, 0, 512); + + pMemFile = g_engfuncs.pfnLoadFile( "sound/materials.txt", &fileSize ); + if ( !pMemFile ) + return; + + // for each line in the file... + while (memfgets(pMemFile, fileSize, filePos, buffer, 511) != NULL && (gcTextures < CTEXTURESMAX)) + { + // skip whitespace + i = 0; + while(buffer[i] && isspace(buffer[i])) + i++; + + if (!buffer[i]) + continue; + + // skip comment lines + if (buffer[i] == '/' || !isalpha(buffer[i])) + continue; + + // get texture type + grgchTextureType[gcTextures] = toupper(buffer[i++]); + + // skip whitespace + while(buffer[i] && isspace(buffer[i])) + i++; + + if (!buffer[i]) + continue; + + // get sentence name + j = i; + while (buffer[j] && !isspace(buffer[j])) + j++; + + if (!buffer[j]) + continue; + + // null-terminate name and save in sentences array + j = min (j, CBTEXTURENAMEMAX-1+i); + buffer[j] = 0; + strcpy(&(grgszTextureName[gcTextures++][0]), &(buffer[i])); + } + + g_engfuncs.pfnFreeFile( pMemFile ); + + fTextureTypeInit = TRUE; +} + +// given texture name, find texture type +// if not found, return type 'concrete' + +// NOTE: this routine should ONLY be called if the +// current texture under the player changes! + +char TEXTURETYPE_Find(char *name) +{ + // CONSIDER: pre-sort texture names and perform faster binary search here + + for (int i = 0; i < gcTextures; i++) + { + if (!strnicmp(name, &(grgszTextureName[i][0]), CBTEXTURENAMEMAX-1)) + return (grgchTextureType[i]); + } + + return CHAR_TEX_CONCRETE; +} + +// play a strike sound based on the texture that was hit by the attack traceline. VecSrc/VecEnd are the +// original traceline endpoints used by the attacker, iBulletType is the type of bullet that hit the texture. +// returns volume of strike instrument (crowbar) to play +// (this is not used for footsteps, only attack sound effects. --LRC) + +float TEXTURETYPE_PlaySound(TraceResult *ptr, Vector vecSrc, Vector vecEnd, int iBulletType) +{ +// hit the world, try to play sound based on texture material type + + char chTextureType; + float fvol; + float fvolbar; + char szbuffer[64]; + const char *pTextureName; + float rgfl1[3]; + float rgfl2[3]; + char *rgsz[4]; + int cnt; + float fattn = ATTN_NORM; + + if ( !g_pGameRules->PlayTextureSounds() ) + return 0.0; + + CBaseEntity *pEntity = CBaseEntity::Instance(ptr->pHit); + CBaseBrush *pBrush = pEntity->MyBrushPointer(); + + if(pBrush && pBrush->m_Material) return MatVolume(pBrush->m_Material); + + chTextureType = 0; + if (pEntity && pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE) + // hit body + chTextureType = CHAR_TEX_FLESH; + else + { + // hit world + + // find texture under strike, get material type + + // copy trace vector into array for trace_texture + + vecSrc.CopyToArray(rgfl1); + vecEnd.CopyToArray(rgfl2); + + // get texture from entity or world (world is ent(0)) + if (pEntity) + pTextureName = TRACE_TEXTURE( ENT(pEntity->pev), rgfl1, rgfl2 ); + else + pTextureName = TRACE_TEXTURE( ENT(0), rgfl1, rgfl2 ); + + if ( pTextureName ) + { + // strip leading '-0' or '+0~' or '{' or '!' + if (*pTextureName == '-' || *pTextureName == '+') + pTextureName += 2; + + if (*pTextureName == '{' || *pTextureName == '!' || *pTextureName == '~' || *pTextureName == ' ') + pTextureName++; + // '}}' + strcpy(szbuffer, pTextureName); + szbuffer[CBTEXTURENAMEMAX - 1] = 0; + + // ALERT ( at_console, "texture hit: %s\n", szbuffer); + + // get texture type + chTextureType = TEXTURETYPE_Find(szbuffer); + } + } + + switch (chTextureType) + { + default: + case CHAR_TEX_CONCRETE: fvol = 0.9; fvolbar = 0.6; + rgsz[0] = "player/pl_step1.wav"; + rgsz[1] = "player/pl_step2.wav"; + cnt = 2; + break; + case CHAR_TEX_METAL: fvol = 0.9; fvolbar = 0.3; + rgsz[0] = "player/pl_metal1.wav"; + rgsz[1] = "player/pl_metal2.wav"; + cnt = 2; + break; + case CHAR_TEX_DIRT: fvol = 0.9; fvolbar = 0.1; + rgsz[0] = "player/pl_dirt1.wav"; + rgsz[1] = "player/pl_dirt2.wav"; + rgsz[2] = "player/pl_dirt3.wav"; + cnt = 3; + break; + case CHAR_TEX_VENT: fvol = 0.5; fvolbar = 0.3; + rgsz[0] = "player/pl_duct1.wav"; + rgsz[1] = "player/pl_duct1.wav"; + cnt = 2; + break; + case CHAR_TEX_GRATE: fvol = 0.9; fvolbar = 0.5; + rgsz[0] = "player/pl_grate1.wav"; + rgsz[1] = "player/pl_grate4.wav"; + cnt = 2; + break; + case CHAR_TEX_TILE: fvol = 0.8; fvolbar = 0.2; + rgsz[0] = "player/pl_tile1.wav"; + rgsz[1] = "player/pl_tile3.wav"; + rgsz[2] = "player/pl_tile2.wav"; + rgsz[3] = "player/pl_tile4.wav"; + cnt = 4; + break; + case CHAR_TEX_SLOSH: fvol = 0.9; fvolbar = 0.0; + rgsz[0] = "player/pl_slosh1.wav"; + rgsz[1] = "player/pl_slosh3.wav"; + rgsz[2] = "player/pl_slosh2.wav"; + rgsz[3] = "player/pl_slosh4.wav"; + cnt = 4; + break; + case CHAR_TEX_WOOD: fvol = 0.9; fvolbar = 0.2; + rgsz[0] = "debris/wood1.wav"; + rgsz[1] = "debris/wood2.wav"; + rgsz[2] = "debris/wood3.wav"; + cnt = 3; + break; + case CHAR_TEX_GLASS: + case CHAR_TEX_COMPUTER: + fvol = 0.8; fvolbar = 0.2; + rgsz[0] = "debris/glass1.wav"; + rgsz[1] = "debris/glass2.wav"; + rgsz[2] = "debris/glass3.wav"; + cnt = 3; + break; + case CHAR_TEX_FLESH: + // crowbar already makes this sound + if (iBulletType == BULLET_CROWBAR) return 0.0; + fvol = 1.0; fvolbar = 0.2; + rgsz[0] = "weapons/bullet_hit1.wav"; + rgsz[1] = "weapons/bullet_hit2.wav"; + fattn = 1.0; + cnt = 2; + break; + } + + // did we hit a breakable? + + if (chTextureType == CHAR_TEX_COMPUTER) + { + // play random spark if computer + + if ( ptr->flFraction != 1.0 && RANDOM_LONG(0,1)) + { + UTIL_Sparks( ptr->vecEndPos ); + + float flVolume = RANDOM_FLOAT ( 0.7 , 1.0 );//random volume range + switch ( RANDOM_LONG(0,1) ) + { + case 0: UTIL_EmitAmbientSound(ENT(0), ptr->vecEndPos, "buttons/spark5.wav", flVolume, ATTN_NORM, 0, 100); break; + case 1: UTIL_EmitAmbientSound(ENT(0), ptr->vecEndPos, "buttons/spark6.wav", flVolume, ATTN_NORM, 0, 100); break; + } + } + } + + // play material hit sound + UTIL_EmitAmbientSound(ENT(0), ptr->vecEndPos, rgsz[RANDOM_LONG(0,cnt-1)], fvol, fattn, 0, 96 + RANDOM_LONG(0,0xf)); + + return fvolbar; +} + +// =================================================================================== +// +// Speaker class. Used for announcements per level, for door lock/unlock spoken voice. +// + +class CSpeaker : public CBaseEntity +{ +public: + void KeyValue( KeyValueData* pkvd); + void Spawn( void ); + void Precache( void ); + void EXPORT ToggleUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT SpeakerThink( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + virtual int ObjectCaps( void ) { return (CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); } + + int m_preset; // preset number +}; + +LINK_ENTITY_TO_CLASS( speaker, CSpeaker ); +TYPEDESCRIPTION CSpeaker::m_SaveData[] = +{ + DEFINE_FIELD( CSpeaker, m_preset, FIELD_INTEGER ), +}; + +IMPLEMENT_SAVERESTORE( CSpeaker, CBaseEntity ); + +// +// ambient_generic - general-purpose user-defined static sound +// +void CSpeaker :: Spawn( void ) +{ + char* szSoundFile = (char*) STRING(pev->message); + + if ( !m_preset && (FStringNull( pev->message ) || strlen( szSoundFile ) < 1 )) + { + ALERT( at_error, "SPEAKER with no Level/Sentence! at: %f, %f, %f\n", pev->origin.x, pev->origin.y, pev->origin.z ); + SetNextThink( 0.1 ); + SetThink( Remove ); + return; + } + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_NONE; + + + SetThink(&CSpeaker ::SpeakerThink); + DontThink(); + + // allow on/off switching via 'use' function. + + SetUse(&CSpeaker :: ToggleUse ); + + Precache( ); +} + +#define ANNOUNCE_MINUTES_MIN 0.25 +#define ANNOUNCE_MINUTES_MAX 2.25 + +void CSpeaker :: Precache( void ) +{ + if ( !FBitSet (pev->spawnflags, SPEAKER_START_SILENT ) ) + // set first announcement time for random n second + SetNextThink( RANDOM_FLOAT(5.0, 15.0) ); +} +void CSpeaker :: SpeakerThink( void ) +{ + char* szSoundFile; + float flvolume = pev->health * 0.1; + float flattenuation = 0.3; + int flags = 0; + int pitch = 100; + + + // Wait for the talkmonster to finish first. + if (gpGlobals->time <= CTalkMonster::g_talkWaitTime) + { + AbsoluteNextThink( CTalkMonster::g_talkWaitTime + RANDOM_FLOAT( 5, 10 ) ); + return; + } + + if (m_preset) + { + // go lookup preset text, assign szSoundFile + switch (m_preset) + { + case 1: szSoundFile = "C1A0_"; break; + case 2: szSoundFile = "C1A1_"; break; + case 3: szSoundFile = "C1A2_"; break; + case 4: szSoundFile = "C1A3_"; break; + case 5: szSoundFile = "C1A4_"; break; + case 6: szSoundFile = "C2A1_"; break; + case 7: szSoundFile = "C2A2_"; break; + case 8: szSoundFile = "C2A3_"; break; + case 9: szSoundFile = "C2A4_"; break; + case 10: szSoundFile = "C2A5_"; break; + case 11: szSoundFile = "C3A1_"; break; + case 12: szSoundFile = "C3A2_"; break; + } + } else + szSoundFile = (char*) STRING(pev->message); + + if (szSoundFile[0] == '!') + { + // play single sentence, one shot + UTIL_EmitAmbientSound ( ENT(pev), pev->origin, szSoundFile, + flvolume, flattenuation, flags, pitch); + + // shut off and reset + DontThink(); + } + else + { + // make random announcement from sentence group + + if (SENTENCEG_PlayRndSz(ENT(pev), szSoundFile, flvolume, flattenuation, flags, pitch) < 0) + ALERT(at_debug, "Level Design Error!\nSPEAKER has bad sentence group name: %s\n",szSoundFile); + + // set next announcement time for random 5 to 10 minute delay + SetNextThink( RANDOM_FLOAT(ANNOUNCE_MINUTES_MIN * 60.0, ANNOUNCE_MINUTES_MAX * 60.0) ); + + CTalkMonster::g_talkWaitTime = gpGlobals->time + 5; // time delay until it's ok to speak: used so that two NPCs don't talk at once + } + + return; +} + + +// +// ToggleUse - if an announcement is pending, cancel it. If no announcement is pending, start one. +// +void CSpeaker :: ToggleUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + int fActive = (m_fNextThink > 0.0); + + // fActive is TRUE only if an announcement is pending + + if ( useType != USE_TOGGLE ) + { + // ignore if we're just turning something on that's already on, or + // turning something off that's already off. + if ( (fActive && useType == USE_ON) || (!fActive && useType == USE_OFF) ) + return; + } + + if ( useType == USE_ON ) + { + // turn on announcements + SetNextThink( 0.1 ); + return; + } + + if ( useType == USE_OFF ) + { + // turn off announcements + DontThink(); + return; + + } + + // Toggle announcements + + + if ( fActive ) + { + // turn off announcements + DontThink(); + } + else + { + // turn on announcements + SetNextThink( 0.1 ); + } +} + +// KeyValue - load keyvalue pairs into member data +// NOTE: called BEFORE spawn! + +void CSpeaker :: KeyValue( KeyValueData *pkvd ) +{ + + // preset + if (FStrEq(pkvd->szKeyName, "preset")) + { + m_preset = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseEntity::KeyValue( pkvd ); +} + +#define SF_LOOP 2 + +class CFMODAudio : public CBaseLogic +{ +public: + void Spawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void StartMessage( CBasePlayer *pPlayer ); +}; +LINK_ENTITY_TO_CLASS( ambient_music, CFMODAudio ); + +void CFMODAudio::Spawn( void ) +{ + if(pev->spawnflags & SF_START_ON ) + { + SetBits( pev->impulse, SF_START_ON ); + m_iState = STATE_ON; + } + if(pev->spawnflags & SF_LOOP) SetBits( pev->impulse, SF_LOOP ); +} + +void CFMODAudio::StartMessage( CBasePlayer *pPlayer ) +{ + MESSAGE_BEGIN( MSG_ONE, gmsgFsound, NULL, pPlayer->pev ); + WRITE_STRING( STRING(pev->message)); + WRITE_SHORT( pev->button );//position + WRITE_SHORT( pev->impulse );//flags + MESSAGE_END(); +} + +void CFMODAudio::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if(pActivator && pActivator->IsPlayer())//only at player + { + m_hActivator = pActivator;//save activator + } + else m_hActivator = UTIL_PlayerByIndex( 1 ); + + if (useType == USE_TOGGLE) + { + if(m_iState == STATE_OFF) useType = USE_ON; + else useType = USE_OFF; + } + if (useType == USE_ON) + { + m_iState = STATE_ON; + SetBits( pev->impulse, SF_START_ON ); + StartMessage( (CBasePlayer *)(CBaseEntity *)m_hActivator ); + } + else if (useType == USE_OFF) + { + ClearBits( pev->impulse, SF_START_ON ); + StartMessage( (CBasePlayer *)(CBaseEntity *)m_hActivator ); + m_iState = STATE_OFF; + } + else if (useType == USE_SHOWINFO) + { + DEBUGHEAD; + ALERT(at_console, "State: %s, Song name: %s\n", GetStringForState( GetState()), STRING(pev->message)); + SHIFT; + } +} \ No newline at end of file diff --git a/server/game/spectator.h b/server/game/spectator.h new file mode 100644 index 00000000..d459a384 --- /dev/null +++ b/server/game/spectator.h @@ -0,0 +1,27 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// Spectator.h + +class CBaseSpectator : public CBaseEntity +{ +public: + void Spawn(); + void SpectatorConnect(void); + void SpectatorDisconnect(void); + void SpectatorThink(void); + +private: + void SpectatorImpulseCommand(void); +}; \ No newline at end of file diff --git a/server/game/teamplay_gamerules.cpp b/server/game/teamplay_gamerules.cpp new file mode 100644 index 00000000..72b7fe91 --- /dev/null +++ b/server/game/teamplay_gamerules.cpp @@ -0,0 +1,614 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// teamplay_gamerules.cpp +// +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "player.h" +#include "baseweapon.h" +#include "gamerules.h" +#include "teamplay_gamerules.h" +#include "game.h" + +static char team_names[MAX_TEAMS][MAX_TEAMNAME_LENGTH]; +static int team_scores[MAX_TEAMS]; +static int num_teams = 0; + +extern DLL_GLOBAL BOOL g_fGameOver; + +CHalfLifeTeamplay :: CHalfLifeTeamplay() +{ + m_DisableDeathMessages = FALSE; + m_DisableDeathPenalty = FALSE; + + memset( team_names, 0, sizeof(team_names) ); + memset( team_scores, 0, sizeof(team_scores) ); + num_teams = 0; + + // Copy over the team from the server config + m_szTeamList[0] = 0; + + // Cache this because the team code doesn't want to deal with changing this in the middle of a game + strncpy( m_szTeamList, CVAR_GET_STRING( "mp_teamlist" ), TEAMPLAY_TEAMLISTLENGTH ); + + edict_t *pWorld = INDEXENT(0); + if ( pWorld && pWorld->v.team ) + { + if( CVAR_GET_FLOAT( "mp_teamoverride" )) + { + const char *pTeamList = STRING(pWorld->v.team); + if ( pTeamList && strlen(pTeamList) ) + { + strncpy( m_szTeamList, pTeamList, TEAMPLAY_TEAMLISTLENGTH ); + } + } + } + // Has the server set teams + if ( strlen( m_szTeamList ) ) + m_teamLimit = TRUE; + else + m_teamLimit = FALSE; + + RecountTeams(); +} + +void CHalfLifeTeamplay :: Think ( void ) +{ + ///// Check game rules ///// + static int last_frags; + static int last_time; + + int frags_remaining = 0; + int time_remaining = 0; + + if ( g_fGameOver ) // someone else quit the game already + { + CHalfLifeMultiplay::Think(); + return; + } + + float flTimeLimit = CVAR_GET_FLOAT("mp_timelimit") * 60; + + time_remaining = (int)(flTimeLimit ? ( flTimeLimit - gpGlobals->time ) : 0); + + if ( flTimeLimit != 0 && gpGlobals->time >= flTimeLimit ) + { + GoToIntermission(); + return; + } + + float flFragLimit = CVAR_GET_FLOAT( "mp_fraglimit" ); + if ( flFragLimit ) + { + int bestfrags = 9999; + int remain; + + // check if any team is over the frag limit + for ( int i = 0; i < num_teams; i++ ) + { + if ( team_scores[i] >= flFragLimit ) + { + GoToIntermission(); + return; + } + + remain = flFragLimit - team_scores[i]; + if ( remain < bestfrags ) + { + bestfrags = remain; + } + } + frags_remaining = bestfrags; + } + + // Updates when frags change + if( frags_remaining != last_frags ) + { + g_engfuncs.pfnCVarSetString( "mp_fragsleft", UTIL_VarArgs( "%i", frags_remaining )); + } + + // Updates once per second + if( CVAR_GET_FLOAT( "mp_timeleft" ) != last_time ) + { + g_engfuncs.pfnCVarSetString( "mp_timeleft", UTIL_VarArgs( "%i", time_remaining )); + } + + last_frags = frags_remaining; + last_time = time_remaining; +} + +//========================================================= +// ClientCommand +// the user has typed a command which is unrecognized by everything else; +// this check to see if the gamerules knows anything about the command +//========================================================= +BOOL CHalfLifeTeamplay :: ClientCommand( CBasePlayer *pPlayer, const char *pcmd ) +{ + if ( FStrEq( pcmd, "menuselect" ) ) + { + if ( CMD_ARGC() < 2 ) + return TRUE; + + int slot = atoi( CMD_ARGV(1) ); + + // select the item from the current menu + + return TRUE; + } + + return FALSE; +} + +extern int gmsgGameMode; +extern int gmsgSayText; +extern int gmsgTeamInfo; +extern int gmsgTeamNames; +extern int gmsgScoreInfo; + +void CHalfLifeTeamplay :: UpdateGameMode( CBasePlayer *pPlayer ) +{ + MESSAGE_BEGIN( MSG_ONE, gmsgGameMode, NULL, pPlayer->edict() ); + WRITE_BYTE( 1 ); // game mode teamplay + MESSAGE_END(); +} + + +const char *CHalfLifeTeamplay::SetDefaultPlayerTeam( CBasePlayer *pPlayer ) +{ + // copy out the team name from the model + char *mdls = g_engfuncs.pfnInfoKeyValue( g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model" ); + strncpy( pPlayer->m_szTeamName, mdls, TEAM_NAME_LENGTH ); + + RecountTeams(); + + // update the current player of the team he is joining + if ( pPlayer->m_szTeamName[0] == '\0' || !IsValidTeam( pPlayer->m_szTeamName ) || CVAR_GET_FLOAT( "mp_defaultteam" )) + { + const char *pTeamName = NULL; + + if( CVAR_GET_FLOAT( "mp_defaultteam" )) + { + pTeamName = team_names[0]; + } + else + { + pTeamName = TeamWithFewestPlayers(); + } + strncpy( pPlayer->m_szTeamName, pTeamName, TEAM_NAME_LENGTH ); + } + + return pPlayer->m_szTeamName; +} + + +//========================================================= +// InitHUD +//========================================================= +void CHalfLifeTeamplay::InitHUD( CBasePlayer *pPlayer ) +{ + int i; + + SetDefaultPlayerTeam( pPlayer ); + CHalfLifeMultiplay::InitHUD( pPlayer ); + + // Send down the team names + MESSAGE_BEGIN( MSG_ONE, gmsgTeamNames, NULL, pPlayer->edict() ); + WRITE_BYTE( num_teams ); + for ( i = 0; i < num_teams; i++ ) + { + WRITE_STRING( team_names[ i ] ); + } + MESSAGE_END(); + + RecountTeams(); + + char *mdls = g_engfuncs.pfnInfoKeyValue( g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model" ); + // update the current player of the team he is joining + char text[1024]; + if ( !strcmp( mdls, pPlayer->m_szTeamName ) ) + { + sprintf( text, "* you are on team \'%s\'\n", pPlayer->m_szTeamName ); + } + else + { + sprintf( text, "* assigned to team %s\n", pPlayer->m_szTeamName ); + } + + ChangePlayerTeam( pPlayer, pPlayer->m_szTeamName, FALSE, FALSE ); + UTIL_SayText( text, pPlayer ); + int clientIndex = pPlayer->entindex(); + RecountTeams(); + // update this player with all the other players team info + // loop through all active players and send their team info to the new client + for ( i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *plr = UTIL_PlayerByIndex( i ); + if ( plr && IsValidTeam( plr->TeamID() ) ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgTeamInfo, NULL, pPlayer->edict() ); + WRITE_BYTE( plr->entindex() ); + WRITE_STRING( plr->TeamID() ); + MESSAGE_END(); + } + } +} + + +void CHalfLifeTeamplay::ChangePlayerTeam( CBasePlayer *pPlayer, const char *pTeamName, BOOL bKill, BOOL bGib ) +{ + int damageFlags = DMG_GENERIC; + int clientIndex = pPlayer->entindex(); + + if ( !bGib ) + { + damageFlags |= DMG_NEVERGIB; + } + else + { + damageFlags |= DMG_ALWAYSGIB; + } + + if ( bKill ) + { + // kill the player, remove a death, and let them start on the new team + m_DisableDeathMessages = TRUE; + m_DisableDeathPenalty = TRUE; + + entvars_t *pevWorld = VARS( INDEXENT(0) ); + pPlayer->TakeDamage( pevWorld, pevWorld, 900, damageFlags ); + + m_DisableDeathMessages = FALSE; + m_DisableDeathPenalty = FALSE; + } + + // copy out the team name from the model + strncpy( pPlayer->m_szTeamName, pTeamName, TEAM_NAME_LENGTH ); + + g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model", pPlayer->m_szTeamName ); + g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "team", pPlayer->m_szTeamName ); + + // notify everyone's HUD of the team change + MESSAGE_BEGIN( MSG_ALL, gmsgTeamInfo ); + WRITE_BYTE( clientIndex ); + WRITE_STRING( pPlayer->m_szTeamName ); + MESSAGE_END(); + + MESSAGE_BEGIN( MSG_ALL, gmsgScoreInfo ); + WRITE_BYTE( clientIndex ); + WRITE_SHORT( pPlayer->pev->frags ); + WRITE_SHORT( pPlayer->m_iDeaths ); + WRITE_SHORT( 0 ); + WRITE_SHORT( g_pGameRules->GetTeamIndex( pPlayer->m_szTeamName ) + 1 ); + MESSAGE_END(); +} + + +//========================================================= +// ClientUserInfoChanged +//========================================================= +void CHalfLifeTeamplay::ClientUserInfoChanged( CBasePlayer *pPlayer, char *infobuffer ) +{ + char text[1024]; + + // prevent skin/color/model changes + char *mdls = g_engfuncs.pfnInfoKeyValue( infobuffer, "model" ); + + if ( !stricmp( mdls, pPlayer->m_szTeamName ) ) + return; + + if( CVAR_GET_FLOAT( "mp_defaultteam" )) + { + int clientIndex = pPlayer->entindex(); + + g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model", pPlayer->m_szTeamName ); + g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "team", pPlayer->m_szTeamName ); + sprintf( text, "* Not allowed to change teams in this game!\n" ); + UTIL_SayText( text, pPlayer ); + return; + } + + if( CVAR_GET_FLOAT( "mp_defaultteam" ) || !IsValidTeam( mdls )) + { + int clientIndex = pPlayer->entindex(); + + g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model", pPlayer->m_szTeamName ); + sprintf( text, "* Can't change team to \'%s\'\n", mdls ); + UTIL_SayText( text, pPlayer ); + sprintf( text, "* Server limits teams to \'%s\'\n", m_szTeamList ); + UTIL_SayText( text, pPlayer ); + return; + } + // notify everyone of the team change + sprintf( text, "* %s has changed to team \'%s\'\n", STRING(pPlayer->pev->netname), mdls ); + UTIL_SayTextAll( text, pPlayer ); + + ChangePlayerTeam( pPlayer, mdls, TRUE, TRUE ); + + // recound stuff + RecountTeams( TRUE ); +} + +extern int gmsgDeathMsg; + +//========================================================= +// Deathnotice. +//========================================================= +void CHalfLifeTeamplay::DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pevInflictor ) +{ + if ( m_DisableDeathMessages ) + return; + + if ( pVictim && pKiller && pKiller->flags & FL_CLIENT ) + { + CBasePlayer *pk = (CBasePlayer*) CBaseEntity::Instance( pKiller ); + + if ( pk ) + { + if ( (pk != pVictim) && (PlayerRelationship( pVictim, pk ) == GR_TEAMMATE) ) + { + MESSAGE_BEGIN( MSG_ALL, gmsgDeathMsg ); + WRITE_BYTE( ENTINDEX(ENT(pKiller)) ); // the killer + WRITE_BYTE( ENTINDEX(pVictim->edict()) ); // the victim + WRITE_STRING( "teammate" ); // flag this as a teammate kill + MESSAGE_END(); + return; + } + } + } + + CHalfLifeMultiplay::DeathNotice( pVictim, pKiller, pevInflictor ); +} + +//========================================================= +//========================================================= +void CHalfLifeTeamplay :: PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ) +{ + if ( !m_DisableDeathPenalty ) + { + CHalfLifeMultiplay::PlayerKilled( pVictim, pKiller, pInflictor ); + RecountTeams(); + } +} + + +//========================================================= +// IsTeamplay +//========================================================= +BOOL CHalfLifeTeamplay::IsTeamplay( void ) +{ + return TRUE; +} + +BOOL CHalfLifeTeamplay::FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker ) +{ + if ( pAttacker && PlayerRelationship( pPlayer, pAttacker ) == GR_TEAMMATE ) + { + // my teammate hit me. + if( (CVAR_GET_FLOAT( "mp_friendlyfire" ) == 0) && (pAttacker != pPlayer) ) + { + // friendly fire is off, and this hit came from someone other than myself, then don't get hurt + return FALSE; + } + } + + return CHalfLifeMultiplay::FPlayerCanTakeDamage( pPlayer, pAttacker ); +} + +//========================================================= +//========================================================= +int CHalfLifeTeamplay::PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ) +{ + // half life multiplay has a simple concept of Player Relationships. + // you are either on another player's team, or you are not. + if ( !pPlayer || !pTarget || !pTarget->IsPlayer() ) + return GR_NOTTEAMMATE; + + if ( (*GetTeamID(pPlayer) != '\0') && (*GetTeamID(pTarget) != '\0') && !stricmp( GetTeamID(pPlayer), GetTeamID(pTarget) ) ) + { + return GR_TEAMMATE; + } + + return GR_NOTTEAMMATE; +} + +//========================================================= +//========================================================= +BOOL CHalfLifeTeamplay::ShouldAutoAim( CBasePlayer *pPlayer, edict_t *target ) +{ + // always autoaim, unless target is a teammate + CBaseEntity *pTgt = CBaseEntity::Instance( target ); + if ( pTgt && pTgt->IsPlayer() ) + { + if ( PlayerRelationship( pPlayer, pTgt ) == GR_TEAMMATE ) + return FALSE; // don't autoaim at teammates + } + + return CHalfLifeMultiplay::ShouldAutoAim( pPlayer, target ); +} + +//========================================================= +//========================================================= +int CHalfLifeTeamplay::IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ) +{ + if ( !pKilled ) + return 0; + + if ( !pAttacker ) + return 1; + + if ( pAttacker != pKilled && PlayerRelationship( pAttacker, pKilled ) == GR_TEAMMATE ) + return -1; + + return 1; +} + +//========================================================= +//========================================================= +const char *CHalfLifeTeamplay::GetTeamID( CBaseEntity *pEntity ) +{ + if ( pEntity == NULL || pEntity->pev == NULL ) + return ""; + + // return their team name + return pEntity->TeamID(); +} + + +int CHalfLifeTeamplay::GetTeamIndex( const char *pTeamName ) +{ + if ( pTeamName && *pTeamName != 0 ) + { + // try to find existing team + for ( int tm = 0; tm < num_teams; tm++ ) + { + if ( !stricmp( team_names[tm], pTeamName ) ) + return tm; + } + } + + return -1; // No match +} + + +const char *CHalfLifeTeamplay::GetIndexedTeamName( int teamIndex ) +{ + if ( teamIndex < 0 || teamIndex >= num_teams ) + return ""; + + return team_names[ teamIndex ]; +} + + +BOOL CHalfLifeTeamplay::IsValidTeam( const char *pTeamName ) +{ + if ( !m_teamLimit ) // Any team is valid if the teamlist isn't set + return TRUE; + + return ( GetTeamIndex( pTeamName ) != -1 ) ? TRUE : FALSE; +} + +const char *CHalfLifeTeamplay::TeamWithFewestPlayers( void ) +{ + int i; + int minPlayers = MAX_TEAMS; + int teamCount[ MAX_TEAMS ]; + char *pTeamName = NULL; + + memset( teamCount, 0, MAX_TEAMS * sizeof(int) ); + + // loop through all clients, count number of players on each team + for ( i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *plr = UTIL_PlayerByIndex( i ); + + if ( plr ) + { + int team = GetTeamIndex( plr->TeamID() ); + if ( team >= 0 ) + teamCount[team] ++; + } + } + + // Find team with least players + for ( i = 0; i < num_teams; i++ ) + { + if ( teamCount[i] < minPlayers ) + { + minPlayers = teamCount[i]; + pTeamName = team_names[i]; + } + } + + return pTeamName; +} + + +//========================================================= +//========================================================= +void CHalfLifeTeamplay::RecountTeams( bool bResendInfo ) +{ + char *pName; + char teamlist[TEAMPLAY_TEAMLISTLENGTH]; + + // loop through all teams, recounting everything + num_teams = 0; + + // Copy all of the teams from the teamlist + // make a copy because strtok is destructive + strcpy( teamlist, m_szTeamList ); + pName = teamlist; + pName = strtok( pName, ";" ); + while ( pName != NULL && *pName ) + { + if ( GetTeamIndex( pName ) < 0 ) + { + strcpy( team_names[num_teams], pName ); + num_teams++; + } + pName = strtok( NULL, ";" ); + } + + if ( num_teams < 2 ) + { + num_teams = 0; + m_teamLimit = FALSE; + } + + // Sanity check + memset( team_scores, 0, sizeof(team_scores) ); + + // loop through all clients + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *plr = UTIL_PlayerByIndex( i ); + + if ( plr ) + { + const char *pTeamName = plr->TeamID(); + // try add to existing team + int tm = GetTeamIndex( pTeamName ); + + if ( tm < 0 ) // no team match found + { + if ( !m_teamLimit ) + { + // add to new team + tm = num_teams; + num_teams++; + team_scores[tm] = 0; + strncpy( team_names[tm], pTeamName, MAX_TEAMNAME_LENGTH ); + } + } + + if ( tm >= 0 ) + { + team_scores[tm] += plr->pev->frags; + } + + if ( bResendInfo ) //Someone's info changed, let's send the team info again. + { + if ( plr && IsValidTeam( plr->TeamID() ) ) + { + MESSAGE_BEGIN( MSG_ALL, gmsgTeamInfo, NULL ); + WRITE_BYTE( plr->entindex() ); + WRITE_STRING( plr->TeamID() ); + MESSAGE_END(); + } + } + } + } +} diff --git a/server/game/teamplay_gamerules.h b/server/game/teamplay_gamerules.h new file mode 100644 index 00000000..529e22bd --- /dev/null +++ b/server/game/teamplay_gamerules.h @@ -0,0 +1,56 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// +// teamplay_gamerules.h +// + +#define MAX_TEAMNAME_LENGTH 16 +#define MAX_TEAMS 32 + +#define TEAMPLAY_TEAMLISTLENGTH MAX_TEAMS*MAX_TEAMNAME_LENGTH + +class CHalfLifeTeamplay : public CHalfLifeMultiplay +{ +public: + CHalfLifeTeamplay(); + + virtual BOOL ClientCommand( CBasePlayer *pPlayer, const char *pcmd ); + virtual void ClientUserInfoChanged( CBasePlayer *pPlayer, char *infobuffer ); + virtual BOOL IsTeamplay( void ); + virtual BOOL FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker ); + virtual int PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget ); + virtual const char *GetTeamID( CBaseEntity *pEntity ); + virtual BOOL ShouldAutoAim( CBasePlayer *pPlayer, edict_t *target ); + virtual int IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled ); + virtual void InitHUD( CBasePlayer *pl ); + virtual void DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pevInflictor ); + virtual void UpdateGameMode( CBasePlayer *pPlayer ); // the client needs to be informed of the current game mode + virtual void PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor ); + virtual void Think ( void ); + virtual int GetTeamIndex( const char *pTeamName ); + virtual const char *GetIndexedTeamName( int teamIndex ); + virtual BOOL IsValidTeam( const char *pTeamName ); + const char *SetDefaultPlayerTeam( CBasePlayer *pPlayer ); + virtual void ChangePlayerTeam( CBasePlayer *pPlayer, const char *pTeamName, BOOL bKill, BOOL bGib ); + +private: + void RecountTeams( bool bResendInfo = FALSE ); + const char *TeamWithFewestPlayers( void ); + + BOOL m_DisableDeathMessages; + BOOL m_DisableDeathPenalty; + BOOL m_teamLimit; // This means the server set only some teams as valid + char m_szTeamList[TEAMPLAY_TEAMLISTLENGTH]; +}; diff --git a/server/global/bullets.h b/server/global/bullets.h new file mode 100644 index 00000000..acf2bfc0 --- /dev/null +++ b/server/global/bullets.h @@ -0,0 +1,22 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// bullets.h - shared bullets enum +//======================================================================= +#ifndef BULLETS_H +#define BULLETS_H + +// bullet types +typedef enum +{ + BULLET_NONE = 0, + BULLET_9MM, // glock + BULLET_MP5, // mp5 + BULLET_357, // python + BULLET_12MM, // sentry turret + BULLET_556, // m249 bullet + BULLET_762, // sniper bullet + BULLET_BUCKSHOT, // shotgun + BULLET_CROWBAR, // crowbar swipe +} Bullet; + +#endif //BULLETS_H \ No newline at end of file diff --git a/server/global/client.cpp b/server/global/client.cpp new file mode 100644 index 00000000..57d67e13 --- /dev/null +++ b/server/global/client.cpp @@ -0,0 +1,1204 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +// Robin, 4-22-98: Moved set_suicide_frame() here from player.cpp to allow us to +// have one without a hardcoded player.mdl in tf_client.cpp + +/* + +===== client.cpp ======================================================== + + client/server game specific stuff + +*/ + +#include + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "saverestore.h" +#include "shake.h" +#include "player.h" +#include "spectator.h" +#include "client.h" +#include "gamerules.h" +#include "game.h" +#include "soundent.h" +#include "baseweapon.h" +#include "defaults.h" +#include "decals.h" +#include "nodes.h" + +extern DLL_GLOBAL ULONG g_ulModelIndexPlayer; +extern DLL_GLOBAL BOOL g_fGameOver; +extern DLL_GLOBAL ULONG g_ulFrameCount; +extern CBaseEntity *g_pLastSpawn; +extern CSoundEnt *pSoundEnt; +DLL_GLOBAL edict_t *g_pBodyQueueHead; +DLL_GLOBAL int g_serveractive = 0; +BOOL MSGSended; +BOOL NewLevel = FALSE; +float MsgDelay; +void LinkUserMessages( void ); +char text[128]; +extern int gmsgSayText; +extern int gmsgHUDColor; +extern int gmsgCamData; // for trigger_viewset +extern int g_teamplay; + +//messages affect only player +typedef struct _SelAmmo +{ + BYTE Ammo1Type; + BYTE Ammo1; + BYTE Ammo2Type; + BYTE Ammo2; +} SelAmmo; +int gmsgShake = 0; +int gmsgFade = 0; +int gmsgSelAmmo = 0; +int gmsgFlashlight = 0; +int gmsgFlashBattery = 0; +int gmsgResetHUD = 0; +int gmsgInitHUD = 0; +int gmsgHUDColor = 0; +int gmsgCurWeapon = 0; +int gmsgHealth = 0; +int gmsgDamage = 0; +int gmsgBattery = 0; +int gmsgTrain = 0; +int gmsgWeaponList = 0; +int gmsgAmmoX = 0; +int gmsgDeathMsg = 0; +int gmsgScoreInfo = 0; +int gmsgTeamInfo = 0; +int gmsgTeamScore = 0; +int gmsgGameMode = 0; +int gmsgMOTD = 0; +int gmsgServerName = 0; +int gmsgAmmoPickup = 0; +int gmsgWeapPickup = 0; +int gmsgItemPickup = 0; +int gmsgHideWeapon = 0; +int gmsgSetCurWeap = 0; +int gmsgSayText = 0; +int gmsgTextMsg = 0; +int gmsgSetFOV = 0; +int gmsgShowMenu = 0; +int gmsgGeigerRange = 0; +int gmsgTeamNames = 0; +int gmsgStatusText = 0; +int gmsgStatusValue = 0; +int gmsgSetBody = 0; +int gmsgSetSkin = 0; +int gmsgZoomHUD = 0; +int gmsgWarHUD = 0; + +//entity messages +int gmsgHudText = 0; +int gmsgStatusIcon = 0; +int gmsgSetFog = 0; +int gmsgSetSky = 0; +int gmsgParticle = 0; +int gmsgBeams = 0; +int gmsgFsound = 0; +int gmsgShowGameTitle = 0; +int gmsgCamData; +int gmsgRainData = 0; +int gmsgAddMirror = 0; +int gmsgAddScreen = 0; +int gmsgAddPortal = 0; + +/* + * used by kill command and disconnect command + * ROBIN: Moved here from player.cpp, to allow multiple player models + */ +void set_suicide_frame(entvars_t* pev) +{ + if (!FStrEq(STRING(pev->model), "models/player.mdl")) + return; // allready gibbed + +// pev->frame = $deatha11; + pev->solid = SOLID_NOT; + pev->movetype = MOVETYPE_TOSS; + pev->deadflag = DEAD_DEAD; + pev->nextthink = -1; +} + +void InitBodyQue(void) +{ + string_t istrClassname = MAKE_STRING("bodyque"); + + g_pBodyQueueHead = CREATE_NAMED_ENTITY( istrClassname ); + entvars_t *pev = VARS(g_pBodyQueueHead); + + // Reserve 3 more slots for dead bodies + for ( int i = 0; i < 3; i++ ) + { + pev->owner = CREATE_NAMED_ENTITY( istrClassname ); + pev = VARS(pev->owner); + } + + pev->owner = g_pBodyQueueHead; +} + +// +// make a body que entry for the given ent so the ent can be respawned elsewhere +// +// GLOBALS ASSUMED SET: g_eoBodyQueueHead +// +void CopyToBodyQue(entvars_t *pev) +{ + if (pev->effects & EF_NODRAW) + return; + + entvars_t *pevHead = VARS(g_pBodyQueueHead); + + pevHead->angles = pev->angles; + pevHead->model = pev->model; + pevHead->modelindex = pev->modelindex; + pevHead->frame = pev->frame; + pevHead->colormap = pev->colormap; + pevHead->movetype = MOVETYPE_TOSS; + pevHead->velocity = pev->velocity; + pevHead->flags = 0; + pevHead->deadflag = pev->deadflag; + pevHead->renderfx = kRenderFxDeadPlayer; + pevHead->renderamt = ENTINDEX( ENT( pev ) ); + + pevHead->effects = pev->effects | EF_NOINTERP; + //pevHead->goalstarttime = pev->goalstarttime; + //pevHead->goalframe = pev->goalframe; + //pevHead->goalendtime = pev->goalendtime ; + + pevHead->sequence = pev->sequence; + pevHead->animtime = pev->animtime; + + UTIL_SetEdictOrigin(g_pBodyQueueHead, pev->origin); + UTIL_SetSize(pevHead, pev->mins, pev->maxs); + g_pBodyQueueHead = pevHead->owner; +} + + +/* +=========== +ClientConnect + +called when a player connects to a server +============ +*/ +BOOL ClientConnect( edict_t *pEntity, const char *userinfo ) +{ + return g_pGameRules->ClientConnected( pEntity, userinfo ); + +// a client connecting during an intermission can cause problems +// if (intermission_running) +// ExitIntermission (); + +} + + +/* +=========== +ClientDisconnect + +called when a player disconnects from a server + +GLOBALS ASSUMED SET: g_fGameOver +============ +*/ +void ClientDisconnect( edict_t *pEntity ) +{ + if (g_fGameOver) + return; + + char text[256]; + sprintf( text, "- %s has left the game\n", STRING(pEntity->v.netname) ); + MESSAGE_BEGIN( MSG_ALL, gmsgSayText, NULL ); + WRITE_BYTE( ENTINDEX(pEntity) ); + WRITE_STRING( text ); + MESSAGE_END(); + + CSound *pSound; + pSound = CSoundEnt::SoundPointerForIndex( CSoundEnt::ClientSoundIndex( pEntity ) ); + { + // since this client isn't around to think anymore, reset their sound. + if ( pSound ) + { + pSound->Reset(); + } + } + +// since the edict doesn't get deleted, fix it so it doesn't interfere. + pEntity->v.takedamage = DAMAGE_NO;// don't attract autoaim + pEntity->v.solid = SOLID_NOT;// nonsolid + UTIL_SetEdictOrigin ( pEntity, pEntity->v.origin ); + + g_pGameRules->ClientDisconnected( pEntity ); +} + + +// called by ClientKill and DeadThink +void respawn(entvars_t* pev, BOOL fCopyCorpse) +{ + if (gpGlobals->coop || gpGlobals->deathmatch) + { + if ( fCopyCorpse ) + { + // make a copy of the dead body for appearances sake + CopyToBodyQue(pev); + } + + // respawn player + GetClassPtr( (CBasePlayer *)pev)->Spawn( ); + } + else + { // restart the entire server + SERVER_COMMAND("reload\n"); + } +} + +/* +============ +ClientKill + +Player entered the suicide command + +GLOBALS ASSUMED SET: g_ulModelIndexPlayer +============ +*/ +void ClientKill( edict_t *pEntity ) +{ + entvars_t *pev = &pEntity->v; + + CBasePlayer *pl = (CBasePlayer*) CBasePlayer::Instance( pev ); + + if ( pl->m_fNextSuicideTime > gpGlobals->time ) + return; // prevent suiciding too ofter + + pl->m_fNextSuicideTime = gpGlobals->time + 1; // don't let them suicide for 5 seconds after suiciding + + // have the player kill themself + pev->health = 0; + pl->Killed( pev, GIB_NEVER ); + +// pev->modelindex = g_ulModelIndexPlayer; +// pev->frags -= 2; // extra penalty +// respawn( pev ); +} + +/* +=========== +ClientPutInServer + +called each time a player is spawned +============ +*/ +void ClientPutInServer( edict_t *pEntity ) +{ + CBasePlayer *pPlayer; + + entvars_t *pev = &pEntity->v; + + pPlayer = GetClassPtr((CBasePlayer *)pev); + pPlayer->SetCustomDecalFrames(-1); // Assume none; + + // Allocate a CBasePlayer for pev, and call spawn + pPlayer->Spawn() ; + + // Reset interpolation during first frame + pPlayer->pev->effects |= EF_NOINTERP; +} + +//// HOST_SAY +// String comes in as +// say blah blah blah +// or as +// blah blah blah +// +void Host_Say( edict_t *pEntity, int teamonly ) +{ + CBasePlayer *client; + int j; + char *p; + char text[128]; + char szTemp[256]; + const char *cpSay = "say"; + const char *cpSayTeam = "say_team"; + const char *pcmd = CMD_ARGV(0); + + // We can get a raw string now, without the "say " prepended + if ( CMD_ARGC() == 0 ) + return; + + entvars_t *pev = &pEntity->v; + CBasePlayer* player = GetClassPtr((CBasePlayer *)pev); + + //Not yet. + if ( player->m_flNextChatTime > gpGlobals->time ) + return; + + if ( !stricmp( pcmd, cpSay) || !stricmp( pcmd, cpSayTeam ) ) + { + if ( CMD_ARGC() >= 2 ) + { + p = (char *)CMD_ARGS(); + } + else + { + // say with a blank message, nothing to do + return; + } + } + else // Raw text, need to prepend argv[0] + { + if ( CMD_ARGC() >= 2 ) + { + sprintf( szTemp, "%s %s", ( char * )pcmd, (char *)CMD_ARGS() ); + } + else + { + // Just a one word command, use the first word...sigh + sprintf( szTemp, "%s", ( char * )pcmd ); + } + p = szTemp; + } + +// remove quotes if present + if (*p == '"') + { + p++; + p[strlen(p)-1] = 0; + } + +// make sure the text has content + char *pc; + for ( pc = p; pc != NULL && *pc != 0; pc++ ) + { + if ( isprint( *pc ) && !isspace( *pc ) ) + { + pc = NULL; // we've found an alphanumeric character, so text is valid + break; + } + } + if ( pc != NULL ) + return; // no character found, so say nothing + +// turn on color set 2 (color on, no sound) + if ( teamonly ) + sprintf( text, "%c(TEAM) %s: ", 2, STRING( pEntity->v.netname ) ); + else + sprintf( text, "%c%s: ", 2, STRING( pEntity->v.netname ) ); + + j = sizeof(text) - 2 - strlen(text); // -2 for /n and null terminator + if ( (int)strlen(p) > j ) + p[j] = 0; + + strcat( text, p ); + strcat( text, "\n" ); + + + player->m_flNextChatTime = gpGlobals->time + CHAT_INTERVAL; + + // loop through all players + // Start with the first player. + // This may return the world in single player if the client types something between levels or during spawn + // so check it, or it will infinite loop + + client = NULL; + while ( ((client = (CBasePlayer*)UTIL_FindEntityByClassname( client, "player" )) != NULL) && (!FNullEnt(client->edict())) ) + { + if ( !client->pev ) + continue; + + if ( client->edict() == pEntity ) + continue; + + if ( !(client->IsNetClient()) ) // Not a client ? (should never be true) + continue; + + if ( teamonly && g_pGameRules->PlayerRelationship(client, CBaseEntity::Instance(pEntity)) != GR_TEAMMATE ) + continue; + + MESSAGE_BEGIN( MSG_ONE, gmsgSayText, NULL, client->pev ); + WRITE_BYTE( ENTINDEX(pEntity) ); + WRITE_STRING( text ); + MESSAGE_END(); + + } + + // print to the sending client + MESSAGE_BEGIN( MSG_ONE, gmsgSayText, NULL, &pEntity->v ); + WRITE_BYTE( ENTINDEX(pEntity) ); + WRITE_STRING( text ); + MESSAGE_END(); + + // echo to server console + g_engfuncs.pfnServerPrint( text ); + + char * temp; + if ( teamonly ) + temp = "say_team"; + else + temp = "say"; +} + + +/* +=========== +ClientCommand +called each time a player uses a "cmd" command +============ +*/ +extern float g_flWeaponCheat; + +// Use CMD_ARGV, CMD_ARGV, and CMD_ARGC to get pointers the character string command. +void ClientCommand( edict_t *pEntity ) +{ + const char *pcmd = CMD_ARGV(0); + const char *pstr; + + // Is the client spawned yet? + if( !pEntity->pvServerData ) + return; + + entvars_t *pev = &pEntity->v; + + if( FStrEq( pcmd, "noclip" )) + { + if( pEntity->v.movetype == MOVETYPE_WALK ) + { + pEntity->v.movetype = MOVETYPE_NOCLIP; + Msg( "noclip on\n" ); + } + else + { + pEntity->v.movetype = MOVETYPE_WALK; + Msg( "noclip off\n" ); + } + } + else if ( FStrEq( pcmd, "hud_color") ) //LRC + { + if (CMD_ARGC() == 4) + { + int col = (atoi(CMD_ARGV(1)) & 255) << 16; + col += (atoi(CMD_ARGV(2)) & 255) << 8; + col += (atoi(CMD_ARGV(3)) & 255); + MESSAGE_BEGIN( MSG_ONE, gmsgHUDColor, NULL, &pEntity->v ); + WRITE_LONG(col); + MESSAGE_END(); + } + else + { + ALERT(at_console, "Syntax: hud_color RRR GGG BBB\n"); + } + } + else if ( FStrEq(pcmd, "fire") ) //LRC - trigger entities manually + { + if (g_flWeaponCheat) + { + CBaseEntity *pPlayer = CBaseEntity::Instance(pEntity); + if (CMD_ARGC() > 1) + { + UTIL_FireTargets(CMD_ARGV(1), pPlayer, pPlayer, USE_TOGGLE); + } + else + { + TraceResult tr; + UTIL_MakeVectors(pev->v_angle); + UTIL_TraceLine( + pev->origin + pev->view_ofs, + pev->origin + pev->view_ofs + gpGlobals->v_forward * 1000, + dont_ignore_monsters, pEntity, &tr + ); + + if (tr.pHit) + { + CBaseEntity *pHitEnt = CBaseEntity::Instance(tr.pHit); + if (pHitEnt) + { + pHitEnt->Use(pPlayer, pPlayer, USE_TOGGLE, 0); + ALERT(at_aiconsole, "Fired %s \"%s\"\n", STRING(pHitEnt->pev->classname), STRING(pHitEnt->pev->targetname)); + } + } + } + } + } + else if ( FStrEq(pcmd, "debug") ) + { + Msg("debug called\n" ); + if (g_flWeaponCheat) + { + CBaseEntity *pPlayer = CBaseEntity::Instance(pEntity); + if (CMD_ARGC() > 1) + { + UTIL_FireTargets(CMD_ARGV(1), pPlayer, pPlayer, USE_SHOWINFO); + } + else + { + TraceResult tr; + UTIL_MakeVectors(pev->v_angle); + UTIL_TraceLine( + pev->origin + pev->view_ofs, + pev->origin + pev->view_ofs + gpGlobals->v_forward * 1000, + dont_ignore_monsters, pEntity, &tr + ); + + if (tr.pHit) + { + CBaseEntity *pHitEnt = CBaseEntity::Instance(tr.pHit); + if (pHitEnt) + { + pHitEnt->Use(pPlayer, pPlayer, USE_SHOWINFO, 0); + ALERT(at_aiconsole, "Fired %s \"%s\"\n", STRING(pHitEnt->pev->classname), STRING(pHitEnt->pev->targetname)); + } + } + } + } + } + else if ( FStrEq(pcmd, "say" ) ) + { + Host_Say( pEntity, 0 ); + } + else if ( FStrEq(pcmd, "game_over" ) ) + { + if(IsMultiplayer()) g_pGameRules->EndMultiplayerGame(); //loading next map + // FIXME: return to main menu here + } + else if ( FStrEq(pcmd, "gametitle" ) ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgShowGameTitle, NULL, ENT(pev) ); + WRITE_BYTE( 0 ); + MESSAGE_END(); + } + else if ( FStrEq(pcmd, "intermission" ) ) + { + MESSAGE_BEGIN(MSG_ONE, SVC_INTERMISSION, NULL, ENT(pev)); + MESSAGE_END(); + } + else if ( FStrEq(pcmd, "fullupdate" ) ) + { + GetClassPtr((CBasePlayer *)pev)->ForceClientDllUpdate(); + } + else if ( FStrEq(pcmd, "give" ) ) + { + if ( g_flWeaponCheat != 0.0) + { + int iszItem = ALLOC_STRING( CMD_ARGV(1) ); // Make a copy of the classname + GetClassPtr((CBasePlayer *)pev)->GiveNamedItem( STRING(iszItem) ); + } + } + + else if ( FStrEq(pcmd, "drop" ) ) + { + // player is dropping an item. + GetClassPtr((CBasePlayer *)pev)->DropPlayerItem((char *)CMD_ARGV(1)); + } + else if ( FStrEq(pcmd, "fov" ) ) + { + if ( g_flWeaponCheat && CMD_ARGC() > 1) + { + GetClassPtr((CBasePlayer *)pev)->m_iFOV = atoi( CMD_ARGV(1) ); + } + else + { + CLIENT_PRINTF( pEntity, HUD_PRINTCONSOLE, UTIL_VarArgs( "\"fov\" is \"%d\"\n", (int)GetClassPtr((CBasePlayer *)pev)->m_iFOV ) ); + } + } + else if ( FStrEq(pcmd, "use" ) ) + { + GetClassPtr((CBasePlayer *)pev)->SelectItem((char *)CMD_ARGV(1)); + } + else if (((pstr = strstr(pcmd, "weapon_")) != NULL) && (pstr == pcmd)) + { + GetClassPtr((CBasePlayer *)pev)->SelectItem(pcmd); + } + else if( FStrEq( pcmd, "lastinv" )) + { + GetClassPtr((CBasePlayer *)pev)->SelectLastItem(); + } + else if( FStrEq( pcmd, "spectate" )) // added for proxy support + { + CBasePlayer * pPlayer = GetClassPtr((CBasePlayer *)pev); + + edict_t *pentSpawnSpot = g_pGameRules->GetPlayerSpawnSpot( pPlayer ); + pPlayer->StartObserver( pev->origin, VARS(pentSpawnSpot)->angles); + } + else if ( g_pGameRules->ClientCommand( GetClassPtr((CBasePlayer *)pev), pcmd ) ) + { + // MenuSelect returns true only if the command is properly handled, so don't print a warning + } + else + { + // tell the user they entered an unknown command + char command[128]; + + // check the length of the command (prevents crash) + // max total length is 192 ...and we're adding a string below ("Unknown command: %s\n") + strncpy( command, pcmd, 127 ); + command[127] = '\0'; + + // tell the user they entered an unknown command + ClientPrint( &pEntity->v, HUD_PRINTCONSOLE, UTIL_VarArgs( "Unknown command: %s\n", command ) ); + } +} + + +/* +======================== +ClientUserInfoChanged + +called after the player changes +userinfo - gives dll a chance to modify it before +it gets sent into the rest of the engine. +======================== +*/ +void ClientUserInfoChanged( edict_t *pEntity, char *infobuffer ) +{ + // Is the client spawned yet? + if ( !pEntity->pvServerData ) + return; + + // msg everyone if someone changes their name, and it isn't the first time (changing no name to current name) + if ( pEntity->v.netname && STRING(pEntity->v.netname)[0] != 0 && !FStrEq( STRING(pEntity->v.netname), g_engfuncs.pfnInfoKeyValue( infobuffer, "name" )) ) + { + char sName[256]; + char *pName = g_engfuncs.pfnInfoKeyValue( infobuffer, "name" ); + strncpy( sName, pName, sizeof(sName) - 1 ); + sName[ sizeof(sName) - 1 ] = '\0'; + + // First parse the name and remove any %'s + for ( char *pApersand = sName; pApersand != NULL && *pApersand != 0; pApersand++ ) + { + // Replace it with a space + if ( *pApersand == '%' ) + *pApersand = ' '; + } + + // Set the name + g_engfuncs.pfnSetClientKeyValue( ENTINDEX(pEntity), infobuffer, "name", sName ); + + char text[256]; + sprintf( text, "* %s changed name to %s\n", STRING(pEntity->v.netname), g_engfuncs.pfnInfoKeyValue( infobuffer, "name" ) ); + MESSAGE_BEGIN( MSG_ALL, gmsgSayText, NULL ); + WRITE_BYTE( ENTINDEX(pEntity) ); + WRITE_STRING( text ); + MESSAGE_END(); + } + g_pGameRules->ClientUserInfoChanged( GetClassPtr((CBasePlayer *)&pEntity->v), infobuffer ); +} + +void ServerDeactivate( void ) +{ + // make sure they reinitialise the World in the next server + g_pWorld = NULL; + + // It's possible that the engine will call this function more times than is necessary + // Therefore, only run it one time for each call to ServerActivate + if ( g_serveractive != 1 ) + { + return; + } + + g_serveractive = 0; + + // Peform any shutdown operations here... + // +} + +void ServerActivate( edict_t *pEdictList, int edictCount, int clientMax ) +{ + int i; + CBaseEntity *pClass; + + // Every call to ServerActivate should be matched by a call to ServerDeactivate + g_serveractive = 1; + // Clients have not been initialized yet + for ( i = 0; i < edictCount; i++ ) + { + if ( pEdictList[i].free ) + continue; + + // Clients aren't necessarily initialized until ClientPutInServer() + if ( i < clientMax || !pEdictList[i].pvServerData ) + continue; + + pClass = CBaseEntity::Instance( &pEdictList[i] ); + // Activate this entity if it's got a class & isn't dormant + if ( pClass && !(pClass->pev->flags & FL_DORMANT) ) + { + pClass->SetupPhysics(); + pClass->Activate(); + } + else Msg( "Can't instance %s\n", STRING(pEdictList[i].v.classname) ); + } + + // Link user messages here to make sure first client can get them... + NewLevel = FALSE; + LinkUserMessages(); +} + +// a cached version of gpGlobals->frametime. The engine sets frametime to 0 if the player is frozen... so we just cache it in prethink, +// allowing it to be restored later and used by CheckDesiredList. +float cached_frametime = 0.0f; + +/* +================ +PlayerPreThink + +Called every frame before physics are run +================ +*/ +void PlayerPreThink( edict_t *pEntity ) +{ + entvars_t *pev = &pEntity->v; + CBasePlayer *pPlayer = (CBasePlayer *)GET_PRIVATE(pEntity); + + if (pPlayer)pPlayer->PreThink( ); + cached_frametime = gpGlobals->frametime; +} + +/* +================ +PlayerPostThink + +Called every frame after physics are run +================ +*/ +void PlayerPostThink( edict_t *pEntity ) +{ + entvars_t *pev = &pEntity->v; + CBasePlayer *pPlayer = (CBasePlayer *)GET_PRIVATE(pEntity); + + if (pPlayer)pPlayer->PostThink( ); + + // use the old frametime, even if the engine has reset it + gpGlobals->frametime = cached_frametime; +} + +void ParmsChangeLevel( void ) +{ + // retrieve the pointer to the save data + SAVERESTOREDATA *pSaveData = (SAVERESTOREDATA *)gpGlobals->pSaveData; + + if ( pSaveData ) + pSaveData->connectionCount = BuildChangeList( pSaveData->levelList, MAX_LEVEL_CONNECTIONS ); +} + +//======================================================================= +// Build ChangeLevel List +//======================================================================= +int AddTransitionToList( LEVELLIST *pLevelList, int listCount, const char *pMapName, const char *pLandmarkName, edict_t *pentLandmark ) +{ + int i; + + if ( !pLevelList || !pMapName || !pLandmarkName || !pentLandmark ) return 0; + + for ( i = 0; i < listCount; i++ ) + { + if ( pLevelList[i].pentLandmark == pentLandmark && strcmp( pLevelList[i].mapName, pMapName ) == 0 ) + return 0; + } + strcpy( pLevelList[listCount].mapName, pMapName ); + strcpy( pLevelList[listCount].landmarkName, pLandmarkName ); + pLevelList[listCount].pentLandmark = pentLandmark; + pLevelList[listCount].vecLandmarkOrigin = VARS(pentLandmark)->origin; + + return 1; +} + +int BuildChangeList( LEVELLIST *pLevelList, int maxList ) +{ + edict_t *pentLandmark; + int i, count; + + count = 0; + + // Find all of the possible level changes on this BSP + CBaseEntity *pChangelevel = UTIL_FindEntityByClassname( NULL, "trigger_changelevel" ); + + if ( !pChangelevel ) return NULL; + + while ( pChangelevel ) + { + // Find the corresponding landmark + pentLandmark = UTIL_FindLandmark( pChangelevel->pev->message ); + if ( pentLandmark ) + { + // Build a list of unique transitions + DevMsg("Map name %s, landmark name %s\n", STRING(pChangelevel->pev->netname), STRING(pChangelevel->pev->message)); + if ( AddTransitionToList( pLevelList, count, (char *)STRING(pChangelevel->pev->netname), (char *)STRING(pChangelevel->pev->message), pentLandmark )) + { + count++; + if ( count >= maxList )break;//List is FULL!!! + } + } + pChangelevel = UTIL_FindEntityByClassname( pChangelevel, "trigger_changelevel" ); + } + + if ( gpGlobals->pSaveData && ((SAVERESTOREDATA *)gpGlobals->pSaveData)->pTable ) + { + CSave saveHelper( (SAVERESTOREDATA *)gpGlobals->pSaveData ); + + for ( i = 0; i < count; i++ ) + { + int j, entityCount = 0; + CBaseEntity *pEntList[ MAX_TRANSITION_ENTITY ]; + int entityFlags[ MAX_TRANSITION_ENTITY ]; + + // Follow the linked list of entities in the PVS of the transition landmark + edict_t *pent = UTIL_EntitiesInPVS( pLevelList[i].pentLandmark ); + + // Build a list of valid entities in this linked list (we're going to use pent->v.chain again) + while ( !FNullEnt( pent ) ) + { + CBaseEntity *pEntity = CBaseEntity::Instance(pent); + if ( pEntity ) + { + int caps = pEntity->ObjectCaps(); + if ( !(caps & FCAP_DONT_SAVE) ) + { + int flags = 0; + + // If this entity can be moved or is global, mark it + if ( caps & FCAP_ACROSS_TRANSITION ) flags |= FENTTABLE_MOVEABLE; + if ( pEntity->pev->globalname && !pEntity->IsDormant() ) flags |= FENTTABLE_GLOBAL; + if ( flags ) + { + pEntList[ entityCount ] = pEntity; + entityFlags[ entityCount ] = flags; + entityCount++; + if ( entityCount > MAX_TRANSITION_ENTITY ) Msg("ERROR: Too many entities across a transition!" ); + } + } + } + pent = pent->v.chain; + } + + for ( j = 0; j < entityCount; j++ ) + { + // Check to make sure the entity isn't screened out by a trigger_transition + if ( entityFlags[j] && UTIL_FindTransition( pEntList[j], pLevelList[i].landmarkName ) ) + { + // Mark entity table with 1< gpGlobals->time ) return; + if( plr && !plr->m_fInitHUD && !gInitHUD ) + { + for ( int i = 0; i < gpGlobals->maxEntities; i++, pEdict++ ) + { + if( pEdict->free ) continue; + pClass = CBaseEntity::Instance(pEdict); + if ( pClass && !(pClass->pev->flags & FL_DORMANT) ) + { + pClass->PostActivate(); + } + } + MSGSended = TRUE;//messages sucessfully sended + } +} + +// +// GLOBALS ASSUMED SET: g_ulFrameCount +// +void StartFrame( void ) +{ + //Msg(" frametime %g\n", gpGlobals->frametime ); + if ( g_pGameRules )g_pGameRules->Think(); + if ( g_fGameOver ) return; + + gpGlobals->teamplay = (CVAR_GET_FLOAT( "mp_teamplay" ) == 1.0f) ? TRUE : FALSE; + g_ulFrameCount++; + + ServerPostActivate();//called once + PhysicsFrame(); + PhysicsPostFrame(); + +} + +void EndFrame( void ) +{ +} + +void ClientPrecache( void ) +{ + // Material System!!! move this in next versions + PRECACHE_SOUND("player/pl_step1.wav"); // walk on concrete + PRECACHE_SOUND("player/pl_step2.wav"); + PRECACHE_SOUND("player/pl_step3.wav"); + PRECACHE_SOUND("player/pl_step4.wav"); + + PRECACHE_SOUND("common/npc_step1.wav"); // NPC walk on concrete + PRECACHE_SOUND("common/npc_step2.wav"); + PRECACHE_SOUND("common/npc_step3.wav"); + PRECACHE_SOUND("common/npc_step4.wav"); + + PRECACHE_SOUND("player/pl_metal1.wav"); // walk on metal + PRECACHE_SOUND("player/pl_metal2.wav"); + PRECACHE_SOUND("player/pl_metal3.wav"); + PRECACHE_SOUND("player/pl_metal4.wav"); + + PRECACHE_SOUND("player/pl_dirt1.wav"); // walk on dirt + PRECACHE_SOUND("player/pl_dirt2.wav"); + PRECACHE_SOUND("player/pl_dirt3.wav"); + PRECACHE_SOUND("player/pl_dirt4.wav"); + + PRECACHE_SOUND("player/pl_duct1.wav"); // walk in duct + PRECACHE_SOUND("player/pl_duct2.wav"); + PRECACHE_SOUND("player/pl_duct3.wav"); + PRECACHE_SOUND("player/pl_duct4.wav"); + + PRECACHE_SOUND("player/pl_grate1.wav"); // walk on grate + PRECACHE_SOUND("player/pl_grate2.wav"); + PRECACHE_SOUND("player/pl_grate3.wav"); + PRECACHE_SOUND("player/pl_grate4.wav"); + + PRECACHE_SOUND("player/pl_slosh1.wav"); // walk in shallow water + PRECACHE_SOUND("player/pl_slosh2.wav"); + PRECACHE_SOUND("player/pl_slosh3.wav"); + PRECACHE_SOUND("player/pl_slosh4.wav"); + + PRECACHE_SOUND("player/pl_tile1.wav"); // walk on tile + PRECACHE_SOUND("player/pl_tile2.wav"); + PRECACHE_SOUND("player/pl_tile3.wav"); + PRECACHE_SOUND("player/pl_tile4.wav"); + PRECACHE_SOUND("player/pl_tile5.wav"); + + PRECACHE_SOUND("player/pl_swim1.wav"); // breathe bubbles + PRECACHE_SOUND("player/pl_swim2.wav"); + PRECACHE_SOUND("player/pl_swim3.wav"); + PRECACHE_SOUND("player/pl_swim4.wav"); + + PRECACHE_SOUND("player/pl_ladder1.wav"); // climb ladder rung + PRECACHE_SOUND("player/pl_ladder2.wav"); + PRECACHE_SOUND("player/pl_ladder3.wav"); + PRECACHE_SOUND("player/pl_ladder4.wav"); + + PRECACHE_SOUND("player/pl_wade1.wav"); // wade in water + PRECACHE_SOUND("player/pl_wade2.wav"); + PRECACHE_SOUND("player/pl_wade3.wav"); + PRECACHE_SOUND("player/pl_wade4.wav"); + + PRECACHE_SOUND("debris/wood1.wav"); // hit wood texture + PRECACHE_SOUND("debris/wood2.wav"); + PRECACHE_SOUND("debris/wood3.wav"); + + PRECACHE_SOUND("buttons/spark5.wav"); // hit computer texture + PRECACHE_SOUND("buttons/spark6.wav"); + PRECACHE_SOUND("debris/glass1.wav"); + PRECACHE_SOUND("debris/glass2.wav"); + PRECACHE_SOUND("debris/glass3.wav"); +} + +/* +=============== +GetGameDescription + +Returns the descriptive name of this .dll. E.g., Half-Life, or Team Fortress 2 +=============== +*/ +const char *GetGameDescription() +{ + char token[256]; + char szbuffer[128]; + + char *pfile = (char *)LOAD_FILE( "liblist.gam", NULL ); + if(pfile) + { + while ( pfile ) + { + if ( !stricmp( token, "game" )) + { + pfile = COM_ParseFile(pfile, token); + sprintf( szbuffer, "%s ", token ); + strcat( text, szbuffer ); + } + else if ( !stricmp( token, "version" )) + { + pfile = COM_ParseFile(pfile, token); + strcat( text, token ); + } + pfile = COM_ParseFile(pfile, token); + } + COM_FreeFile( pfile ); + return text; + } + return "Half-Life"; +} + +//======================================================================= +// Link User Messages - register new client messages here +//======================================================================= +void LinkUserMessages( void ) +{ + // Already taken care of? + if ( gmsgSelAmmo )return; + + //messages affect only player + gmsgSelAmmo = REG_USER_MSG("SelAmmo", sizeof(SelAmmo)); + gmsgCurWeapon = REG_USER_MSG("CurWeapon", 3); + gmsgGeigerRange = REG_USER_MSG("Geiger", 1); + gmsgFlashlight = REG_USER_MSG("Flashlight", 2); + gmsgFlashBattery = REG_USER_MSG("FlashBat", 1); + gmsgHealth = REG_USER_MSG( "Health", 1 ); + gmsgDamage = REG_USER_MSG( "Damage", 12 ); + gmsgBattery = REG_USER_MSG( "Battery", 2); + gmsgTrain = REG_USER_MSG( "Train", 1); + gmsgSayText = REG_USER_MSG( "SayText", -1 ); + gmsgTextMsg = REG_USER_MSG( "TextMsg", -1 ); + gmsgWeaponList = REG_USER_MSG("WeaponList", -1); + gmsgResetHUD = REG_USER_MSG("ResetHUD", 1); + gmsgInitHUD = REG_USER_MSG("InitHUD", 0 ); + gmsgHUDColor = REG_USER_MSG( "HUDColor", 4 ); + gmsgDeathMsg = REG_USER_MSG( "DeathMsg", -1 ); + gmsgScoreInfo = REG_USER_MSG( "ScoreInfo", 9 ); + gmsgTeamInfo = REG_USER_MSG( "TeamInfo", -1 ); + gmsgTeamScore = REG_USER_MSG( "TeamScore", -1 ); + gmsgGameMode = REG_USER_MSG( "GameMode", 1 ); + gmsgMOTD = REG_USER_MSG( "MOTD", -1 ); + gmsgServerName = REG_USER_MSG( "ServerName", -1 ); + gmsgAmmoPickup = REG_USER_MSG( "AmmoPickup", 2 ); + gmsgWeapPickup = REG_USER_MSG( "WeapPickup", 1 ); + gmsgItemPickup = REG_USER_MSG( "ItemPickup", -1 ); + gmsgHideWeapon = REG_USER_MSG( "HideWeapon", 1 ); + gmsgSetFOV = REG_USER_MSG( "SetFOV", 1 ); + gmsgShowMenu = REG_USER_MSG( "ShowMenu", -1 ); + gmsgShake = REG_USER_MSG("ScreenShake", 13 ); + gmsgFade = REG_USER_MSG("ScreenFade", sizeof(ScreenFade)); + gmsgAmmoX = REG_USER_MSG("AmmoX", 2); + gmsgTeamNames = REG_USER_MSG( "TeamNames", -1 ); + gmsgStatusText = REG_USER_MSG("StatusText", -1); + gmsgStatusValue = REG_USER_MSG("StatusValue", 3); + gmsgSetBody = REG_USER_MSG("SetBody", 1); + gmsgSetSkin = REG_USER_MSG("SetSkin", 1); + gmsgZoomHUD = REG_USER_MSG("ZoomHUD", 1); + gmsgWarHUD = REG_USER_MSG("WarHUD", 1); + + //entity messages + gmsgStatusIcon = REG_USER_MSG("StatusIcon", -1); + gmsgCamData = REG_USER_MSG("CamData", -1); + gmsgFsound = REG_USER_MSG("Fsound", -1); + gmsgRainData = REG_USER_MSG("RainData", 16); + gmsgAddScreen = REG_USER_MSG( "AddScreen", 1); + gmsgAddPortal = REG_USER_MSG( "AddPortal", 1); + gmsgHudText = REG_USER_MSG( "HudText", -1 ); + gmsgShowGameTitle = REG_USER_MSG("GameTitle", 1); + gmsgSetFog = REG_USER_MSG("SetFog", 7 ); + gmsgSetSky = REG_USER_MSG( "SetSky", 7 ); + gmsgParticle = REG_USER_MSG( "Particle", -1); + gmsgBeams = REG_USER_MSG( "Beams", -1 ); + gmsgAddMirror = REG_USER_MSG( "AddMirror", 2); +} + +/* +================================ +AllowLagCompensation + + The game .dll should return 1 if lag compensation should be allowed ( could also just set + the sv_unlag cvar. + Most games right now should return 0, until client-side weapon prediction code is written + and tested for them ( note you can predict weapons, but not do lag compensation, too, + if you want. +================================ +*/ +int AllowLagCompensation( void ) +{ + return 1; +} + + +/* +================ +InitWorld + +Called ever time when worldspawn will precached +================ +*/ + +void InitWorld( void ) +{ + int i; + + CVAR_SET_STRING("sv_gravity", "800"); // 67ft/sec + CVAR_SET_STRING("sv_stepsize", "18"); + CVAR_SET_STRING("room_type", "0");// clear DSP + + g_pLastSpawn = NULL; + + // Set up game rules + if (g_pGameRules) delete g_pGameRules; + g_pGameRules = InstallGameRules( ); + + if ( WorldGraph.m_fGraphPresent && !WorldGraph.m_fGraphPointersSet ) + { + if ( !WorldGraph.FSetGraphPointers()) Msg( "Graph pointers were not set!\n"); + else Msg( "**Graph Pointers Set!\n" ); + } + + UTIL_PrecacheResourse(); //precache all resource + + pSoundEnt = GetClassPtr( ( CSoundEnt *)NULL ); + pSoundEnt->Spawn(); + if ( !pSoundEnt ) Msg( "Couldn create soundent!\n" ); + + InitBodyQue(); + + SENTENCEG_Init(); + TEXTURETYPE_Init(); + + MSGSended = FALSE; + if(IsMultiplayer()) MsgDelay = 0.5 + gpGlobals->time; + else MsgDelay = 0.03 + gpGlobals->time; + + ClientPrecache(); + + // Setup light animation tables. 'a' is total darkness, 'z' is maxbright. + for (i = 0; i <= 13; i++) LIGHT_STYLE(i, (char*)STRING(GetStdLightStyle(i))); + for (i = 0; i < DECAL_COUNT; i++ ) gDecals[i].index = DECAL_INDEX( gDecals[i].name ); + + // init the WorldGraph. + WorldGraph.InitGraph(); + + if ( !WorldGraph.CheckNODFile ( ( char * )STRING( gpGlobals->mapname ))) WorldGraph.AllocNodes(); + else + { + if ( !WorldGraph.FLoadGraph ( (char *)STRING( gpGlobals->mapname )))WorldGraph.AllocNodes(); + else Msg("\n*Graph Loaded!\n" ); + } + + CVAR_SET_FLOAT( "sv_zmax", MAP_SIZE ); +} \ No newline at end of file diff --git a/server/global/client.h b/server/global/client.h new file mode 100644 index 00000000..d6999f4b --- /dev/null +++ b/server/global/client.h @@ -0,0 +1,120 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef CLIENT_H +#define CLIENT_H + +extern void PhysicsPreFrame( void ); +extern void PhysicsFrame( void ); +extern void PhysicsPostFrame( void ); + +extern void respawn( entvars_t* pev, BOOL fCopyCorpse ); +extern BOOL ClientConnect( edict_t *pEntity, const char *userinfo ); +extern void ClientDisconnect( edict_t *pEntity ); +extern void ClientKill( edict_t *pEntity ); +extern void ClientPutInServer( edict_t *pEntity ); +extern void ClientCommand( edict_t *pEntity ); +extern void ClientUserInfoChanged( edict_t *pEntity, char *infobuffer ); +extern void ServerActivate( edict_t *pEdictList, int edictCount, int clientMax ); +extern void ServerDeactivate( void ); +extern void StartFrame( void ); +extern void EndFrame( void ); +extern void PlayerPostThink( edict_t *pEntity ); +extern void PlayerPreThink( edict_t *pEntity ); +extern void ParmsChangeLevel( void ); +extern void InitBodyQue(void); +extern void InitWorld( void ); + +extern const char *GetGameDescription( void ); +extern void SpectatorConnect ( edict_t *pEntity ); +extern void SpectatorDisconnect ( edict_t *pEntity ); +extern void SpectatorThink ( edict_t *pEntity ); + +extern void SetupVisibility( edict_t *pViewEntity, edict_t *pClient, unsigned char **pvs, unsigned char **pas ); +extern void UpdateClientData ( const struct edict_s *ent, int sendweapons, struct clientdata_s *cd ); +extern int AddToFullPack( struct entity_state_s *state, int e, edict_t *ent, edict_t *host, int hostflags, int player, unsigned char *pSet ); +extern void CreateBaseline( int player, int eindex, struct entity_state_s *baseline, struct edict_s *entity, int playermodelindex, vec3_t player_mins, vec3_t player_maxs ); +extern void RegisterEncoders( void ); + +extern int GetWeaponData( struct edict_s *player, struct weapon_data_s *info ); + +extern void CmdStart( const edict_t *player, const struct usercmd_s *cmd, unsigned int random_seed ); +extern void CmdEnd ( const edict_t *player ); + +extern int ConnectionlessPacket( const struct netadr_s *net_from, const char *args, char *response_buffer, int *response_buffer_size ); +extern int GetHullBounds( int hullnumber, float *mins, float *maxs ); +extern void CreateInstancedBaselines ( void ); +extern int InconsistentFile( const edict_t *player, const char *filename, char *disconnect_message ); +extern int AllowLagCompensation( void ); +extern int g_serveractive; + +//messages affect only player +extern int gmsgShake; +extern int gmsgFade; +extern int gmsgSelAmmo; +extern int gmsgFlashlight; +extern int gmsgFlashBattery; +extern int gmsgResetHUD; +extern int gmsgInitHUD; +extern int gmsgHUDColor; +extern int gmsgCurWeapon; +extern int gmsgHealth; +extern int gmsgDamage; +extern int gmsgBattery; +extern int gmsgTrain; +extern int gmsgWeaponList; +extern int gmsgAmmoX; +extern int gmsgDeathMsg; +extern int gmsgScoreInfo; +extern int gmsgTeamInfo; +extern int gmsgTeamScore; +extern int gmsgGameMode; +extern int gmsgMOTD; +extern int gmsgServerName; +extern int gmsgAmmoPickup; +extern int gmsgWeapPickup; +extern int gmsgItemPickup; +extern int gmsgHideWeapon; +extern int gmsgSetCurWeap; +extern int gmsgSayText; +extern int gmsgSetFOV; +extern int gmsgShowMenu; +extern int gmsgGeigerRange; +extern int gmsgTeamNames; +extern int gmsgTextMsg; +extern int gmsgStatusText; +extern int gmsgStatusValue; +extern int gmsgSetBody; +extern int gmsgSetSkin; +extern int gmsgZoomHUD; +extern int gmsgWarHUD; + +//entity messages +extern int gmsgSetFog; +extern int gmsgStatusIcon; +extern int gmsgSetSky; +extern int gmsgParticle; +extern int gmsgFsound; +extern int gmsgCamData; +extern int gmsgRainData; +extern int gmsgHudText; +extern int gmsgShowGameTitle; +extern int gmsgAddScreen; +extern int gmsgAddMirror; +extern int gmsgAddPortal; +extern int gmsgBeams; + +extern BOOL MSGSended; + +#endif // CLIENT_H \ No newline at end of file diff --git a/server/global/decals.cpp b/server/global/decals.cpp new file mode 100644 index 00000000..dd93ef20 --- /dev/null +++ b/server/global/decals.cpp @@ -0,0 +1,51 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#include "decals.h" + +DLL_DECALLIST gDecals[] = +{ + { "{shot1", 0 }, // DECAL_GUNSHOT1 + { "{shot2", 0 }, // DECAL_GUNSHOT2 + { "{shot3", 0 }, // DECAL_GUNSHOT3 + { "{shot4", 0 }, // DECAL_GUNSHOT4 + { "{shot5", 0 }, // DECAL_GUNSHOT5 + { "{lambda01", 0 }, // DECAL_LAMBDA1 + { "{lambda02", 0 }, // DECAL_LAMBDA2 + { "{lambda03", 0 }, // DECAL_LAMBDA3 + { "{lambda04", 0 }, // DECAL_LAMBDA4 + { "{lambda05", 0 }, // DECAL_LAMBDA5 + { "{lambda06", 0 }, // DECAL_LAMBDA6 + { "{scorch1", 0 }, // DECAL_SCORCH1 + { "{scorch2", 0 }, // DECAL_SCORCH2 + { "{blood1", 0 }, // DECAL_BLOOD1 + { "{blood2", 0 }, // DECAL_BLOOD2 + { "{blood3", 0 }, // DECAL_BLOOD3 + { "{blood4", 0 }, // DECAL_BLOOD4 + { "{blood5", 0 }, // DECAL_BLOOD5 + { "{blood6", 0 }, // DECAL_BLOOD6 + { "{yblood1", 0 }, // DECAL_YBLOOD1 + { "{yblood2", 0 }, // DECAL_YBLOOD2 + { "{yblood3", 0 }, // DECAL_YBLOOD3 + { "{yblood4", 0 }, // DECAL_YBLOOD4 + { "{yblood5", 0 }, // DECAL_YBLOOD5 + { "{yblood6", 0 }, // DECAL_YBLOOD6 + { "{break1", 0 }, // DECAL_GLASSBREAK1 + { "{break2", 0 }, // DECAL_GLASSBREAK2 + { "{break3", 0 }, // DECAL_GLASSBREAK3 + { "{bigshot1", 0 }, // DECAL_BIGSHOT1 + { "{bigshot2", 0 }, // DECAL_BIGSHOT2 + { "{bigshot3", 0 }, // DECAL_BIGSHOT3 + { "{bigshot4", 0 }, // DECAL_BIGSHOT4 + { "{bigshot5", 0 }, // DECAL_BIGSHOT5 + { "{spit1", 0 }, // DECAL_SPIT1 + { "{spit2", 0 }, // DECAL_SPIT2 + { "{bproof1", 0 }, // DECAL_BPROOF1 + { "{gargstomp", 0 }, // DECAL_GARGSTOMP1, // Gargantua stomp crack + { "{smscorch1", 0 }, // DECAL_SMALLSCORCH1, // Small scorch mark + { "{smscorch2", 0 }, // DECAL_SMALLSCORCH2, // Small scorch mark + { "{smscorch3", 0 }, // DECAL_SMALLSCORCH3, // Small scorch mark + { "{mommablob", 0 }, // DECAL_MOMMABIRTH // BM Birth spray + { "{mommablob", 0 }, // DECAL_MOMMASPLAT // BM Mortar spray?? need decal +}; \ No newline at end of file diff --git a/server/global/decals.h b/server/global/decals.h new file mode 100644 index 00000000..31442109 --- /dev/null +++ b/server/global/decals.h @@ -0,0 +1,63 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= +#ifndef DECALS_H +#define DECALS_H + +enum decal_e +{ + DECAL_GUNSHOT1 = 0, + DECAL_GUNSHOT2, + DECAL_GUNSHOT3, + DECAL_GUNSHOT4, + DECAL_GUNSHOT5, + DECAL_LAMBDA1, + DECAL_LAMBDA2, + DECAL_LAMBDA3, + DECAL_LAMBDA4, + DECAL_LAMBDA5, + DECAL_LAMBDA6, + DECAL_SCORCH1, + DECAL_SCORCH2, + DECAL_BLOOD1, + DECAL_BLOOD2, + DECAL_BLOOD3, + DECAL_BLOOD4, + DECAL_BLOOD5, + DECAL_BLOOD6, + DECAL_YBLOOD1, + DECAL_YBLOOD2, + DECAL_YBLOOD3, + DECAL_YBLOOD4, + DECAL_YBLOOD5, + DECAL_YBLOOD6, + DECAL_GLASSBREAK1, + DECAL_GLASSBREAK2, + DECAL_GLASSBREAK3, + DECAL_BIGSHOT1, + DECAL_BIGSHOT2, + DECAL_BIGSHOT3, + DECAL_BIGSHOT4, + DECAL_BIGSHOT5, + DECAL_SPIT1, + DECAL_SPIT2, + DECAL_BPROOF1, // Bulletproof glass decal + DECAL_GARGSTOMP1, // Gargantua stomp crack + DECAL_SMALLSCORCH1, // Small scorch mark + DECAL_SMALLSCORCH2, // Small scorch mark + DECAL_SMALLSCORCH3, // Small scorch mark + DECAL_MOMMABIRTH, // Big momma birth splatter + DECAL_MOMMASPLAT, + + DECAL_COUNT, //Must be last in the list! +}; + +typedef struct +{ + char *name; + int index; +} DLL_DECALLIST; + +extern DLL_DECALLIST gDecals[]; + +#endif // DECALS_H \ No newline at end of file diff --git a/server/global/defaults.h b/server/global/defaults.h new file mode 100644 index 00000000..9589a0b0 --- /dev/null +++ b/server/global/defaults.h @@ -0,0 +1,447 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// defaults.h - default global settings for max ammo, +// monster's health, e.t.c +//======================================================================= + +#ifndef DEFAULT_H +#define DEFAULT_H + +//name of directory with mod +#define PITCHMIN 30 //min pitch +#define PITCHMAX 100 //max pitch + +#define MAX_AVELOCITY 4096 +#define MAX_VELOCITY 4096 +#define MAX_TRANSITION_ENTITY 512 +#define MAP_MAX_NAME 32 + +#define MAX_ITEM_TYPES 6 //selection slots +#define MAX_ITEMS 5 //item types + +#define PLAYER_LONGJUMP_SPEED 350 // how fast we longjump + +//========================= +// Debug macros +//========================= +#define SHIFT Msg("\n") +#define ACTION Msg("Action!\n") +#define DEBUGHEAD Msg("======/Xash Debug System/======\nclassname: %s, targetname %s\n", STRING(pev->classname), STRING(pev->targetname)) +#define MSGSTATEHEALTH Msg("State: %s, health %g\n", GetStringForState( GetState()), pev->health ) +#define MSGSTATESTRENGTH Msg("State: %s, strength %g\n", GetStringForState( GetState()), pev->health ) +#define MSGSTATEVOLUME Msg("State: %s, volume %g\n", GetStringForState( GetState()), m_flVolume ) +#define MSGSTATESPEED Msg("State: %s, speed %g\n", GetStringForState( GetState()), pev->speed ) + +//========================= +// CLOCK DEFAULTS +//========================= +#define SECONDS 60 +#define MINUTES 3600 +#define HOURS 43200 + +//========================= +// LIGHT DEFAULTS +//========================= +#define START_SWITCH_STYLE 32 + +//========================= +// WORLD DEFAULTS +//========================= +#define XEN_GRAVITY 0.6 +#define EARTH_GRAVITY 1.0 +#define MAP_SIZE 8192 +#define MAP_HALFSIZE MAP_SIZE / 2 +#define FOG_LIMIT 30000 +#define MAX_GIB_LIFETIME 30 + +//========================= +// Global spawnflag system +//========================= +#define SF_START_ON 0x1 +#define SF_NOTSOLID 0x2 +#define SF_FIREONCE 0x2 +#define SF_NORESPAWN ( 1 << 30 ) +//========================= +// FCAP DEFAULTS +//========================= +// These are caps bits to indicate what an object's capabilities (currently used for save/restore and level transitions) +#define FCAP_CUSTOMSAVE 0x00000001 +#define FCAP_ACROSS_TRANSITION 0x00000002 // should transfer between transitions +#define FCAP_MUST_SPAWN 0x00000004 // Spawn after restore +#define FCAP_IMPULSE_USE 0x00000008 // can be used by the player +#define FCAP_CONTINUOUS_USE 0x00000010 // can be used by the player +#define FCAP_ONOFF_USE 0x00000020 // can be used by the player +#define FCAP_DIRECTIONAL_USE 0x00000040 // Player sends +/- 1 when using (currently only tracktrains) +#define FCAP_FORCE_TRANSITION 0x00000080 // ALWAYS goes across transitions +#define FCAP_ONLYDIRECT_USE 0x00000100 //LRC - can't use this entity through a wall. +#define FCAP_DONT_SAVE 0x80000000 // Don't save this + +//========================= +// MONSTER CLASSIFY +//========================= +// For CLASSIFY +#define CLASS_NONE 0 +#define CLASS_MACHINE 1 +#define CLASS_PLAYER 2 +#define CLASS_HUMAN_PASSIVE 3 +#define CLASS_HUMAN_MILITARY 4 +#define CLASS_ALIEN_MILITARY 5 +#define CLASS_ALIEN_PASSIVE 6 +#define CLASS_ALIEN_MONSTER 7 +#define CLASS_ALIEN_PREY 8 +#define CLASS_ALIEN_PREDATOR 9 +#define CLASS_INSECT 10 +#define CLASS_PLAYER_ALLY 11 +#define CLASS_PLAYER_BIOWEAPON 12 // hornets and snarks.launched by players +#define CLASS_ALIEN_BIOWEAPON 13 // hornets and snarks.launched by the alien menace +#define CLASS_FACTION_A 14 // very simple new classes, for use with Behaves As +#define CLASS_FACTION_B 15 +#define CLASS_FACTION_C 16 +#define CLASS_BARNACLE 99 //special because no one pays attention to it, and it eats a wide cross-section of creatures. + +//========================= +// DAMAGE DEFAULTS +//========================= +#define DMG_GENERIC 0 // generic damage was done +#define DMG_CRUSH (1 << 0) // crushed by falling or moving object +#define DMG_BULLET (1 << 1) // shot +#define DMG_SLASH (1 << 2) // cut, clawed, stabbed +#define DMG_BURN (1 << 3) // heat burned +#define DMG_FREEZE (1 << 4) // frozen +#define DMG_FALL (1 << 5) // fell too far +#define DMG_BLAST (1 << 6) // explosive blast damage +#define DMG_CLUB (1 << 7) // crowbar, punch, headbutt +#define DMG_SHOCK (1 << 8) // electric shock +#define DMG_SONIC (1 << 9) // sound pulse shockwave +#define DMG_ENERGYBEAM (1 << 10) // laser or other high energy beam +#define DMG_NEVERGIB (1 << 12) // with this bit OR'd in, no damage type will be able to gib victims upon death +#define DMG_ALWAYSGIB (1 << 13) // with this bit OR'd in, any damage type can be made to gib victims upon death. +#define DMG_DROWN (1 << 14) // Drowning +#define DMG_TIMEBASED (~(0x3fff))// mask for time-based damage +#define DMG_PARALYZE (1 << 15) // slows affected creature down +#define DMG_NERVEGAS (1 << 16) // nerve toxins, very bad +#define DMG_POISON (1 << 17) // blood poisioning +#define DMG_RADIATION (1 << 18) // radiation exposure +#define DMG_DROWNRECOVER (1 << 19) // drowning recovery +#define DMG_ACID (1 << 20) // toxic chemicals or acid burns +#define DMG_SLOWBURN (1 << 21) // in an oven +#define DMG_SLOWFREEZE (1 << 22) // in a subzero freezer +#define DMG_MORTAR (1 << 23) // Hit by air raid (done to distinguish grenade from mortar) +#define DMG_NUCLEAR (1 << 24) // dmg at nuclear explode + +// these are the damage types that are allowed to gib corpses +#define DMG_GIB_CORPSE ( DMG_CRUSH | DMG_FALL | DMG_BLAST | DMG_SONIC | DMG_CLUB | DMG_NUCLEAR ) +// these are the damage types that have client hud art +#define DMG_SHOWNHUD (DMG_POISON | DMG_ACID | DMG_FREEZE | DMG_SLOWFREEZE | DMG_DROWN | DMG_BURN | DMG_SLOWBURN | DMG_NERVEGAS | DMG_RADIATION | DMG_SHOCK | DMG_NUCLEAR) + +#define PARALYZE_DURATION 2 // number of 2 second intervals to take damage +#define PARALYZE_DAMAGE 1.0 // damage to take each 2 second interval +#define NERVEGAS_DURATION 2 +#define NERVEGAS_DAMAGE 5.0 +#define POISON_DURATION 5 +#define POISON_DAMAGE 2.0 +#define RADIATION_DURATION 2 +#define RADIATION_DAMAGE 1.0 +#define ACID_DURATION 2 +#define ACID_DAMAGE 5.0 +#define SLOWBURN_DURATION 2 +#define SLOWBURN_DAMAGE 1.0 +#define SLOWFREEZE_DURATION 2 +#define SLOWFREEZE_DAMAGE 1.0 + +#define itbd_Paralyze 0 +#define itbd_NerveGas 1 +#define itbd_Poison 2 +#define itbd_Radiation 3 +#define itbd_DrownRecover 4 +#define itbd_Acid 5 +#define itbd_SlowBurn 6 +#define itbd_SlowFreeze 7 +#define CDMG_TIMEBASED 8 + +// when calling KILLED(), a value that governs gib behavior is expected to be +// one of these three values +#define GIB_NORMAL 0// gib if entity was overkilled +#define GIB_NEVER 1// never gib, no matter how much death damage is done ( freezing, etc ) +#define GIB_ALWAYS 2// always gib ( Houndeye Shock, Barnacle Bite ) + +//========================= +// CAPS DEFAULTS +//========================= +#define bits_CAP_DUCK ( 1 << 0 )// crouch +#define bits_CAP_JUMP ( 1 << 1 )// jump/leap +#define bits_CAP_STRAFE ( 1 << 2 )// strafe ( walk/run sideways) +#define bits_CAP_SQUAD ( 1 << 3 )// can form squads +#define bits_CAP_SWIM ( 1 << 4 )// proficiently navigate in water +#define bits_CAP_CLIMB ( 1 << 5 )// climb ladders/ropes +#define bits_CAP_USE ( 1 << 6 )// open doors/push buttons/pull levers +#define bits_CAP_HEAR ( 1 << 7 )// can hear forced sounds +#define bits_CAP_AUTO_DOORS ( 1 << 8 )// can trigger auto doors +#define bits_CAP_OPEN_DOORS ( 1 << 9 )// can open manual doors +#define bits_CAP_TURN_HEAD ( 1 << 10)// can turn head, always bone controller 0 + +#define bits_CAP_RANGE_ATTACK1 ( 1 << 11)// can do a range attack 1 +#define bits_CAP_RANGE_ATTACK2 ( 1 << 12)// can do a range attack 2 +#define bits_CAP_MELEE_ATTACK1 ( 1 << 13)// can do a melee attack 1 +#define bits_CAP_MELEE_ATTACK2 ( 1 << 14)// can do a melee attack 2 +#define bits_CAP_FLY ( 1 << 15)// can fly, move all around +#define bits_CAP_DOORS_GROUP (bits_CAP_USE | bits_CAP_AUTO_DOORS | bits_CAP_OPEN_DOORS) + + +//========================= +// WEAPON DEFAULTS +//========================= +//sound volume for different states +#define PRIMARY_CHARGE_VOLUME 256 +#define PRIMARY_FIRE_VOLUME 450 + +#define LOUD_GUN_VOLUME 1000 +#define NORMAL_GUN_VOLUME 600 +#define QUIET_GUN_VOLUME 200 + +//flash brightness +#define BRIGHT_GUN_FLASH 512 +#define NORMAL_GUN_FLASH 256 +#define DIM_GUN_FLASH 128 +#define NO_GUN_FLASH 0 + +#define WEAPON_NOCLIP -1 + +//default as barney hands for weapon +#define GORDON_HANDS 1 +#define BARNEY_HANDS 0 +//Bullet damage settings +//#define 9MM_BULLET_DMG 8 + +//========================= +// SETTINGS FOR EACH WEAPON +//========================= + +//Crowbar settings (weapon_crowbar) +#define CROWBAR_WEIGHT 0 +#define CROWBAR_DMG 10 +#define CROWBAR_BODYHIT_VOLUME 128 +#define CROWBAR_WALLHIT_VOLUME 512 + +//Glock settings (weapon_glock) +#define GLOCK_WEIGHT 10 +#define GLOCK_MAX_CARRY 250 +#define GLOCK_MAX_CLIP 17 +#define GLOCK_DEFAULT_GIVE 17 +#define GLOCK_RANDOM_GIVE RANDOM_LONG( 10, 17 ) + +//Desert eagle settings (weapon_eagle) +#define DESERT_MAX_CLIP 7 +#define DESERT_WEIGHT 15 +#define DESERT_DEFAULT_GIVE 7 +#define DESERT_RANDOM_GIVE RANDOM_LONG( 4, 7 ) +#define DESERT_MAX_CARRY 21 +#define DESERT_LASER_FOCUS 850 + +//mp5 settings (weapon_mp5) +#define MP5_WEIGHT 15 +#define MP5_MAX_CLIP 50 +#define MP5_DEFAULT_GIVE 25 +#define MP5_RANDOM_GIVE RANDOM_LONG( 19, 45 ) +#define MP5_M203_DEFAULT_GIVE 0 +#define MP5_M203_RANDOM_GIVE RANDOM_LONG( 0, 3 ) + +//saw settings (weapon_m249) +#define SAW_WEIGHT 25 +#define SAW_MAX_CARRY 200 +#define SAW_MAX_CLIP 100 +#define SAW_DEFAULT_GIVE 100 +#define SAW_RANDOM_GIVE RANDOM_LONG( 20, 100 ) + +//shotgun settings (weapon_shotgun) +#define SHOTGUN_WEIGHT 15 +#define SHOTGUN_MAX_CLIP 6 +#define SHOTGUN_DEFAULT_GIVE 6 +#define SHOTGUN_RANDOM_GIVE RANDOM_LONG( 1, 6 ) +#define VECTOR_CONE_DM_SHOTGUN Vector( 0.08716, 0.04362, 0.00 )// 10 degrees by 5 degrees +#define VECTOR_CONE_DM_DOUBLESHOTGUN Vector( 0.17365, 0.04362, 0.00 ) // 20 degrees by 5 degrees + +//m40a1 settings (weapon_m40a1) +#define M40A1_WEIGHT 25 +#define M40A1_MAX_CARRY 10 +#define M40A1_MAX_CLIP 5 +#define M40A1_DEFAULT_GIVE 5 +#define M40A1_RANDOM_GIVE RANDOM_LONG( 1, 5 ) +#define MAX_ZOOM 15 + +//Rpg settings (weapon_rpg) +#define RPG_WEIGHT 20 +#define RPG_MAX_CLIP 1 +#define RPG_DEFAULT_GIVE 1 +#define AMMO_RPGCLIP_GIVE 1 +#define RPG_ROCKET_DMG 100 +#define RPG_LASER_FOCUS 350 + +//gauss settings (weapon_gauss) +#define GAUSS_PRIMARY_DMG 20 +#define GAUSS_WEIGHT 20 +#define GAUSS_DEFAULT_GIVE 20 +#define GAUSS_RANDOM_GIVE RANDOM_LONG( 10, 40 ) + +//Egon settings (weapon_egon) +#define EGON_WEIGHT 20 +#define EGON_DEFAULT_GIVE 20 +#define EGON_RANDOM_GIVE RANDOM_LONG( 15, 35 ) +#define EGON_NARROW_DMG 6 +#define EGON_WIDE_DMG 14 +#define EGON_BEAM_SPRITE "sprites/xbeam1.spr" +#define EGON_FLARE_SPRITE "sprites/XSpark1.spr" +#define EGON_SOUND_OFF "weapons/egon_off1.wav" +#define EGON_SOUND_RUN "weapons/egon_run3.wav" +#define EGON_SOUND_STARTUP "weapons/egon_windup2.wav" +#define EGON_SWITCH_NARROW_TIME 0.75 +#define EGON_SWITCH_WIDE_TIME 1.5 +#define EGON_PULSE_INTERVAL 0.1 +#define EGON_DISCHARGE_INTERVAL 0.1 + +//Displacer settings (weapon_displacer) +#define DISPLACER_WEIGHT 10 +#define DISPLACER_DEFAULT_GIVE 80 +#define DISPLACER_RANDOM_GIVE RANDOM_LONG( 40, 100 ) +#define DBALL_DMG 100 +#define DBALL_RADIUS 400 + +//hornetgun settings (weapon_hornetgun) +#define HORNETGUN_WEIGHT 10 +#define HIVEHAND_DEFAULT_GIVE 8 + +//Handgrenade settings (weapon_handgrenade) +#define HANDGRENADE_WEIGHT 5 +#define HANDGRENADE_DMG 100 +#define HANDGRENADE_DEFAULT_GIVE 1 + +//Redeemder settings (weapon_redeemer) +#define REDEEMER_ROCKET_DMG 500 +#define WARHEAD_SPEED 600 +#define WARHEAD_SPEED_UNDERWATER 300 +#define WARHEAD_MAX_SPEED 1400 + +//========================= +// AMMO SETTINGS +//========================= + +//max capacity +#define _9MM_MAX_CARRY 250 +#define URANIUM_MAX_CARRY 100 +#define BUCKSHOT_MAX_CARRY 125 +#define BOLT_MAX_CARRY 50 +#define ROCKET_MAX_CARRY 3 +#define HANDGRENADE_MAX_CARRY 10 +#define HORNET_MAX_CARRY 8 +#define M203_GRENADE_MAX_CARRY 10 + +//ammo default give +#define AMMO_URANIUMBOX_GIVE 20 +#define AMMO_GLOCKCLIP_GIVE GLOCK_MAX_CLIP +#define AMMO_357BOX_GIVE 6 +#define AMMO_MP5CLIP_GIVE MP5_MAX_CLIP +#define AMMO_CHAINBOX_GIVE 200 +#define AMMO_M203BOX_GIVE 2 +#define AMMO_BUCKSHOTBOX_GIVE 12 +#define AMMO_CROSSBOWCLIP_GIVE CROSSBOW_MAX_CLIP +#define AMMO_URANIUMBOX_GIVE 20 + +//========================= +// TIME TO RESPAWN +//========================= + +#define RESPAWN_TIME_30SEC 30 +#define RESPAWN_TIME_60SEC 60 +#define RESPAWN_TIME_120SEC 120 +#define RND_RESPAWN_TIME_30 RANDOM_FLOAT( 15, 45 ) +#define RND_RESPAWN_TIME_60 RANDOM_FLOAT( 45, 80 ) +#define RND_RESPAWN_TIME_120 RANDOM_FLOAT( 80, 145 ) + +//========================= +// BATTERY AND HEALTHKIT CAPACITY +//========================= + +#define BATTERY_CHARGE 15 +#define MAX_NORMAL_BATTERY 100 + +//========================= +// MONSTERS HEALTH +//========================= +//agrunt settings +#define AGRUNT_DMG_PUNCH 10 +#define AGRUNT_HEALTH 100 + +//apache +#define APACHE_HEALTH 300 + +//barney +#define BARNEY_HEALTH 50 +#define DEAD_BARNEY_HEALTH 8 + +// HEALTH/SUIT CHARGE DISTRIBUTION +#define SUIT_CHARGER_CAP 75 +#define HEATH_CHARGER_CAP 50 +#define MEDKIT_CAP 20 + +#define BOLT_DMG 40 +#define M203_DMG 100 +#define BULLSQUID_HEALTH 40 + +#define HGRUNT_GRENADE_SPEED 400 +#define HGRUNT_SHOTGUN_PELLETS 5 +#define HGRUNT_DMG_KICK 5 +#define HGRUNT_HEALTH 70 +#define HASSASSIN_HEALTH 50 + +#define LEECH_HEALTH 2 +#define LEECH_DMG_BITE 2 + +//headcrab +#define HEADCRAB_HEALTH 10 +#define HEADCRAB_DMG_BITE 10 + +//scientist +#define SCIENTIST_HEALTH 20 +#define SCIENTIST_HEAL 25 + +//turret +#define TURRET_HEALTH 60 +#define MINITURRET_HEALTH 40 +#define SENTRY_HEALTH 50 + +//zombie +#define ZOMBIE_HEALTH 75 +#define ZOMBIE_ONE_SLASH 15 +#define ZOMBIE_BOTH_SLASH 40 + +//hitgroup setttings +#define DMG_HEAD 3 +#define DMG_CHEST 1 +#define DMG_STOMACH 1 +#define DMG_ARM 1 +#define DMG_LEG 1 + +//bullets damage +#define _9MM_DMG 10 +#define _MP5_DMG 5 +#define _12MM_DMG 20 +#define _357_DMG 40 +#define _556_DMG 30 +#define _762_DMG 50 +#define BUCKSHOT_DMG 15 + +//bullet vector cone +#define VECTOR_CONE_1DEGREES Vector( 0.00873, 0.00873, 0.00873 ) +#define VECTOR_CONE_2DEGREES Vector( 0.01745, 0.01745, 0.01745 ) +#define VECTOR_CONE_3DEGREES Vector( 0.02618, 0.02618, 0.02618 ) +#define VECTOR_CONE_4DEGREES Vector( 0.03490, 0.03490, 0.03490 ) +#define VECTOR_CONE_5DEGREES Vector( 0.04362, 0.04362, 0.04362 ) +#define VECTOR_CONE_6DEGREES Vector( 0.05234, 0.05234, 0.05234 ) +#define VECTOR_CONE_7DEGREES Vector( 0.06105, 0.06105, 0.06105 ) +#define VECTOR_CONE_8DEGREES Vector( 0.06976, 0.06976, 0.06976 ) +#define VECTOR_CONE_9DEGREES Vector( 0.07846, 0.07846, 0.07846 ) +#define VECTOR_CONE_10DEGREES Vector( 0.08716, 0.08716, 0.08716 ) +#define VECTOR_CONE_15DEGREES Vector( 0.13053, 0.13053, 0.13053 ) +#define VECTOR_CONE_20DEGREES Vector( 0.17365, 0.17365, 0.17365 ) +#define VECTOR_CONE_0DEGREES Vector( 0.00000, 0.00000, 0.00000 ) +#endif//DEFAULT_H \ No newline at end of file diff --git a/server/global/dll_int.cpp b/server/global/dll_int.cpp new file mode 100644 index 00000000..102b93ee --- /dev/null +++ b/server/global/dll_int.cpp @@ -0,0 +1,434 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// dll_int.cpp - dll entry points +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "saverestore.h" +#include "client.h" +#include "decals.h" +#include "gamerules.h" +#include "game.h" +#include "defaults.h" + +void EntvarsKeyvalue( entvars_t *pev, KeyValueData *pkvd ); + +BOOL gTouchDisabled = FALSE; +enginefuncs_t g_engfuncs; +globalvars_t *gpGlobals; + +// Main DLL entry point +BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) +{ + return TRUE; +} + +void GiveFnptrsToDll( enginefuncs_t* pengfuncsFromEngine, globalvars_t *pGlobals ) +{ + memcpy(&g_engfuncs, pengfuncsFromEngine, sizeof(enginefuncs_t)); + gpGlobals = pGlobals; +} + +static DLL_FUNCTIONS gFunctionTable = +{ + sizeof( DLL_FUNCTIONS ), + GameDLLInit, // pfnGameInit + DispatchSpawn, // pfnSpawn + DispatchThink, // pfnThink + DispatchUse, // pfnUse + DispatchTouch, // pfnTouch + DispatchBlocked, // pfnBlocked + DispatchKeyValue, // pfnKeyValue + DispatchFrame, // pfnFrame + DispatchSave, // pfnSave + DispatchRestore, // pfnRestore + DispatchObjectCollsionBox, // pfnAbsBox + + SaveWriteFields, //pfnSaveWriteFields + SaveReadFields, //pfnSaveReadFields + + SaveGlobalState, //pfnSaveGlobalState + RestoreGlobalState, //pfnRestoreGlobalState + ResetGlobalState, //pfnResetGlobalState + + ClientConnect, //pfnClientConnect + ClientDisconnect, //pfnClientDisconnect + ClientKill, //pfnClientKill + ClientPutInServer, //pfnClientPutInServer + ClientCommand, //pfnClientCommand + ClientUserInfoChanged, //pfnClientUserInfoChanged + + ServerActivate, //pfnServerActivate + ServerDeactivate, //pfnServerDeactivate + + PlayerPreThink, //pfnPlayerPreThink + PlayerPostThink, //pfnPlayerPostThink + + StartFrame, //pfnStartFrame + EndFrame, //pfnEndFrame + ParmsChangeLevel, //pfnParmsChangeLevel + + GetGameDescription, //pfnGetGameDescription Returns string describing current .dll game. +}; + +//======================================================================= +// GetApi +//======================================================================= + +int GetEntityAPI( DLL_FUNCTIONS *pFunctionTable, int interfaceVersion ) +{ + if ( !pFunctionTable || interfaceVersion != INTERFACE_VERSION ) + { + return FALSE; + } + + memcpy( pFunctionTable, &gFunctionTable, sizeof( DLL_FUNCTIONS ) ); + return TRUE; +} + +//======================================================================= +// dispatch functions +//======================================================================= + +int DispatchSpawn( edict_t *pent ) +{ + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pent); + + if (pEntity) + { + // Initialize these or entities who don't link to the world won't have anything in here + pEntity->pev->absmin = pEntity->pev->origin - Vector(1,1,1); + pEntity->pev->absmax = pEntity->pev->origin + Vector(1,1,1); + + pEntity->Spawn(); + pEntity = (CBaseEntity *)GET_PRIVATE(pent); + + if ( pEntity ) + { + pEntity->pev->colormap = ENTINDEX(pent); + if ( g_pGameRules && !g_pGameRules->IsAllowedToSpawn( pEntity ) ) + return -1; // return that this entity should be deleted + if ( pEntity->pev->flags & FL_KILLME ) + return -1; + } + + + // Handle global stuff here + if ( pEntity && pEntity->pev->globalname ) + { + const globalentity_t *pGlobal = gGlobalState.EntityFromTable( pEntity->pev->globalname ); + if ( pGlobal ) + { + // Already dead? delete + if ( pGlobal->state == GLOBAL_DEAD ) return -1; + else if ( !FStrEq( STRING(gpGlobals->mapname), pGlobal->levelName ) ) + pEntity->MakeDormant();// Hasn't been moved to this level yet, wait but stay alive + // In this level & not dead, continue on as normal + } + else gGlobalState.EntityAdd( pEntity->pev->globalname, gpGlobals->mapname, GLOBAL_ON ); + } + } + return 0; +} + +void DispatchKeyValue( edict_t *pentKeyvalue, KeyValueData *pkvd ) +{ + if ( !pkvd || !pentKeyvalue )return; + + EntvarsKeyvalue( VARS(pentKeyvalue), pkvd ); + + // If the key was an entity variable, or there's no class set yet, don't look for the object, it may + // not exist yet. + if ( pkvd->fHandled || pkvd->szClassName == NULL )return; + + // Get the actualy entity object + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pentKeyvalue); + + if ( !pEntity )return; + pEntity->KeyValue( pkvd ); +} + +void DispatchFrame( edict_t *pent ) +{ +} + +void DispatchTouch( edict_t *pentTouched, edict_t *pentOther ) +{ + if ( gTouchDisabled ) return; + + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pentTouched); + CBaseEntity *pOther = (CBaseEntity *)GET_PRIVATE( pentOther ); + + if ( pEntity && pOther && ! ((pEntity->pev->flags | pOther->pev->flags) & FL_KILLME) ) + pEntity->Touch( pOther ); +} + +void DispatchUse( edict_t *pentUsed, edict_t *pentOther ) +{ + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pentUsed); + CBaseEntity *pOther = (CBaseEntity *)GET_PRIVATE(pentOther); + + if (pEntity && !(pEntity->pev->flags & FL_KILLME) ) + pEntity->Use( pOther, pOther, USE_TOGGLE, 0 ); +} + +void DispatchThink( edict_t *pent ) +{ + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pent); + + if (pEntity) + { + if ( FBitSet( pEntity->pev->flags, FL_DORMANT ) ) + Msg("ERROR: Dormant entity %s is thinking!!\n", STRING(pEntity->pev->classname) ); + + pEntity->Think(); + } +} + +void DispatchBlocked( edict_t *pentBlocked, edict_t *pentOther ) +{ + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE( pentBlocked ); + CBaseEntity *pOther = (CBaseEntity *)GET_PRIVATE( pentOther ); + + if (pEntity)pEntity->Blocked( pOther ); +} + +void DispatchSave( edict_t *pent, SAVERESTOREDATA *pSaveData ) +{ + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pent); + + if ( pEntity && pSaveData ) + { + ENTITYTABLE *pTable = &pSaveData->pTable[ pSaveData->currentIndex ]; + + if ( pTable->pent != pent ) + ALERT( at_error, "ENTITY TABLE OR INDEX IS WRONG!!!!\n" ); + + if ( pEntity->ObjectCaps() & FCAP_DONT_SAVE )return; + + // These don't use ltime & nextthink as times really, but we'll fudge around it. + if ( pEntity->pev->movetype == MOVETYPE_PUSH ) + { + float delta = gpGlobals->time - pEntity->pev->ltime; + pEntity->pev->ltime += delta; + pEntity->pev->nextthink += delta; + pEntity->m_fPevNextThink = pEntity->pev->nextthink; + pEntity->m_fNextThink += delta; + } + + pTable->location = pSaveData->size; // Remember entity position for file I/O + pTable->classname = pEntity->pev->classname; // Remember entity class for respawn + + CSave saveHelper( pSaveData ); + pEntity->Save( saveHelper ); + + pTable->size = pSaveData->size - pTable->location;// Size of entity block is data size written to block + } +} + +int DispatchRestore( edict_t *pent, SAVERESTOREDATA *pSaveData, int globalEntity ) +{ + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pent); + + if ( pEntity && pSaveData ) + { + entvars_t tmpVars; + Vector oldOffset; + + CRestore restoreHelper( pSaveData ); + if ( globalEntity ) + { + CRestore tmpRestore( pSaveData ); + tmpRestore.PrecacheMode( 0 ); + tmpRestore.ReadEntVars( "ENTVARS", &tmpVars ); + + pSaveData->size = pSaveData->pTable[pSaveData->currentIndex].location; + pSaveData->pCurrentData = pSaveData->pBaseData + pSaveData->size; + + const globalentity_t *pGlobal = gGlobalState.EntityFromTable( tmpVars.globalname ); + + // Don't overlay any instance of the global that isn't the latest + // pSaveData->szCurrentMapName is the level this entity is coming from + // pGlobla->levelName is the last level the global entity was active in. + // If they aren't the same, then this global update is out of date. + if ( !FStrEq( pSaveData->szCurrentMap, pGlobal->levelName ) )return 0; + + // Compute the new global offset + oldOffset = pSaveData->vecLandmarkOffset; + CBaseEntity *pNewEntity = UTIL_FindGlobalEntity( tmpVars.classname, tmpVars.globalname ); + if ( pNewEntity ) + { + // Tell the restore code we're overlaying a global entity from another level + restoreHelper.SetGlobalMode( 1 ); // Don't overwrite global fields + pSaveData->vecLandmarkOffset = (pSaveData->vecLandmarkOffset - pNewEntity->pev->mins) + tmpVars.mins; + pEntity = pNewEntity;// we're going to restore this data OVER the old entity + pent = ENT( pEntity->pev ); + // Update the global table to say that the global definition of this entity should come from this level + gGlobalState.EntityUpdate( pEntity->pev->globalname, gpGlobals->mapname ); + } + else + { + // This entity will be freed automatically by the engine. If we don't do a restore on a matching entity (below) + // or call EntityUpdate() to move it to this level, we haven't changed global state at all. + return 0; + } + + } + + if ( pEntity->ObjectCaps() & FCAP_MUST_SPAWN ) + { + pEntity->Restore( restoreHelper ); + pEntity->Spawn(); + } + else + { + pEntity->Restore( restoreHelper ); + pEntity->Precache( ); + } + + // Again, could be deleted, get the pointer again. + pEntity = (CBaseEntity *)GET_PRIVATE(pent); + if (pEntity )pEntity->pev->colormap = ENTINDEX(pent); + + // Is this an overriding global entity (coming over the transition), or one restoring in a level + if ( globalEntity ) + { + pSaveData->vecLandmarkOffset = oldOffset; + if ( pEntity ) + { + pEntity->RestorePhysics(); + UTIL_SetOrigin( pEntity, pEntity->pev->origin ); + pEntity->OverrideReset(); + } + } + else if ( pEntity && pEntity->pev->globalname ) + { + const globalentity_t *pGlobal = gGlobalState.EntityFromTable( pEntity->pev->globalname ); + if ( pGlobal ) + { + // Already dead? delete + if ( pGlobal->state == GLOBAL_DEAD ) return -1; + else if ( !FStrEq( STRING(gpGlobals->mapname), pGlobal->levelName ) ) + { + pEntity->MakeDormant(); // Hasn't been moved to this level yet, wait but stay alive + } + // In this level & not dead, continue on as normal + } + else + { + ALERT( at_error, "Global Entity %s (%s) not in table!!!\n", STRING(pEntity->pev->globalname), STRING(pEntity->pev->classname) ); + // Spawned entities default to 'On' + gGlobalState.EntityAdd( pEntity->pev->globalname, gpGlobals->mapname, GLOBAL_ON ); + } + } + } + return 0; +} + +void SaveWriteFields( SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ) +{ + CSave saveHelper( pSaveData ); + saveHelper.WriteFields( "SWF", pname, pBaseData, pFields, fieldCount ); +} + +void SaveReadFields( SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ) +{ + CRestore restoreHelper( pSaveData ); + restoreHelper.ReadFields( pname, pBaseData, pFields, fieldCount ); +} + +void SetObjectCollisionBox( entvars_t *pev ) +{ + if ( (pev->solid == SOLID_BSP) && (pev->angles.x || pev->angles.y|| pev->angles.z) ) + { // expand for rotation + float max, v; + int i; + + max = 0; + for (i=0 ; i<3 ; i++) + { + v = fabs( ((float *)pev->mins)[i]); + if (v > max) max = v; + v = fabs( ((float *)pev->maxs)[i]); + if (v > max) max = v; + } + for (i=0 ; i<3 ; i++) + { + ((float *)pev->absmin)[i] = ((float *)pev->origin)[i] - max; + ((float *)pev->absmax)[i] = ((float *)pev->origin)[i] + max; + } + } + else + { + pev->absmin = pev->origin + pev->mins; + pev->absmax = pev->origin + pev->maxs; + } + pev->absmin.x -= 1; + pev->absmin.y -= 1; + pev->absmin.z -= 1; + pev->absmax.x += 1; + pev->absmax.y += 1; + pev->absmax.z += 1; +} + +void DispatchObjectCollsionBox( edict_t *pent ) +{ + CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pent); + if (pEntity)pEntity->SetObjectCollisionBox(); + else SetObjectCollisionBox( &pent->v ); +} + +//======================================================================= +// ehandle operations +//======================================================================= + +edict_t * EHANDLE::Get( void ) +{ + if (m_pent) + { + if (m_pent->serialnumber == m_serialnumber) return m_pent; + else return NULL; + } + return NULL; +}; + +edict_t *EHANDLE::Set( edict_t *pent ) +{ + m_pent = pent; + if (pent) m_serialnumber = m_pent->serialnumber; + return pent; +}; + + +EHANDLE :: operator CBaseEntity *() +{ + return (CBaseEntity *)GET_PRIVATE(Get()); +}; + + +CBaseEntity *EHANDLE :: operator = (CBaseEntity *pEntity) +{ + if (pEntity) + { + m_pent = ENT( pEntity->pev ); + if (m_pent)m_serialnumber = m_pent->serialnumber; + } + else + { + m_pent = NULL; + m_serialnumber = 0; + } + return pEntity; +} + +EHANDLE :: operator int() +{ + return Get() != NULL; +} + +CBaseEntity *EHANDLE :: operator->() +{ + return (CBaseEntity *)GET_PRIVATE(Get()); +} \ No newline at end of file diff --git a/server/global/enginecallback.h b/server/global/enginecallback.h new file mode 100644 index 00000000..e7b52368 --- /dev/null +++ b/server/global/enginecallback.h @@ -0,0 +1,134 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef ENGINECALLBACK_H +#define ENGINECALLBACK_H +#pragma once + +// Must be provided by user of this code +extern enginefuncs_t g_engfuncs; + +// The actual engine callbacks +#define MALLOC( x ) (*g_engfuncs.pfnMemAlloc)( x, __FILE__, __LINE__ ) +#define CALLOC( x, y ) (*g_engfuncs.pfnMemAlloc)((x) * (y), __FILE__, __LINE__ ) +#define FREE( x ) (*g_engfuncs.pfnMemFree)( x, __FILE__, __LINE__ ) +#define PRECACHE_MODEL (*g_engfuncs.pfnPrecacheModel) +#define PRECACHE_SOUND (*g_engfuncs.pfnPrecacheSound) +#define PRECACHE_GENERIC (*g_engfuncs.pfnPrecacheGeneric) +#define SET_MODEL (*g_engfuncs.pfnSetModel) +#define MODEL_INDEX (*g_engfuncs.pfnModelIndex) +#define MODEL_FRAMES (*g_engfuncs.pfnModelFrames) +#define SET_SIZE (*g_engfuncs.pfnSetSize) +#define CHANGE_LEVEL (*g_engfuncs.pfnChangeLevel) +#define GET_SPAWN_PARMS (*g_engfuncs.pfnGetSpawnParms) +#define SAVE_SPAWN_PARMS (*g_engfuncs.pfnSaveSpawnParms) +#define VEC_TO_YAW (*g_engfuncs.pfnVecToYaw) +#define VEC_TO_ANGLES (*g_engfuncs.pfnVecToAngles) +#define MOVE_TO_ORIGIN (*g_engfuncs.pfnMoveToOrigin) +#define oldCHANGE_YAW (*g_engfuncs.pfnChangeYaw) +#define CHANGE_PITCH (*g_engfuncs.pfnChangePitch) +#define MAKE_VECTORS (*g_engfuncs.pfnMakeVectors) +#define CREATE_ENTITY (*g_engfuncs.pfnCreateEntity) +#define REMOVE_ENTITY (*g_engfuncs.pfnRemoveEntity) +#define CREATE_NAMED_ENTITY (*g_engfuncs.pfnCreateNamedEntity) +#define MAKE_STATIC (*g_engfuncs.pfnMakeStatic) +#define ENT_IS_ON_FLOOR (*g_engfuncs.pfnEntIsOnFloor) +#define DROP_TO_FLOOR (*g_engfuncs.pfnDropToFloor) +#define WALK_MOVE (*g_engfuncs.pfnWalkMove) +#define SET_ORIGIN (*g_engfuncs.pfnSetOrigin) +#define EMIT_SOUND_DYN2 (*g_engfuncs.pfnEmitSound) +#define BUILD_SOUND_MSG (*g_engfuncs.pfnBuildSoundMsg) +#define TRACE_LINE (*g_engfuncs.pfnTraceLine) +#define TRACE_TOSS (*g_engfuncs.pfnTraceToss) +#define TRACE_MONSTER_HULL (*g_engfuncs.pfnTraceMonsterHull) +#define TRACE_HULL (*g_engfuncs.pfnTraceHull) +#define GET_AIM_VECTOR (*g_engfuncs.pfnGetAimVector) +#define SERVER_COMMAND (*g_engfuncs.pfnServerCommand) +#define SERVER_EXECUTE (*g_engfuncs.pfnServerExecute) +#define CLIENT_COMMAND (*g_engfuncs.pfnClientCommand) +#define PARTICLE_EFFECT (*g_engfuncs.pfnParticleEffect) +#define LIGHT_STYLE (*g_engfuncs.pfnLightStyle) +#define DECAL_INDEX (*g_engfuncs.pfnDecalIndex) +#define POINT_CONTENTS (*g_engfuncs.pfnPointContents) +#define CRC_INIT (*g_engfuncs.pfnCRC_Init) +#define CRC_PROCESS_BUFFER (*g_engfuncs.pfnCRC_ProcessBuffer) +#define CRC_FINAL (*g_engfuncs.pfnCRC_Final) +#define RANDOM_LONG (*g_engfuncs.pfnRandomLong) +#define RANDOM_FLOAT (*g_engfuncs.pfnRandomFloat) +#define GETPLAYERAUTHID (*g_engfuncs.pfnGetPlayerAuthId) + +inline void MESSAGE_BEGIN( int msg_dest, int msg_type, const float *pOrigin = NULL, edict_t *ed = NULL ) { + (*g_engfuncs.pfnMessageBegin)(msg_dest, msg_type, pOrigin, ed); +} +#define MESSAGE_END (*g_engfuncs.pfnMessageEnd) +#define WRITE_BYTE (*g_engfuncs.pfnWriteByte) +#define WRITE_CHAR (*g_engfuncs.pfnWriteChar) +#define WRITE_SHORT (*g_engfuncs.pfnWriteShort) +#define WRITE_LONG (*g_engfuncs.pfnWriteLong) +#define WRITE_ANGLE (*g_engfuncs.pfnWriteAngle) +#define WRITE_COORD (*g_engfuncs.pfnWriteCoord) +#define WRITE_FLOAT (*g_engfuncs.pfnWriteFloat) +#define WRITE_STRING (*g_engfuncs.pfnWriteString) +#define WRITE_ENTITY (*g_engfuncs.pfnWriteEntity) +#define CVAR_REGISTER (*g_engfuncs.pfnCVarRegister) +#define CVAR_GET_FLOAT (*g_engfuncs.pfnCVarGetFloat) +#define CVAR_GET_STRING (*g_engfuncs.pfnCVarGetString) +#define CVAR_SET_FLOAT (*g_engfuncs.pfnCVarSetFloat) +#define CVAR_SET_STRING (*g_engfuncs.pfnCVarSetString) +#define CVAR_GET_POINTER (*g_engfuncs.pfnCVarGetPointer) +#define ALERT (*g_engfuncs.pfnAlertMessage) +#define ENGINE_FPRINTF (*g_engfuncs.pfnEngineFprintf) +#define ALLOC_PRIVATE (*g_engfuncs.pfnPvAllocEntPrivateData) +inline void *GET_PRIVATE( edict_t *pent ) +{ + if ( pent ) + return pent->pvServerData; + return NULL; +} + +// NOTE: Xash3D using custom StringTable System that using safety methods for access to strings +// and never make duplicated strings, so it make no differences between ALLOC_STRING and MAKE_STRING +// leave macros as legacy +#define ALLOC_STRING (*g_engfuncs.pfnAllocString) +#define MAKE_STRING (*g_engfuncs.pfnAllocString) +#define STRING (*g_engfuncs.pfnGetString) + +#define FREE_PRIVATE (*g_engfuncs.pfnFreeEntPrivateData) +#define FIND_ENTITY_BY_STRING (*g_engfuncs.pfnFindEntityByString) +#define GETENTITYILLUM (*g_engfuncs.pfnGetEntityIllum) +#define FIND_ENTITY_IN_SPHERE (*g_engfuncs.pfnFindEntityInSphere) +#define FIND_CLIENT_IN_PVS (*g_engfuncs.pfnFindClientInPVS) +#define EMIT_AMBIENT_SOUND (*g_engfuncs.pfnEmitAmbientSound) +#define GET_MODEL_PTR (*g_engfuncs.pfnGetModelPtr) +#define REG_USER_MSG (*g_engfuncs.pfnRegUserMsg) +#define GET_BONE_POSITION (*g_engfuncs.pfnGetBonePosition) +#define FUNCTION_FROM_NAME (*g_engfuncs.pfnFunctionFromName) +#define NAME_FOR_FUNCTION (*g_engfuncs.pfnNameForFunction) +#define TRACE_TEXTURE (*g_engfuncs.pfnTraceTexture) +#define CLIENT_PRINTF (*g_engfuncs.pfnClientPrintf) +#define CMD_ARGS (*g_engfuncs.pfnCmd_Args) +#define CMD_ARGC (*g_engfuncs.pfnCmd_Argc) +#define CMD_ARGV (*g_engfuncs.pfnCmd_Argv) +#define GET_ATTACHMENT (*g_engfuncs.pfnGetAttachment) +#define SET_VIEW (*g_engfuncs.pfnSetView) +#define SET_CROSSHAIRANGLE (*g_engfuncs.pfnCrosshairAngle) +#define LOAD_FILE (*g_engfuncs.pfnLoadFile) +#define FREE_FILE (*g_engfuncs.pfnFreeFile) +#define COMPARE_FILE_TIME (*g_engfuncs.pfnCompareFileTime) +#define GET_GAME_DIR (*g_engfuncs.pfnGetGameDir) +#define IS_MAP_VALID (*g_engfuncs.pfnIsMapValid) +#define IS_DEDICATED_SERVER (*g_engfuncs.pfnIsDedicatedServer) +#define HOST_ERROR (*g_engfuncs.pfnHostError) + +#endif //ENGINECALLBACK_H \ No newline at end of file diff --git a/server/global/extdll.h b/server/global/extdll.h new file mode 100644 index 00000000..f20c8a7b --- /dev/null +++ b/server/global/extdll.h @@ -0,0 +1,81 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// basetypes.h - global defines for dll's +// +//======================================================================= + +#ifndef BASETYPES_H +#define BASETYPES_H + +// Silence certain warnings +#pragma warning(disable : 4305) // int or float data truncation +#pragma warning(disable : 4201) // nameless struct/union +#pragma warning(disable : 4514) // unreferenced inline function removed +#pragma warning(disable : 4100) // unreferenced formal parameter + +// Prevent tons of unused windows definitions +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#define NOWINRES +#define NOSERVICE +#define NOMCX +#define NOIME +#include "windows.h" +#else // _WIN32 +#define FALSE 0 +#define TRUE (!FALSE) +typedef unsigned long ULONG; +typedef unsigned char BYTE; +typedef int BOOL; +#define MAX_PATH PATH_MAX +#include +#include +#ifndef min +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif +#ifndef max +#define max(a,b) (((a) > (b)) ? (a) : (b)) +#define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d) +#endif +#endif //_WIN32 + +#ifndef BIT +#define BIT( n ) (1<<( n )) +#endif + +// Misc C-runtime library headers +#include "stdio.h" +#include "stdlib.h" +#include "math.h" + +// Header file containing definition of globalvars_t and entvars_t +typedef unsigned char byte; +typedef unsigned short word; +typedef unsigned int uint; +typedef unsigned long dword; +typedef int string_t; // from engine's pr_comp.h; +typedef int shader_t; +typedef float vec_t; // needed before including progdefs.h + +inline unsigned long& FloatBits( vec_t& f ) { return *reinterpret_cast(&f); } +inline unsigned long const& FloatBits( vec_t const& f ) { return *reinterpret_cast(&f); } +inline vec_t BitsToFloat( unsigned long i ) { return *reinterpret_cast(&i); } +inline bool IsFinite( vec_t f ) { return ((FloatBits(f) & 0x7F800000) != 0x7F800000); } +inline unsigned long FloatAbsBits( vec_t f ) { return FloatBits(f) & 0x7FFFFFFF; } +inline float FloatMakeNegative( vec_t f ) { return BitsToFloat( FloatBits(f) | 0x80000000 ); } +inline float FloatMakePositive( vec_t f ) { return BitsToFloat( FloatBits(f) & 0x7FFFFFFF ); } + +// Shared engine/DLL constants +#include "const.h" + +// Vector class +#include "vector.h" + + // Shared header describing protocol between engine and DLLs +#include "entity_def.h" +#include "svgame_api.h" + +extern Vector vec3_origin; +extern Vector vec3_angles; + +#endif // BASETYPES_H \ No newline at end of file diff --git a/server/global/globals.cpp b/server/global/globals.cpp new file mode 100644 index 00000000..5f00e39b --- /dev/null +++ b/server/global/globals.cpp @@ -0,0 +1,81 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +/* + +===== globals.cpp ======================================================== + + DLL-wide global variable definitions. + They're all defined here, for convenient centralization. + Source files that need them should "extern ..." declare each + variable, to better document what globals they care about. + +*/ + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "baseweapon.h" +#include "soundent.h" + +DLL_GLOBAL ULONG g_ulFrameCount; +DLL_GLOBAL ULONG g_ulModelIndexEyes; +DLL_GLOBAL ULONG g_ulModelIndexPlayer; +DLL_GLOBAL Vector g_vecAttackDir; +DLL_GLOBAL int g_iSkillLevel; +DLL_GLOBAL int gDisplayTitle; +DLL_GLOBAL BOOL g_fGameOver; +DLL_GLOBAL BOOL g_startSuit; +DLL_GLOBAL const Vector g_vecZero = Vector(0,0,0); +DLL_GLOBAL int g_Language; +DLL_GLOBAL short g_sModelIndexLaser; +DLL_GLOBAL int g_sModelIndexLaserDot; +DLL_GLOBAL short g_sModelIndexFireball; +DLL_GLOBAL short g_sModelIndexSmoke; +DLL_GLOBAL short g_sModelIndexWExplosion; +DLL_GLOBAL short g_sModelIndexBubbles; +DLL_GLOBAL short g_sModelIndexBloodDrop; +DLL_GLOBAL short g_sModelIndexBloodSpray; +DLL_GLOBAL int g_sModelIndexErrorSprite; +DLL_GLOBAL int g_sModelIndexErrorModel; +DLL_GLOBAL int g_sModelIndexNullModel; +DLL_GLOBAL int g_sModelIndexNullSprite; + +int GetStdLightStyle (int iStyle) +{ + switch (iStyle) + { + case 0: return MAKE_STRING("m"); // 0 normal + case 1: return MAKE_STRING("mmnmmommommnonmmonqnmmo"); // 1 FLICKER (first variety) + case 2: return MAKE_STRING("abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba"); // 2 SLOW STRONG PULSE + case 3: return MAKE_STRING("mmmmmaaaaammmmmaaaaaabcdefgabcdefg"); // 3 CANDLE (first variety) + case 4: return MAKE_STRING("mamamamamama"); // 4 FAST STROBE + case 5: return MAKE_STRING("jklmnopqrstuvwxyzyxwvutsrqponmlkj"); // 5 GENTLE PULSE 1 + case 6: return MAKE_STRING("nmonqnmomnmomomno"); // 6 FLICKER (second variety) + case 7: return MAKE_STRING("mmmaaaabcdefgmmmmaaaammmaamm"); // 7 CANDLE (second variety) + case 8: return MAKE_STRING("mmmaaammmaaammmabcdefaaaammmmabcdefmmmaaaa"); // 8 CANDLE (third variety) + case 9: return MAKE_STRING("aaaaaaaazzzzzzzz"); // 9 SLOW STROBE (fourth variety) + case 10: return MAKE_STRING("mmamammmmammamamaaamammma"); // 10 FLUORESCENT FLICKER + case 11: return MAKE_STRING("abcdefghijklmnopqrrqponmlkjihgfedcba"); // 11 SLOW PULSE NOT FADE TO BLACK + case 12: return MAKE_STRING("mmnnmmnnnmmnn"); // 12 UNDERWATER LIGHT MUTATION + case 13: return MAKE_STRING("a"); // 13 OFF + case 14: return MAKE_STRING("aabbccddeeffgghhiijjkkllmmmmmmmmmmmmmm"); // 14 SLOW FADE IN + case 15: return MAKE_STRING("abcdefghijklmmmmmmmmmmmmmmmmmmmmmmmmmm"); // 15 MED FADE IN + case 16: return MAKE_STRING("acegikmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"); // 16 FAST FADE IN + case 17: return MAKE_STRING("llkkjjiihhggffeeddccbbaaaaaaaaaaaaaaaa"); // 17 SLOW FADE OUT + case 18: return MAKE_STRING("lkjihgfedcbaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 18 MED FADE OUT + case 19: return MAKE_STRING("kigecaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); // 19 FAST FADE OUT + default: return MAKE_STRING("m"); + } +} \ No newline at end of file diff --git a/server/global/globals.h b/server/global/globals.h new file mode 100644 index 00000000..30868b15 --- /dev/null +++ b/server/global/globals.h @@ -0,0 +1,95 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// globals.h - store global variables, +//======================================================================= +#ifndef GLOBALS_H +#define GLOBALS_H + +extern DLL_GLOBAL short g_sModelIndexLaser;// holds the index for the laser beam +extern DLL_GLOBAL short g_sModelIndexFireball;// holds the index for the fireball +extern DLL_GLOBAL short g_sModelIndexSmoke;// holds the index for the smoke cloud +extern DLL_GLOBAL short g_sModelIndexWExplosion;// holds the index for the underwater explosion +extern DLL_GLOBAL short g_sModelIndexBubbles;// holds the index for the bubbles model +extern DLL_GLOBAL short g_sModelIndexBloodDrop;// holds the sprite index for blood drops +extern DLL_GLOBAL short g_sModelIndexBloodSpray;// holds the sprite index for blood spray (bigger) +extern DLL_GLOBAL int g_sModelIndexLaserDot; +extern DLL_GLOBAL BOOL g_fGameOver; +extern DLL_GLOBAL BOOL g_startSuit; + +// C functions for external declarations that call the appropriate C++ methods + +#ifdef _WIN32 +#define EXPORT _declspec( dllexport ) +#else +#define EXPORT /* */ +#endif + +extern "C" EXPORT int GetEntityAPI( DLL_FUNCTIONS *pFunctionTable, int interfaceVersion ); +extern "C" EXPORT int GetEntityAPI2( DLL_FUNCTIONS *pFunctionTable, int *interfaceVersion ); + +extern int DispatchSpawn( edict_t *pent ); +extern void DispatchKeyValue( edict_t *pentKeyvalue, KeyValueData *pkvd ); +extern void DispatchTouch( edict_t *pentTouched, edict_t *pentOther ); +extern void DispatchUse( edict_t *pentUsed, edict_t *pentOther ); +extern void DispatchThink( edict_t *pent ); +extern void DispatchFrame( edict_t *pent ); +extern void DispatchBlocked( edict_t *pentBlocked, edict_t *pentOther ); +extern void DispatchSave( edict_t *pent, SAVERESTOREDATA *pSaveData ); +extern int DispatchRestore( edict_t *pent, SAVERESTOREDATA *pSaveData, int globalEntity ); +extern void DispatchObjectCollsionBox( edict_t *pent ); +extern void SaveWriteFields( SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ); +extern void SaveReadFields( SAVERESTOREDATA *pSaveData, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ); +extern void SaveGlobalState( SAVERESTOREDATA *pSaveData ); +extern void RestoreGlobalState( SAVERESTOREDATA *pSaveData ); +extern void ResetGlobalState( void ); + +typedef void (CBaseEntity::*BASEPTR)(void); +typedef void (CBaseEntity::*ENTITYFUNCPTR)(CBaseEntity *pOther ); +typedef void (CBaseEntity::*USEPTR)( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + +// +// EHANDLE. Safe way to point to CBaseEntities who may die between frames +// +class EHANDLE +{ +private: + edict_t *m_pent; + int m_serialnumber; +public: + edict_t *Get( void ); + edict_t *Set( edict_t *pent ); + + operator int (); + + operator CBaseEntity *(); + + CBaseEntity * operator = (CBaseEntity *pEntity); + CBaseEntity * operator ->(); +}; + + +// +// Converts a entvars_t * to a class pointer +// It will allocate the class and entity if necessary +// +template T * GetClassPtr( T *a ) +{ + entvars_t *pev = (entvars_t *)a; + + // allocate entity if necessary + if (pev == NULL) + pev = VARS(CREATE_ENTITY()); + + // get the private data + a = (T *)GET_PRIVATE(ENT(pev)); + + if (a == NULL) + { + // allocate private data + a = new(pev) T; + a->pev = pev; + } + return a; +} + +#endif //GLOBALS_H \ No newline at end of file diff --git a/server/global/hierarchy.h b/server/global/hierarchy.h new file mode 100644 index 00000000..51f08406 --- /dev/null +++ b/server/global/hierarchy.h @@ -0,0 +1,29 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// hierarchy.h - Not real code. +// Map of relationshep between classes. +//======================================================================= +#ifndef HIERARCHY_H +#define HIERARCHY_H + +/* +Class Hierachy + +CBaseEntity + CPointEntity + CBasePlayerAmmo + CBaseLogic + CBaseAnimating + CBasePlayerWeapon + CBaseToggle + CBaseButton + CBaseDoor + CBaseTrigger + CBasePlatTrain + CBaseMonster + CCycler + CBasePlayer + CCineMonster +*/ + +#endif //HIERARCHY_H \ No newline at end of file diff --git a/server/global/parent.cpp b/server/global/parent.cpp new file mode 100644 index 00000000..82dd1421 --- /dev/null +++ b/server/global/parent.cpp @@ -0,0 +1,324 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// parent.cpp - xash parent system +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "saverestore.h" +#include "client.h" +#include "decals.h" +#include "gamerules.h" +#include "game.h" +#include "defaults.h" + +CWorld *g_pWorld = NULL; //world pointer +extern BOOL CanAffect; +BOOL NeedUpdate( CBaseEntity *pEnt ); + +//======================================================================= +// debug utils +//======================================================================= +void GetPFlags( CBaseEntity* Ent ) //get parent system flags +{ + char szFlags[256] = "";//clear line + + CHECK_FLAG(PF_AFFECT); + CHECK_FLAG(PF_DESIRED); + CHECK_FLAG(PF_ACTION); + CHECK_FLAG(PF_MOVENONE); + CHECK_FLAG(PF_CORECTSPEED); + CHECK_FLAG(PF_MERGEPOS); + CHECK_FLAG(PF_ANGULAR); + CHECK_FLAG(PF_POSTVELOCITY); + CHECK_FLAG(PF_POSTAVELOCITY); + CHECK_FLAG(PF_SETTHINK); + CHECK_FLAG(PF_POSTAFFECT); + CHECK_FLAG(PF_LINKCHILD); + + ALERT(at_console, "=========/Xash Parent System Info:/=========\n"); + if(!Ent->pev->targetname)ALERT(at_console, "INFO: %s has follow flags:\n", STRING(Ent->pev->classname) ); + else ALERT(at_console, "INFO: %s with name %s has follow flags:\n", STRING(Ent->pev->classname), STRING(Ent->pev->targetname) ); + ALERT(at_console, "%s\n", szFlags ); + SHIFT; +} + +void GetPInfo( CBaseEntity* Ent ) //get parent system parameters +{ + int name = 0; + int parentname = 0; + int childsname = 0; + char parent[32] = ""; + char chname[256] = ""; + char buffer[32]; + + Vector pVelocity, pAvelocity, cVelocity, cAvelocity; + + if(Ent->pev->targetname)name = Ent->pev->targetname; + else name = Ent->pev->classname; + + if(Ent->m_pParent) + { + if(Ent->m_pParent->pev->targetname)parentname = Ent->m_pParent->pev->targetname; + else parentname = Ent->m_pParent->pev->classname; + pVelocity = Ent->m_pParent->pev->velocity; + pAvelocity = Ent->m_pParent->pev->avelocity; + sprintf( parent, "with parent \"%s\"", STRING(parentname) ); + } + else sprintf(parent, "without parent"); + + if( Ent->m_pChild ) + { + CBaseEntity *pInfo = Ent->m_pChild; + int sloopbreaker = MAX_CHILDS; + + while (pInfo) + { + if(pInfo->pev->targetname) childsname = pInfo->pev->targetname; + else childsname = pInfo->pev->classname; + pInfo = pInfo->m_pNextChild; + if(pInfo)sprintf(buffer, "\"%s\", ", STRING(childsname)); + else sprintf(buffer, "\"%s\"", STRING(childsname));//it,s last child + strcat( chname, buffer); + sloopbreaker--; + if (sloopbreaker <= 0)break; + } + } + else sprintf(chname, "this entity not have childs"); + + cVelocity = Ent->pev->velocity; + cAvelocity = Ent->pev->avelocity; + + //print info + Msg("=========/Xash Parent System Info:/=========\n"); + Msg("INFO: Entity \"%s\" %s, have following parameters:\n", STRING(name), parent ); + Msg("Child names: %s. Self Velocity: %g %g %g. Self Avelocity %g %g %g\n", chname, cVelocity.x, cVelocity.y, cVelocity.z, cAvelocity.x, cAvelocity.y, cAvelocity.z ); + if(Ent->m_pParent)Msg("Parent Velocity: %g %g %g. Parent Avelocity: %g %g %g.\n", pVelocity.x, pVelocity.y, pVelocity.z, pAvelocity.x, pAvelocity.y, pAvelocity.z ); + else SHIFT; +} + +//======================================================================= +// physics frame +//======================================================================= + +void PhysicsFrame( void ) +{ + CBaseEntity *pListMember; + + if ( !g_pWorld )return; + pListMember = g_pWorld; + + while ( pListMember->m_pLinkList )// handle the remaining entries in the list + { + FrameAffect(pListMember->m_pLinkList); + if (!(pListMember->m_pLinkList->pFlags & PF_LINKCHILD)) + { + CBaseEntity *pTemp = pListMember->m_pLinkList; + pListMember->m_pLinkList = pListMember->m_pLinkList->m_pLinkList; + pTemp->m_pLinkList = NULL; + } + else pListMember = pListMember->m_pLinkList; + } +} + +void PhysicsPostFrame( void ) +{ + CBaseEntity *pListMember; + int loopbreaker = 1024; //max edicts + if (CanAffect) ALERT(at_console, "Affect already applied ?!\n"); + CanAffect = TRUE; + if (!g_pWorld) return; + + pListMember = g_pWorld; + CBaseEntity *pNext; + + pListMember = g_pWorld->m_pLinkList; + + while (pListMember) + { + pNext = pListMember->m_pLinkList; + PostFrameAffect( pListMember ); + pListMember = pNext; + loopbreaker--; + if (loopbreaker <= 0)break; + } + CanAffect = FALSE; +} + +//======================================================================= +// affect & postaffect +//======================================================================= + +int FrameAffect( CBaseEntity *pEnt ) +{ + if (gpGlobals->frametime == 0)return 0; + if (!(pEnt->pFlags & PF_AFFECT)) return 0; + if (pEnt->m_fNextThink <= 0) + { + ClearBits(pEnt->pFlags, PF_AFFECT); + return 0; // cancelling think + } + float fFraction = 0; + if (pEnt->pev->movetype == MOVETYPE_PUSH) + { + if (pEnt->m_fNextThink <= pEnt->pev->ltime + gpGlobals->frametime) + fFraction = (pEnt->m_fNextThink - pEnt->pev->ltime)/gpGlobals->frametime; + } + + else if (pEnt->m_fNextThink <= gpGlobals->time + gpGlobals->frametime) + fFraction = (pEnt->m_fNextThink - gpGlobals->time)/gpGlobals->frametime; + if (fFraction) + { + if (pEnt->pFlags & PF_CORECTSPEED) + { + if (!(pEnt->pFlags & PF_POSTVELOCITY)) + { + pEnt->PostVelocity = pEnt->pev->velocity; + SetBits (pEnt->pFlags, PF_POSTVELOCITY); + } + + if (!(pEnt->pFlags & PF_POSTAVELOCITY)) + { + pEnt->PostAvelocity = pEnt->pev->avelocity; + SetBits (pEnt->pFlags, PF_POSTAVELOCITY); + } + + Vector vecVelTemp = pEnt->pev->velocity; + Vector vecAVelTemp = pEnt->pev->avelocity; + + if (pEnt->m_pParent) + { + pEnt->pev->velocity = (pEnt->pev->velocity - pEnt->m_pParent->pev->velocity)*fFraction + pEnt->m_pParent->pev->velocity; + pEnt->pev->avelocity = (pEnt->pev->avelocity - pEnt->m_pParent->pev->avelocity)*fFraction + pEnt->m_pParent->pev->avelocity; + } + else + { + pEnt->pev->velocity = pEnt->pev->velocity*fFraction; + pEnt->pev->avelocity = pEnt->pev->avelocity*fFraction; + } + + HandleAffect( pEnt, vecVelTemp - pEnt->pev->velocity, vecAVelTemp - pEnt->pev->avelocity ); + UTIL_SetPostAffect( pEnt ); + } + + UTIL_SetThink( pEnt ); + ClearBits(pEnt->pFlags, PF_AFFECT); + } + return 1; +} + +int PostFrameAffect( CBaseEntity *pEnt ) +{ + if (pEnt->pFlags & PF_DESIRED)ClearBits(pEnt->pFlags, PF_DESIRED); + else return 0; + if (pEnt->pFlags & PF_ACTION) + { + ClearBits(pEnt->pFlags, PF_ACTION); + pEnt->DesiredAction(); + if(NeedUpdate(pEnt)) SetBits(pEnt->m_pChild->pFlags, PF_MERGEPOS); + } + if (pEnt->pFlags & PF_POSTAFFECT) + { + ClearBits(pEnt->pFlags, PF_POSTAFFECT); + HandlePostAffect( pEnt ); + } + if (pEnt->pFlags & PF_SETTHINK) + { + ClearBits(pEnt->pFlags, PF_SETTHINK); + pEnt->Think(); + } + return 1; +} + +//======================================================================= +// affect handles +//======================================================================= + +void HandlePostAffect( CBaseEntity *pEnt ) +{ + if (pEnt->pFlags & PF_POSTVELOCITY) + { + pEnt->pev->velocity = pEnt->PostVelocity; + pEnt->PostVelocity = g_vecZero; + ClearBits (pEnt->pFlags, PF_POSTVELOCITY); + } + if (pEnt->pFlags & PF_POSTAVELOCITY) + { + pEnt->pev->avelocity = pEnt->PostAvelocity; + pEnt->PostAvelocity = g_vecZero; + ClearBits (pEnt->pFlags, PF_POSTAVELOCITY); + } + if (pEnt->pFlags & PF_MERGEPOS || pEnt->pFlags & PF_POSTORG) + { + UTIL_MergePos( pEnt ); + } + CBaseEntity *pChild; + for (pChild = pEnt->m_pChild; pChild != NULL; pChild = pChild->m_pNextChild) + HandlePostAffect( pChild ); +} + +void HandleAffect( CBaseEntity *pEnt, Vector vecAdjustVel, Vector vecAdjustAVel ) +{ + CBaseEntity *pChild; + for(pChild = pEnt->m_pChild; pChild != NULL; pChild = pChild->m_pNextChild) + { + if (!(pEnt->pFlags & PF_POSTVELOCITY)) + { + pChild->PostVelocity = pChild->pev->velocity; + SetBits (pEnt->pFlags, PF_POSTVELOCITY); + } + if (!(pEnt->pFlags & PF_POSTAVELOCITY)) + { + pChild->PostAvelocity = pChild->pev->avelocity; + SetBits (pEnt->pFlags, PF_POSTAVELOCITY); + } + pChild->pev->velocity = pChild->pev->velocity - vecAdjustVel; + pChild->pev->avelocity = pChild->pev->avelocity - vecAdjustAVel; + HandleAffect( pChild, vecAdjustVel, vecAdjustAVel ); + } +} + +//======================================================================= +// link operations +//======================================================================= +void LinkChild(CBaseEntity *pEnt) //add children to our list +{ + if (pEnt->m_pLinkList)return; + + if ( !g_pWorld )return;//too early ? + CBaseEntity *pListMember = g_pWorld; + + // find the last entry in the list + while (pListMember->m_pLinkList != NULL) pListMember = pListMember->m_pLinkList; + if (pListMember == pEnt)return; //entity has in list + + pListMember->m_pLinkList = pEnt;//add entity to the list. +} + +BOOL SynchLost( CBaseEntity *pEnt ) +{ + if(pEnt->pFlags & PF_PARENTMOVE)//moving parent + { + if(!pEnt->m_pParent) return FALSE; + if(pEnt->m_pParent->pev->origin != pEnt->PostOrigin) + { + if(pEnt->pev->solid != SOLID_BSP) return TRUE; + } + if(pEnt->m_pParent->pev->angles != pEnt->PostAngles) + { + if(pEnt->pev->solid != SOLID_BSP) return TRUE; + } + } + return FALSE; +} + +BOOL NeedUpdate( CBaseEntity *pEnt ) +{ + if( pEnt->m_pChild && pEnt->m_pChild->OffsetOrigin == g_vecZero)//potentially loser + { + if(pEnt->pev->origin != pEnt->m_pChild->pev->origin) + return TRUE; + } + return FALSE; +} \ No newline at end of file diff --git a/server/global/plane.h b/server/global/plane.h new file mode 100644 index 00000000..11ccbffe --- /dev/null +++ b/server/global/plane.h @@ -0,0 +1,176 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef PLANE_H +#define PLANE_H + +//========================================================= +// Plane +//========================================================= +typedef int SideType; + +// Used to represent sides of things like planes. +#define SIDE_FRONT 0 +#define SIDE_BACK 1 +#define SIDE_ON 2 + +#define VP_EPSILON 0.01f + + +class VPlane +{ +public: + + VPlane(); + VPlane(const Vector &vNormal, vec_t dist); + + void Init(const Vector &vNormal, vec_t dist); + void Init(const Vector &vNormal, const Vector &vecPoint); + + // Return the distance from the point to the plane. + vec_t DistTo(const Vector &vVec) const; + + // Flip the plane. + VPlane Flip(); + + // Get a point on the plane (normal*dist). + Vector GetPointOnPlane() const; + + // Copy. + VPlane& operator=(const VPlane &thePlane); + + // Snap the specified point to the plane (along the plane's normal). + Vector SnapPointToPlane(const Vector &vPoint) const; + + // Returns SIDE_ON, SIDE_FRONT, or SIDE_BACK. + // The epsilon for SIDE_ON can be passed in. + SideType GetPointSide(const Vector &vPoint, vec_t sideEpsilon=VP_EPSILON) const; + + // Returns SIDE_FRONT or SIDE_BACK. + SideType GetPointSideExact(const Vector &vPoint) const; + + // Classify the box with respect to the plane. + // Returns SIDE_ON, SIDE_FRONT, or SIDE_BACK + SideType BoxOnPlaneSide(const Vector &vMin, const Vector &vMax) const; + + +public: + + Vector m_Normal; + vec_t m_Dist; +}; + + +// ------------------------------------------------------------------------------------------- // +// Inlines. +// ------------------------------------------------------------------------------------------- // + +inline VPlane::VPlane() +{ +} + +inline VPlane::VPlane(const Vector &vNormal, vec_t dist) +{ + m_Normal = vNormal; + m_Dist = dist; +} + +inline void VPlane::Init(const Vector &vNormal, vec_t dist) +{ + m_Normal = vNormal; + m_Dist = dist; +} + +inline void VPlane::Init(const Vector &vNormal, const Vector &vecPoint) +{ + m_Normal = vNormal; + m_Dist = vNormal.Dot( vecPoint ); +} + +inline vec_t VPlane::DistTo(const Vector &vVec) const +{ + return DotProduct( vVec, m_Normal ) - m_Dist; +} + +inline VPlane VPlane::Flip() +{ + return VPlane(-m_Normal, -m_Dist); +} + +inline Vector VPlane::GetPointOnPlane() const +{ + return m_Normal * m_Dist; +} + +inline VPlane& VPlane::operator=(const VPlane &thePlane) +{ + m_Normal = thePlane.m_Normal; + m_Dist = thePlane.m_Dist; + return *this; +} + +inline Vector VPlane::SnapPointToPlane(const Vector &vPoint) const +{ + return vPoint - m_Normal * DistTo(vPoint); +} + +inline SideType VPlane::GetPointSide(const Vector &vPoint, vec_t sideEpsilon) const +{ + vec_t fDist; + + fDist = DistTo(vPoint); + if(fDist >= sideEpsilon) + return SIDE_FRONT; + else if(fDist <= -sideEpsilon) + return SIDE_BACK; + else return SIDE_ON; +} + +inline SideType VPlane::GetPointSideExact(const Vector &vPoint) const +{ + return DistTo(vPoint) > 0.0f ? SIDE_FRONT : SIDE_BACK; +} + +inline SideType VPlane::BoxOnPlaneSide(const Vector &vMin, const Vector &vMax) const +{ + int i, firstSide, side; + Vector vPoints[8] = + { + Vector(vMin.x, vMin.y, vMin.z), + Vector(vMin.x, vMin.y, vMax.z), + Vector(vMin.x, vMax.y, vMax.z), + Vector(vMin.x, vMax.y, vMin.z), + + Vector(vMax.x, vMin.y, vMin.z), + Vector(vMax.x, vMin.y, vMax.z), + Vector(vMax.x, vMax.y, vMax.z), + Vector(vMax.x, vMax.y, vMin.z), + }; + + firstSide = GetPointSideExact(vPoints[0]); + for(i=1; i < 8; i++) + { + side = GetPointSideExact(vPoints[i]); + + // Does the box cross the plane? + if(side != firstSide) + return SIDE_ON; + } + + // Ok, they're all on the same side, return that. + return firstSide; +} + + +#endif // PLANE_H diff --git a/server/global/saverestore.cpp b/server/global/saverestore.cpp new file mode 100644 index 00000000..fdb8e8ba --- /dev/null +++ b/server/global/saverestore.cpp @@ -0,0 +1,1235 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "defaults.h" +#include "saverestore.h" +#include +#include "shake.h" +#include "decals.h" +#include "player.h" +#include "baseweapon.h" +#include "gamerules.h" +#include "client.h" + +#define ENTVARS_COUNT (sizeof(gEntvarsDescription)/sizeof(gEntvarsDescription[0])) +CGlobalState gGlobalState; + +TYPEDESCRIPTION gEntvarsDescription[] = +{ + DEFINE_ENTITY_FIELD( classname, FIELD_STRING ), + DEFINE_ENTITY_GLOBAL_FIELD( globalname, FIELD_STRING ), + + DEFINE_ENTITY_FIELD( origin, FIELD_POSITION_VECTOR ), + DEFINE_ENTITY_FIELD( oldorigin, FIELD_POSITION_VECTOR ), + DEFINE_ENTITY_FIELD( velocity, FIELD_VECTOR ), + DEFINE_ENTITY_FIELD( movedir, FIELD_VECTOR ), + + DEFINE_ENTITY_FIELD( angles, FIELD_VECTOR ), + DEFINE_ENTITY_FIELD( avelocity, FIELD_VECTOR ), + DEFINE_ENTITY_FIELD( punchangle, FIELD_VECTOR ), + DEFINE_ENTITY_FIELD( v_angle, FIELD_VECTOR ), + DEFINE_ENTITY_FIELD( fixangle, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( idealpitch, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( pitch_speed, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( ideal_yaw, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( yaw_speed, FIELD_FLOAT ), + + DEFINE_ENTITY_FIELD( modelindex, FIELD_INTEGER ), + DEFINE_ENTITY_GLOBAL_FIELD( model, FIELD_MODELNAME ), + + DEFINE_ENTITY_FIELD( viewmodel, FIELD_MODELNAME ), + DEFINE_ENTITY_FIELD( weaponmodel, FIELD_MODELNAME ), + + DEFINE_ENTITY_FIELD( absmin, FIELD_POSITION_VECTOR ), + DEFINE_ENTITY_FIELD( absmax, FIELD_POSITION_VECTOR ), + DEFINE_ENTITY_GLOBAL_FIELD( mins, FIELD_VECTOR ), + DEFINE_ENTITY_GLOBAL_FIELD( maxs, FIELD_VECTOR ), + DEFINE_ENTITY_GLOBAL_FIELD( size, FIELD_VECTOR ), + + DEFINE_ENTITY_FIELD( ltime, FIELD_TIME ), + DEFINE_ENTITY_FIELD( nextthink, FIELD_TIME ), + + DEFINE_ENTITY_FIELD( solid, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( movetype, FIELD_INTEGER ), + + DEFINE_ENTITY_FIELD( skin, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( body, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( effects, FIELD_INTEGER ), + + DEFINE_ENTITY_FIELD( gravity, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( friction, FIELD_FLOAT ), + + DEFINE_ENTITY_FIELD( frame, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( scale, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( sequence, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( animtime, FIELD_TIME ), + DEFINE_ENTITY_FIELD( framerate, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( controller, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( blending, FIELD_INTEGER ), + + DEFINE_ENTITY_FIELD( rendermode, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( renderamt, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( rendercolor, FIELD_VECTOR ), + DEFINE_ENTITY_FIELD( renderfx, FIELD_INTEGER ), + + DEFINE_ENTITY_FIELD( health, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( frags, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( weapons, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( takedamage, FIELD_FLOAT ), + + DEFINE_ENTITY_FIELD( deadflag, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( view_ofs, FIELD_VECTOR ), + DEFINE_ENTITY_FIELD( button, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( impulse, FIELD_INTEGER ), + + DEFINE_ENTITY_FIELD( chain, FIELD_EDICT ), + DEFINE_ENTITY_FIELD( dmg_inflictor, FIELD_EDICT ), + DEFINE_ENTITY_FIELD( enemy, FIELD_EDICT ), + DEFINE_ENTITY_FIELD( aiment, FIELD_EDICT ), + DEFINE_ENTITY_FIELD( owner, FIELD_EDICT ), + DEFINE_ENTITY_FIELD( groundentity, FIELD_EDICT ), + + DEFINE_ENTITY_FIELD( spawnflags, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( flags, FIELD_FLOAT ), + + DEFINE_ENTITY_FIELD( colormap, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( team, FIELD_INTEGER ), + + DEFINE_ENTITY_FIELD( max_health, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( teleport_time, FIELD_TIME ), + DEFINE_ENTITY_FIELD( armortype, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( armorvalue, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( waterlevel, FIELD_INTEGER ), + DEFINE_ENTITY_FIELD( watertype, FIELD_INTEGER ), + + // Having these fields be local to the individual levels makes it easier to test those levels individually. + DEFINE_ENTITY_GLOBAL_FIELD( target, FIELD_STRING ), + DEFINE_ENTITY_GLOBAL_FIELD( targetname, FIELD_STRING ), + DEFINE_ENTITY_FIELD( netname, FIELD_STRING ), + DEFINE_ENTITY_FIELD( message, FIELD_STRING ), + + DEFINE_ENTITY_FIELD( dmg_take, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( dmg_save, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( dmg, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( dmgtime, FIELD_TIME ), + + DEFINE_ENTITY_FIELD( noise, FIELD_SOUNDNAME ), + DEFINE_ENTITY_FIELD( noise1, FIELD_SOUNDNAME ), + DEFINE_ENTITY_FIELD( noise2, FIELD_SOUNDNAME ), + DEFINE_ENTITY_FIELD( noise3, FIELD_SOUNDNAME ), + DEFINE_ENTITY_FIELD( speed, FIELD_FLOAT ), + DEFINE_ENTITY_FIELD( air_finished, FIELD_TIME ), + DEFINE_ENTITY_FIELD( pain_finished, FIELD_TIME ), + DEFINE_ENTITY_FIELD( radsuit_finished, FIELD_TIME ), +}; + + +// -------------------------------------------------------------- +// +// CSave +// +// -------------------------------------------------------------- +static int gSizes[FIELD_TYPECOUNT] = +{ + sizeof(float), // FIELD_FLOAT + sizeof(int), // FIELD_STRING + sizeof(int), // FIELD_ENTITY + sizeof(int), // FIELD_CLASSPTR + sizeof(int), // FIELD_EHANDLE + sizeof(int), // FIELD_entvars_t + sizeof(int), // FIELD_EDICT + sizeof(float)*3, // FIELD_VECTOR + sizeof(float)*3, // FIELD_POSITION_VECTOR + sizeof(int *), // FIELD_POINTER + sizeof(int), // FIELD_INTEGER + sizeof(int *), // FIELD_FUNCTION + sizeof(int), // FIELD_BOOLEAN + sizeof(short), // FIELD_SHORT + sizeof(char), // FIELD_CHARACTER + sizeof(float), // FIELD_TIME + sizeof(int), // FIELD_MODELNAME + sizeof(int), // FIELD_SOUNDNAME + sizeof(float)*2, // FIELD_RANGE +}; + + +// Base class includes common SAVERESTOREDATA pointer, and manages the entity table +CSaveRestoreBuffer :: CSaveRestoreBuffer( void ) +{ + m_pdata = NULL; +} + + +CSaveRestoreBuffer :: CSaveRestoreBuffer( SAVERESTOREDATA *pdata ) +{ + m_pdata = pdata; +} + + +CSaveRestoreBuffer :: ~CSaveRestoreBuffer( void ) +{ +} + +int CSaveRestoreBuffer :: EntityIndex( CBaseEntity *pEntity ) +{ + if ( pEntity == NULL ) + return -1; + return EntityIndex( pEntity->pev ); +} + + +int CSaveRestoreBuffer :: EntityIndex( entvars_t *pevLookup ) +{ + if ( pevLookup == NULL ) + return -1; + return EntityIndex( ENT( pevLookup ) ); +} + +int CSaveRestoreBuffer :: EntityIndex( EOFFSET eoLookup ) +{ + return EntityIndex( ENT( eoLookup ) ); +} + + +int CSaveRestoreBuffer :: EntityIndex( edict_t *pentLookup ) +{ + if ( !m_pdata || pentLookup == NULL ) + return -1; + + int i; + ENTITYTABLE *pTable; + + for ( i = 0; i < m_pdata->tableCount; i++ ) + { + pTable = m_pdata->pTable + i; + if ( pTable->pent == pentLookup ) + return i; + } + return -1; +} + + +edict_t *CSaveRestoreBuffer :: EntityFromIndex( int entityIndex ) +{ + if ( !m_pdata || entityIndex < 0 ) + return NULL; + + int i; + ENTITYTABLE *pTable; + + for ( i = 0; i < m_pdata->tableCount; i++ ) + { + pTable = m_pdata->pTable + i; + if ( pTable->id == entityIndex ) + return pTable->pent; + } + return NULL; +} + + +int CSaveRestoreBuffer :: EntityFlagsSet( int entityIndex, int flags ) +{ + if ( !m_pdata || entityIndex < 0 ) + return 0; + if ( entityIndex > m_pdata->tableCount ) + return 0; + + m_pdata->pTable[ entityIndex ].flags |= flags; + + return m_pdata->pTable[ entityIndex ].flags; +} + + +void CSaveRestoreBuffer :: BufferRewind( int size ) +{ + if ( !m_pdata ) + return; + + if ( m_pdata->size < size ) + size = m_pdata->size; + + m_pdata->pCurrentData -= size; + m_pdata->size -= size; +} + +#ifndef _WIN32 +extern "C" { +unsigned _rotr ( unsigned val, int shift) +{ + register unsigned lobit; /* non-zero means lo bit set */ + register unsigned num = val; /* number to rotate */ + + shift &= 0x1f; /* modulo 32 -- this will also make + negative shifts work */ + + while (shift--) { + lobit = num & 1; /* get high bit */ + num >>= 1; /* shift right one bit */ + if (lobit) + num |= 0x80000000; /* set hi bit if lo bit was set */ + } + + return num; +} +} +#endif + +unsigned int CSaveRestoreBuffer :: HashString( const char *pszToken ) +{ + unsigned int hash = 0; + + while ( *pszToken ) + hash = _rotr( hash, 4 ) ^ *pszToken++; + + return hash; +} + +unsigned short CSaveRestoreBuffer :: TokenHash( const char *pszToken ) +{ + unsigned short hash = (unsigned short)(HashString( pszToken ) % (unsigned)m_pdata->tokenCount ); + +#if _DEBUG + static int tokensparsed = 0; + tokensparsed++; + if ( !m_pdata->tokenCount || !m_pdata->pTokens ) + ALERT( at_error, "No token table array in TokenHash()!" ); +#endif + + for ( int i=0; itokenCount; i++ ) + { +#if _DEBUG + static BOOL beentheredonethat = FALSE; + if ( i > 50 && !beentheredonethat ) + { + beentheredonethat = TRUE; + ALERT( at_error, "CSaveRestoreBuffer :: TokenHash() is getting too full!" ); + } +#endif + + int index = hash + i; + if ( index >= m_pdata->tokenCount ) + index -= m_pdata->tokenCount; + + if ( !m_pdata->pTokens[index] || strcmp( pszToken, m_pdata->pTokens[index] ) == 0 ) + { + m_pdata->pTokens[index] = (char *)pszToken; + return index; + } + } + + // Token hash table full!!! + // [Consider doing overflow table(s) after the main table & limiting linear hash table search] + ALERT( at_error, "CSaveRestoreBuffer :: TokenHash() is COMPLETELY FULL!" ); + return 0; +} + +void CSave :: WriteData( const char *pname, int size, const char *pdata ) +{ + BufferField( pname, size, pdata ); +} + + +void CSave :: WriteShort( const char *pname, const short *data, int count ) +{ + BufferField( pname, sizeof(short) * count, (const char *)data ); +} + + +void CSave :: WriteInt( const char *pname, const int *data, int count ) +{ + BufferField( pname, sizeof(int) * count, (const char *)data ); +} + + +void CSave :: WriteFloat( const char *pname, const float *data, int count ) +{ + BufferField( pname, sizeof(float) * count, (const char *)data ); +} + + +void CSave :: WriteTime( const char *pname, const float *data, int count ) +{ + int i; + Vector tmp, input; + + BufferHeader( pname, sizeof(float) * count ); + for ( i = 0; i < count; i++ ) + { + float tmp = data[0]; + + // Always encode time as a delta from the current time so it can be re-based if loaded in a new level + // Times of 0 are never written to the file, so they will be restored as 0, not a relative time + if ( m_pdata ) + tmp -= m_pdata->time; + + BufferData( (const char *)&tmp, sizeof(float) ); + data ++; + } +} + + +void CSave :: WriteString( const char *pname, const char *pdata ) +{ +#ifdef TOKENIZE + short token = (short)TokenHash( pdata ); + WriteShort( pname, &token, 1 ); +#else + BufferField( pname, strlen(pdata) + 1, pdata ); +#endif +} + + +void CSave :: WriteString( const char *pname, const int *stringId, int count ) +{ + int i, size; + +#ifdef TOKENIZE + short token = (short)TokenHash( STRING( *stringId ) ); + WriteShort( pname, &token, 1 ); +#else +#if 0 + if ( count != 1 ) + ALERT( at_error, "No string arrays!\n" ); + WriteString( pname, (char *)STRING(*stringId) ); +#endif + + size = 0; + for ( i = 0; i < count; i++ ) + size += strlen( STRING( stringId[i] ) ) + 1; + + BufferHeader( pname, size ); + for ( i = 0; i < count; i++ ) + { + const char *pString = STRING(stringId[i]); + BufferData( pString, strlen(pString)+1 ); + } +#endif +} + + +void CSave :: WriteVector( const char *pname, const Vector &value ) +{ + WriteVector( pname, &value.x, 1 ); +} + +void CSave :: WriteRange( const char *pname, const RandomRange &value ) +{ + WriteRange( pname, &value.m_flMin, 1 ); +} + +void CSave :: WriteRange( const char *pname, const float *value, int count ) +{ + BufferHeader( pname, sizeof(float) * 2 * count ); + BufferData( (const char *)value, sizeof(float) * 2 * count ); +} + +void CSave :: WriteVector( const char *pname, const float *value, int count ) +{ + BufferHeader( pname, sizeof(float) * 3 * count ); + BufferData( (const char *)value, sizeof(float) * 3 * count ); +} + + + +void CSave :: WritePositionVector( const char *pname, const Vector &value ) +{ + + if ( m_pdata && m_pdata->fUseLandmark ) + { + Vector tmp = value - m_pdata->vecLandmarkOffset; + WriteVector( pname, tmp ); + } + + WriteVector( pname, value ); +} + + +void CSave :: WritePositionVector( const char *pname, const float *value, int count ) +{ + int i; + Vector tmp, input; + + BufferHeader( pname, sizeof(float) * 3 * count ); + for ( i = 0; i < count; i++ ) + { + Vector tmp( value[0], value[1], value[2] ); + + if ( m_pdata && m_pdata->fUseLandmark ) + tmp = tmp - m_pdata->vecLandmarkOffset; + + BufferData( (const char *)&tmp.x, sizeof(float) * 3 ); + value += 3; + } +} + + +void CSave :: WriteFunction( const char* cname, const char *pname, const int *data, int count ) +{ + const char *functionName; + + functionName = NAME_FOR_FUNCTION( *data ); + if ( functionName ) + BufferField( pname, strlen(functionName) + 1, functionName ); + else ALERT( at_error, "Member \"%s\" of \"%s\" contains an invalid function pointer %p!", pname, cname, *data ); +} + + +void EntvarsKeyvalue( entvars_t *pev, KeyValueData *pkvd ) +{ + int i; + TYPEDESCRIPTION *pField; + + for ( i = 0; i < ENTVARS_COUNT; i++ ) + { + pField = &gEntvarsDescription[i]; + + if ( !stricmp( pField->fieldName, pkvd->szKeyName ) ) + { + switch( pField->fieldType ) + { + case FIELD_MODELNAME: + case FIELD_SOUNDNAME: + case FIELD_STRING: + (*(int *)((char *)pev + pField->fieldOffset)) = ALLOC_STRING( pkvd->szValue ); + break; + + case FIELD_TIME: + case FIELD_FLOAT: + (*(float *)((char *)pev + pField->fieldOffset)) = atof( pkvd->szValue ); + break; + + case FIELD_INTEGER: + (*(int *)((char *)pev + pField->fieldOffset)) = atoi( pkvd->szValue ); + break; + + case FIELD_POSITION_VECTOR: + case FIELD_VECTOR: + UTIL_StringToVector( (float *)((char *)pev + pField->fieldOffset), pkvd->szValue ); + break; + + default: + case FIELD_EVARS: + case FIELD_CLASSPTR: + case FIELD_EDICT: + case FIELD_ENTITY: + case FIELD_POINTER: + ALERT( at_error, "Bad field in entity!!\n" ); + break; + } + pkvd->fHandled = TRUE; + return; + } + } +} + + + +int CSave :: WriteEntVars( const char *pname, entvars_t *pev ) +{ + if (pev->targetname) + return WriteFields( STRING(pev->targetname), pname, pev, gEntvarsDescription, ENTVARS_COUNT ); + else + return WriteFields( STRING(pev->classname), pname, pev, gEntvarsDescription, ENTVARS_COUNT ); +} + + + +int CSave :: WriteFields( const char *cname, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ) +{ + int i, j, actualCount, emptyCount; + TYPEDESCRIPTION *pTest; + int entityArray[MAX_ENTITYARRAY]; + + // Precalculate the number of empty fields + emptyCount = 0; + for ( i = 0; i < fieldCount; i++ ) + { + void *pOutputData; + pOutputData = ((char *)pBaseData + pFields[i].fieldOffset ); + if ( DataEmpty( (const char *)pOutputData, pFields[i].fieldSize * gSizes[pFields[i].fieldType] ) ) + emptyCount++; + } + + // Empty fields will not be written, write out the actual number of fields to be written + actualCount = fieldCount - emptyCount; + WriteInt( pname, &actualCount, 1 ); + + for ( i = 0; i < fieldCount; i++ ) + { + void *pOutputData; + pTest = &pFields[ i ]; + pOutputData = ((char *)pBaseData + pTest->fieldOffset ); + + // UNDONE: Must we do this twice? + if ( DataEmpty( (const char *)pOutputData, pTest->fieldSize * gSizes[pTest->fieldType] ) ) + continue; + + switch( pTest->fieldType ) + { + case FIELD_FLOAT: + WriteFloat( pTest->fieldName, (float *)pOutputData, pTest->fieldSize ); + break; + case FIELD_TIME: + WriteTime( pTest->fieldName, (float *)pOutputData, pTest->fieldSize ); + break; + case FIELD_MODELNAME: + case FIELD_SOUNDNAME: + case FIELD_STRING: + WriteString( pTest->fieldName, (int *)pOutputData, pTest->fieldSize ); + break; + case FIELD_CLASSPTR: + case FIELD_EVARS: + case FIELD_EDICT: + case FIELD_ENTITY: + case FIELD_EHANDLE: + if ( pTest->fieldSize > MAX_ENTITYARRAY ) + ALERT( at_error, "Can't save more than %d entities in an array!!!\n", MAX_ENTITYARRAY ); + for ( j = 0; j < pTest->fieldSize; j++ ) + { + switch( pTest->fieldType ) + { + case FIELD_EVARS: + entityArray[j] = EntityIndex( ((entvars_t **)pOutputData)[j] ); + break; + case FIELD_CLASSPTR: + entityArray[j] = EntityIndex( ((CBaseEntity **)pOutputData)[j] ); + break; + case FIELD_EDICT: + entityArray[j] = EntityIndex( ((edict_t **)pOutputData)[j] ); + break; + case FIELD_ENTITY: + entityArray[j] = EntityIndex( ((EOFFSET *)pOutputData)[j] ); + break; + case FIELD_EHANDLE: + entityArray[j] = EntityIndex( (CBaseEntity *)(((EHANDLE *)pOutputData)[j]) ); + break; + } + } + WriteInt( pTest->fieldName, entityArray, pTest->fieldSize ); + break; + case FIELD_POSITION_VECTOR: + WritePositionVector( pTest->fieldName, (float *)pOutputData, pTest->fieldSize ); + break; + case FIELD_VECTOR: + WriteVector( pTest->fieldName, (float *)pOutputData, pTest->fieldSize ); + break; + case FIELD_RANGE: + WriteRange( pTest->fieldName, (float *)pOutputData, pTest->fieldSize ); + break; + case FIELD_BOOLEAN: + case FIELD_INTEGER: + WriteInt( pTest->fieldName, (int *)pOutputData, pTest->fieldSize ); + break; + + case FIELD_SHORT: + WriteData( pTest->fieldName, 2 * pTest->fieldSize, ((char *)pOutputData) ); + break; + + case FIELD_CHARACTER: + WriteData( pTest->fieldName, pTest->fieldSize, ((char *)pOutputData) ); + break; + + // For now, just write the address out, we're not going to change memory while doing this yet! + case FIELD_POINTER: + WriteInt( pTest->fieldName, (int *)(char *)pOutputData, pTest->fieldSize ); + break; + + case FIELD_FUNCTION: + WriteFunction( cname, pTest->fieldName, (int *)(char *)pOutputData, pTest->fieldSize ); + break; + default: + ALERT( at_error, "Bad field type\n" ); + } + } + + return 1; +} + + +void CSave :: BufferString( char *pdata, int len ) +{ + char c = 0; + + BufferData( pdata, len ); // Write the string + BufferData( &c, 1 ); // Write a null terminator +} + + +int CSave :: DataEmpty( const char *pdata, int size ) +{ + for ( int i = 0; i < size; i++ ) + { + if ( pdata[i] ) + return 0; + } + return 1; +} + + +void CSave :: BufferField( const char *pname, int size, const char *pdata ) +{ + BufferHeader( pname, size ); + BufferData( pdata, size ); +} + + +void CSave :: BufferHeader( const char *pname, int size ) +{ + short hashvalue = TokenHash( pname ); + if ( size > 1<<(sizeof(short)*8) ) + ALERT( at_error, "CSave :: BufferHeader() size parameter exceeds 'short'!" ); + BufferData( (const char *)&size, sizeof(short) ); + BufferData( (const char *)&hashvalue, sizeof(short) ); +} + + +void CSave :: BufferData( const char *pdata, int size ) +{ + if ( !m_pdata ) + return; + + if ( m_pdata->size + size > m_pdata->bufferSize ) + { + ALERT( at_error, "Save/Restore overflow!" ); + m_pdata->size = m_pdata->bufferSize; + return; + } + + memcpy( m_pdata->pCurrentData, pdata, size ); + m_pdata->pCurrentData += size; + m_pdata->size += size; +} + + + +// -------------------------------------------------------------- +// +// CRestore +// +// -------------------------------------------------------------- + +int CRestore::ReadField( void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount, int startField, int size, char *pName, void *pData ) +{ + int i, j, stringCount, fieldNumber, entityIndex; + TYPEDESCRIPTION *pTest; + float time, timeData; + Vector position; + edict_t *pent; + char *pString; + + time = 0; + position = Vector(0,0,0); + + if ( m_pdata ) + { + time = m_pdata->time; + if ( m_pdata->fUseLandmark ) + position = m_pdata->vecLandmarkOffset; + } + + for ( i = 0; i < fieldCount; i++ ) + { + fieldNumber = (i+startField)%fieldCount; + pTest = &pFields[ fieldNumber ]; + if ( !stricmp( pTest->fieldName, pName ) ) + { + if ( !m_global || !(pTest->flags & FTYPEDESC_GLOBAL) ) + { + for ( j = 0; j < pTest->fieldSize; j++ ) + { + void *pOutputData = ((char *)pBaseData + pTest->fieldOffset + (j*gSizes[pTest->fieldType]) ); + void *pInputData = (char *)pData + j * gSizes[pTest->fieldType]; + + switch( pTest->fieldType ) + { + case FIELD_TIME: + timeData = *(float *)pInputData; + // Re-base time variables + timeData += time; + *((float *)pOutputData) = timeData; + break; + case FIELD_FLOAT: + *((float *)pOutputData) = *(float *)pInputData; + break; + case FIELD_MODELNAME: + case FIELD_SOUNDNAME: + case FIELD_STRING: + // Skip over j strings + pString = (char *)pData; + for ( stringCount = 0; stringCount < j; stringCount++ ) + { + while (*pString) + pString++; + pString++; + } + pInputData = pString; + if ( strlen( (char *)pInputData ) == 0 ) + *((int *)pOutputData) = 0; + else + { + int string; + + string = ALLOC_STRING( (char *)pInputData ); + + *((int *)pOutputData) = string; + + if ( !FStringNull( string ) && m_precache ) + { + if ( pTest->fieldType == FIELD_MODELNAME ) + UTIL_PrecacheModel( string ); + else if ( pTest->fieldType == FIELD_SOUNDNAME ) + UTIL_PrecacheSound( string ); + } + } + break; + case FIELD_EVARS: + entityIndex = *( int *)pInputData; + pent = EntityFromIndex( entityIndex ); + if ( pent ) + *((entvars_t **)pOutputData) = VARS(pent); + else + *((entvars_t **)pOutputData) = NULL; + break; + case FIELD_CLASSPTR: + entityIndex = *( int *)pInputData; + pent = EntityFromIndex( entityIndex ); + if ( pent ) + *((CBaseEntity **)pOutputData) = CBaseEntity::Instance(pent); + else + { + *((CBaseEntity **)pOutputData) = NULL; + if (entityIndex != -1) ALERT(at_console, "## Restore: invalid entitynum %d\n", entityIndex); + } + break; + case FIELD_EDICT: + entityIndex = *( int *)pInputData; + pent = EntityFromIndex( entityIndex ); + *((edict_t **)pOutputData) = pent; + break; + case FIELD_EHANDLE: + // Input and Output sizes are different! + pOutputData = (char *)pOutputData + j*(sizeof(EHANDLE) - gSizes[pTest->fieldType]); + entityIndex = *( int *)pInputData; + pent = EntityFromIndex( entityIndex ); + if ( pent ) + *((EHANDLE *)pOutputData) = CBaseEntity::Instance(pent); + else + *((EHANDLE *)pOutputData) = NULL; + break; + case FIELD_ENTITY: + entityIndex = *( int *)pInputData; + pent = EntityFromIndex( entityIndex ); + if ( pent ) + *((EOFFSET *)pOutputData) = OFFSET(pent); + else + *((EOFFSET *)pOutputData) = 0; + break; + case FIELD_RANGE: + ((float *)pOutputData)[0] = ((float *)pInputData)[0]; + ((float *)pOutputData)[1] = ((float *)pInputData)[1]; + break; + case FIELD_VECTOR: + ((float *)pOutputData)[0] = ((float *)pInputData)[0]; + ((float *)pOutputData)[1] = ((float *)pInputData)[1]; + ((float *)pOutputData)[2] = ((float *)pInputData)[2]; + break; + case FIELD_POSITION_VECTOR: + ((float *)pOutputData)[0] = ((float *)pInputData)[0] + position.x; + ((float *)pOutputData)[1] = ((float *)pInputData)[1] + position.y; + ((float *)pOutputData)[2] = ((float *)pInputData)[2] + position.z; + break; + + case FIELD_BOOLEAN: + case FIELD_INTEGER: + *((int *)pOutputData) = *( int *)pInputData; + break; + + case FIELD_SHORT: + *((short *)pOutputData) = *( short *)pInputData; + break; + + case FIELD_CHARACTER: + *((char *)pOutputData) = *( char *)pInputData; + break; + + case FIELD_POINTER: + *((int *)pOutputData) = *( int *)pInputData; + break; + case FIELD_FUNCTION: + if ( strlen( (char *)pInputData ) == 0 ) + *((int *)pOutputData) = 0; + else + *((int *)pOutputData) = FUNCTION_FROM_NAME( (char *)pInputData ); + break; + + default: + ALERT( at_error, "Bad field type\n" ); + } + } + } +#if 0 + else + { + ALERT( at_debug, "Skipping global field %s\n", pName ); + } +#endif + return fieldNumber; + } + } + + return -1; +} + + +int CRestore::ReadEntVars( const char *pname, entvars_t *pev ) +{ + return ReadFields( pname, pev, gEntvarsDescription, ENTVARS_COUNT ); +} + + +int CRestore::ReadFields( const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ) +{ + unsigned short i, token; + int lastField, fileCount; + HEADER header; + + i = ReadShort(); + ASSERT( i == sizeof(int) ); // First entry should be an int + + token = ReadShort(); + + // Check the struct name + if ( token != TokenHash(pname) ) // Field Set marker + { +// ALERT( at_error, "Expected %s found %s!\n", pname, BufferPointer() ); + BufferRewind( 2*sizeof(short) ); + return 0; + } + + // Skip over the struct name + fileCount = ReadInt(); // Read field count + + lastField = 0; // Make searches faster, most data is read/written in the same order + + // Clear out base data + for ( i = 0; i < fieldCount; i++ ) + { + // Don't clear global fields + if ( !m_global || !(pFields[i].flags & FTYPEDESC_GLOBAL) ) + memset( ((char *)pBaseData + pFields[i].fieldOffset), 0, pFields[i].fieldSize * gSizes[pFields[i].fieldType] ); + } + + for ( i = 0; i < fileCount; i++ ) + { + BufferReadHeader( &header ); + lastField = ReadField( pBaseData, pFields, fieldCount, lastField, header.size, m_pdata->pTokens[header.token], header.pData ); + lastField++; + } + + return 1; +} + + +void CRestore::BufferReadHeader( HEADER *pheader ) +{ + ASSERT( pheader!=NULL ); + pheader->size = ReadShort(); // Read field size + pheader->token = ReadShort(); // Read field name token + pheader->pData = BufferPointer(); // Field Data is next + BufferSkipBytes( pheader->size ); // Advance to next field +} + + +short CRestore::ReadShort( void ) +{ + short tmp = 0; + + BufferReadBytes( (char *)&tmp, sizeof(short) ); + + return tmp; +} + +int CRestore::ReadInt( void ) +{ + int tmp = 0; + + BufferReadBytes( (char *)&tmp, sizeof(int) ); + + return tmp; +} + +int CRestore::ReadNamedInt( const char *pName ) +{ + HEADER header; + + BufferReadHeader( &header ); + return ((int *)header.pData)[0]; +} + +char *CRestore::ReadNamedString( const char *pName ) +{ + HEADER header; + + BufferReadHeader( &header ); +#ifdef TOKENIZE + return (char *)(m_pdata->pTokens[*(short *)header.pData]); +#else + return (char *)header.pData; +#endif +} + + +char *CRestore::BufferPointer( void ) +{ + if ( !m_pdata ) + return NULL; + + return m_pdata->pCurrentData; +} + +void CRestore::BufferReadBytes( char *pOutput, int size ) +{ + ASSERT( m_pdata !=NULL ); + + if ( !m_pdata || Empty() ) + return; + + if ( (m_pdata->size + size) > m_pdata->bufferSize ) + { + ALERT( at_error, "Restore overflow!" ); + m_pdata->size = m_pdata->bufferSize; + return; + } + + if ( pOutput ) + memcpy( pOutput, m_pdata->pCurrentData, size ); + m_pdata->pCurrentData += size; + m_pdata->size += size; +} + + +void CRestore::BufferSkipBytes( int bytes ) +{ + BufferReadBytes( NULL, bytes ); +} + +int CRestore::BufferSkipZString( void ) +{ + char *pszSearch; + int len; + + if ( !m_pdata ) + return 0; + + int maxLen = m_pdata->bufferSize - m_pdata->size; + + len = 0; + pszSearch = m_pdata->pCurrentData; + while ( *pszSearch++ && len < maxLen ) + len++; + + len++; + + BufferSkipBytes( len ); + + return len; +} + +int CRestore::BufferCheckZString( const char *string ) +{ + if ( !m_pdata ) + return 0; + + int maxLen = m_pdata->bufferSize - m_pdata->size; + int len = strlen( string ); + if ( len <= maxLen ) + { + if ( !strncmp( string, m_pdata->pCurrentData, len ) ) + return 1; + } + return 0; +} + + +//======================================================================= +// global entity states +//======================================================================= +CGlobalState::CGlobalState( void ) +{ + Reset(); +} + +void CGlobalState::Reset( void ) +{ + m_pList = NULL; + m_listCount = 0; +} + +globalentity_t *CGlobalState :: Find( string_t globalname ) +{ + if ( !globalname ) + return NULL; + + globalentity_t *pTest; + const char *pEntityName = STRING(globalname); + + + pTest = m_pList; + while ( pTest ) + { + if ( FStrEq( pEntityName, pTest->name ) ) + break; + + pTest = pTest->pNext; + } + + return pTest; +} + +void CGlobalState :: DumpGlobals( void ) +{ + static char *estates[] = { "Off", "On", "Dead" }; + globalentity_t *pTest; + + ALERT( at_debug, "-- Globals --\n" ); + pTest = m_pList; + while ( pTest ) + { + Msg( "%s: %s (%s)\n", pTest->name, pTest->levelName, estates[pTest->state] ); + pTest = pTest->pNext; + } +} + +void CGlobalState :: EntityAdd( string_t globalname, string_t mapName, GLOBALESTATE state ) +{ + ASSERT( !Find(globalname) ); + + globalentity_t *pNewEntity = (globalentity_t *)CALLOC( sizeof( globalentity_t ), 1 ); + ASSERT( pNewEntity != NULL ); + pNewEntity->pNext = m_pList; + m_pList = pNewEntity; + strcpy( pNewEntity->name, STRING( globalname ) ); + strcpy( pNewEntity->levelName, STRING(mapName) ); + pNewEntity->state = state; + m_listCount++; +} + + +void CGlobalState :: EntitySetState( string_t globalname, GLOBALESTATE state ) +{ + globalentity_t *pEnt = Find( globalname ); + if ( pEnt ) pEnt->state = state; +} + + +const globalentity_t *CGlobalState :: EntityFromTable( string_t globalname ) +{ + globalentity_t *pEnt = Find( globalname ); + return pEnt; +} + + +GLOBALESTATE CGlobalState :: EntityGetState( string_t globalname ) +{ + globalentity_t *pEnt = Find( globalname ); + if ( pEnt ) return pEnt->state; + + return GLOBAL_OFF; +} + +TYPEDESCRIPTION CGlobalState::m_SaveData[] = +{ + DEFINE_FIELD( CGlobalState, m_listCount, FIELD_INTEGER ), +}; + +TYPEDESCRIPTION gGlobalEntitySaveData[] = +{ + DEFINE_ARRAY( globalentity_t, name, FIELD_CHARACTER, 64 ), + DEFINE_ARRAY( globalentity_t, levelName, FIELD_CHARACTER, 32 ), + DEFINE_FIELD( globalentity_t, state, FIELD_INTEGER ), +}; + + +int CGlobalState::Save( CSave &save ) +{ + int i; + globalentity_t *pEntity; + + if ( !save.WriteFields( "cGLOBAL", "GLOBAL", this, m_SaveData, ARRAYSIZE(m_SaveData) ) ) + return 0; + + pEntity = m_pList; + for ( i = 0; i < m_listCount && pEntity; i++ ) + { + if ( !save.WriteFields( "cGENT", "GENT", pEntity, gGlobalEntitySaveData, ARRAYSIZE(gGlobalEntitySaveData) ) ) + return 0; + + pEntity = pEntity->pNext; + } + + return 1; +} + +int CGlobalState::Restore( CRestore &restore ) +{ + int i, listCount; + globalentity_t tmpEntity; + + ClearStates(); + if ( !restore.ReadFields( "GLOBAL", this, m_SaveData, ARRAYSIZE(m_SaveData) ) ) return 0; + + listCount = m_listCount; // Get new list count + m_listCount = 0; // Clear loaded data + + for ( i = 0; i < listCount; i++ ) + { + if ( !restore.ReadFields( "GENT", &tmpEntity, gGlobalEntitySaveData, ARRAYSIZE(gGlobalEntitySaveData) ) ) + return 0; + EntityAdd( MAKE_STRING(tmpEntity.name), MAKE_STRING(tmpEntity.levelName), tmpEntity.state ); + } + return 1; +} + +void CGlobalState::EntityUpdate( string_t globalname, string_t mapname ) +{ + globalentity_t *pEnt = Find( globalname ); + if ( pEnt ) strcpy( pEnt->levelName, STRING(mapname) ); +} + + +void CGlobalState::ClearStates( void ) +{ + globalentity_t *pFree = m_pList; + while ( pFree ) + { + globalentity_t *pNext = pFree->pNext; + FREE( pFree ); + pFree = pNext; + } + Reset(); +} + + +void SaveGlobalState( SAVERESTOREDATA *pSaveData ) +{ + CSave saveHelper( pSaveData ); + gGlobalState.Save( saveHelper ); +} + + +void RestoreGlobalState( SAVERESTOREDATA *pSaveData ) +{ + CRestore restoreHelper( pSaveData ); + gGlobalState.Restore( restoreHelper ); +} + + +void ResetGlobalState( void ) +{ + gGlobalState.ClearStates(); + gInitHUD = TRUE; // Init the HUD on a new game / load game +} \ No newline at end of file diff --git a/server/global/saverestore.h b/server/global/saverestore.h new file mode 100644 index 00000000..350fd9b6 --- /dev/null +++ b/server/global/saverestore.h @@ -0,0 +1,165 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= +#ifndef SAVERESTORE_H +#define SAVERESTORE_H + +class CBaseEntity; + +class CSaveRestoreBuffer +{ +public: + CSaveRestoreBuffer( void ); + CSaveRestoreBuffer( SAVERESTOREDATA *pdata ); + ~CSaveRestoreBuffer( void ); + + int EntityIndex( entvars_t *pevLookup ); + int EntityIndex( edict_t *pentLookup ); + int EntityIndex( EOFFSET eoLookup ); + int EntityIndex( CBaseEntity *pEntity ); + + int EntityFlags( int entityIndex, int flags ) { return EntityFlagsSet( entityIndex, 0 ); } + int EntityFlagsSet( int entityIndex, int flags ); + + edict_t *EntityFromIndex( int entityIndex ); + + unsigned short TokenHash( const char *pszToken ); + +protected: + SAVERESTOREDATA *m_pdata; + void BufferRewind( int size ); + unsigned int HashString( const char *pszToken ); +}; + + +class CSave : public CSaveRestoreBuffer +{ +public: + CSave( SAVERESTOREDATA *pdata ) : CSaveRestoreBuffer( pdata ) {}; + + void WriteShort( const char *pname, const short *value, int count ); + void WriteInt( const char *pname, const int *value, int count ); // Save an int + void WriteFloat( const char *pname, const float *value, int count ); // Save a float + void WriteTime( const char *pname, const float *value, int count ); // Save a float (timevalue) + void WriteData( const char *pname, int size, const char *pdata ); // Save a binary data block + void WriteString( const char *pname, const char *pstring ); // Save a null-terminated string + void WriteString( const char *pname, const int *stringId, int count ); // Save a null-terminated string (engine string) + void WriteRange( const char *pname, const RandomRange &value ); + void WriteRange( const char *pname, const float *value, int count ); + void WriteVector( const char *pname, const Vector &value ); // Save a vector + void WriteVector( const char *pname, const float *value, int count ); // Save a vector + void WritePositionVector( const char *pname, const Vector &value ); // Offset for landmark if necessary + void WritePositionVector( const char *pname, const float *value, int count ); // array of pos vectors + void WriteFunction( const char *pname, const int *value, int count ); // Save a function pointer + // Save a function pointer. (LRC- also pass the classname to allow better error messages) + void WriteFunction( const char* cname, const char *pname, const int *value, int count ); + + int WriteEntVars( const char *pname, entvars_t *pev ); // Save entvars_t (entvars_t) + int WriteFields( const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ); + int WriteFields( const char *cname, const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ); + +private: + int DataEmpty( const char *pdata, int size ); + void BufferField( const char *pname, int size, const char *pdata ); + void BufferString( char *pdata, int len ); + void BufferData( const char *pdata, int size ); + void BufferHeader( const char *pname, int size ); +}; + +typedef struct +{ + unsigned short size; + unsigned short token; + char *pData; +} HEADER; + +class CRestore : public CSaveRestoreBuffer +{ +public: + CRestore( SAVERESTOREDATA *pdata ) : CSaveRestoreBuffer( pdata ) { m_global = 0; m_precache = TRUE; } + + int ReadEntVars( const char *pname, entvars_t *pev ); // entvars_t + int ReadFields( const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount ); + int ReadField( void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount, int startField, int size, char *pName, void *pData ); + int ReadInt( void ); + short ReadShort( void ); + int ReadNamedInt( const char *pName ); + char *ReadNamedString( const char *pName ); + int Empty( void ) { return (m_pdata == NULL) || ((m_pdata->pCurrentData-m_pdata->pBaseData)>=m_pdata->bufferSize); } + inline void SetGlobalMode( int global ) { m_global = global; } + void PrecacheMode( BOOL mode ) { m_precache = mode; } + +private: + char *BufferPointer( void ); + void BufferReadBytes( char *pOutput, int size ); + void BufferSkipBytes( int bytes ); + int BufferSkipZString( void ); + int BufferCheckZString( const char *string ); + + void BufferReadHeader( HEADER *pheader ); + + int m_global; // Restoring a global entity? + BOOL m_precache; +}; + +#define MAX_ENTITYARRAY 64 + +//#define ARRAYSIZE(p) (sizeof(p)/sizeof(p[0])) + +#define IMPLEMENT_SAVERESTORE(derivedClass,baseClass) \ + int derivedClass::Save( CSave &save )\ + {\ + if ( !baseClass::Save(save) )\ + return 0;\ + if (pev->targetname)\ + return save.WriteFields( STRING(pev->targetname), #derivedClass, this, m_SaveData, ARRAYSIZE(m_SaveData) );\ + else\ + return save.WriteFields( STRING(pev->classname), #derivedClass, this, m_SaveData, ARRAYSIZE(m_SaveData) );\ + }\ + int derivedClass::Restore( CRestore &restore )\ + {\ + if ( !baseClass::Restore(restore) )\ + return 0;\ + return restore.ReadFields( #derivedClass, this, m_SaveData, ARRAYSIZE(m_SaveData) );\ + } + + +typedef struct globalentity_s globalentity_t; + +struct globalentity_s +{ + char name[64]; + char levelName[32]; + GLOBALESTATE state; + globalentity_t *pNext; +}; + +class CGlobalState +{ +public: + CGlobalState(); + void Reset( void ); + void ClearStates( void ); + void EntityAdd( string_t globalname, string_t mapName, GLOBALESTATE state ); + void EntitySetState( string_t globalname, GLOBALESTATE state ); + void EntityUpdate( string_t globalname, string_t mapname ); + const globalentity_t *EntityFromTable( string_t globalname ); + GLOBALESTATE EntityGetState( string_t globalname ); + int EntityInTable( string_t globalname ) { return (Find( globalname ) != NULL) ? 1 : 0; } + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + +//#ifdef _DEBUG + void DumpGlobals( void ); +//#endif + +private: + globalentity_t *Find( string_t globalname ); + globalentity_t *m_pList; + int m_listCount; +}; + +extern CGlobalState gGlobalState; + +#endif //SAVERESTORE_H diff --git a/server/global/sfx.cpp b/server/global/sfx.cpp new file mode 100644 index 00000000..987f6e00 --- /dev/null +++ b/server/global/sfx.cpp @@ -0,0 +1,149 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// sfx.cpp - different type special effects +// e.g. explodes, sparks, smoke e.t.c +//======================================================================= + +#include "sfx.h" + +void SFX_Explode( short model, Vector origin, float scale, int flags ) +{ + MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, origin ); + WRITE_BYTE( TE_EXPLOSION ); // This makes a dynamic light and the explosion sprites/sound + WRITE_COORD( origin.x ); // Send to PAS because of the sound + WRITE_COORD( origin.y ); + WRITE_COORD( origin.z ); + WRITE_SHORT( model ); + WRITE_BYTE( (BYTE)(scale - 30) * 0.7); // scale * 10 + WRITE_BYTE( 15 ); // framerate + WRITE_BYTE( flags ); + MESSAGE_END(); +} + +void SFX_Trail( int entindex, short model, Vector color, float life ) +{ + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BEAMFOLLOW ); + WRITE_SHORT( entindex ); // entity + WRITE_SHORT( model ); // model + WRITE_BYTE( life ); // life + WRITE_BYTE( 5 ); // width + WRITE_BYTE( color.x ); // r, g, b + WRITE_BYTE( color.y ); // r, g, b + WRITE_BYTE( color.z ); // r, g, b + WRITE_BYTE( 200 ); // brightness + MESSAGE_END();// move PHS/PVS data sending into here (SEND_ALL, SEND_PVS, SEND_PHS) + +} + +void SFX_MakeGibs( int shards, Vector pos, Vector size, Vector velocity, float time, int flags) +{ + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, pos ); + WRITE_BYTE( TE_BREAKMODEL); + WRITE_COORD( pos.x ); // position + WRITE_COORD( pos.y ); + WRITE_COORD( pos.z ); + WRITE_COORD( size.x); // size + WRITE_COORD( size.y); + WRITE_COORD( size.z); + WRITE_COORD( velocity.x ); // velocity + WRITE_COORD( velocity.y ); + WRITE_COORD( velocity.z ); + WRITE_BYTE( 10 ); + WRITE_SHORT( shards ); // model id# + WRITE_BYTE( 0 ); // let client decide + WRITE_BYTE( time ); // life time + WRITE_BYTE( flags ); + MESSAGE_END(); +} + +void SFX_EjectBrass ( const Vector &vecOrigin, const Vector &vecVelocity, float rotation, int model, int soundtype ) +{ + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecOrigin ); + WRITE_BYTE( TE_MODEL); + WRITE_COORD( vecOrigin.x); + WRITE_COORD( vecOrigin.y); + WRITE_COORD( vecOrigin.z); + WRITE_COORD( vecVelocity.x); + WRITE_COORD( vecVelocity.y); + WRITE_COORD( vecVelocity.z); + WRITE_ANGLE( rotation ); + WRITE_SHORT( model ); + WRITE_BYTE ( soundtype); + WRITE_BYTE ( 25 );// 2.5 seconds + MESSAGE_END(); +} + +void SFX_Decal( const Vector &vecOrigin, int decalIndex, int entityIndex, int modelIndex ) +{ + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY); + WRITE_BYTE( TE_BSPDECAL ); + WRITE_COORD( vecOrigin.x ); + WRITE_COORD( vecOrigin.y ); + WRITE_COORD( vecOrigin.z ); + WRITE_SHORT( decalIndex ); + WRITE_SHORT( entityIndex ); + if ( entityIndex ) WRITE_SHORT( modelIndex ); + MESSAGE_END(); +} + +void SFX_Light ( entvars_t *pev, float iTime, float decay, int attachment ) +{ + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, pev->origin ); + WRITE_BYTE( TE_ELIGHT ); + WRITE_SHORT( ENTINDEX( ENT(pev) ) + 0x1000 * attachment );// entity, attachment + WRITE_COORD( pev->origin.x ); // X + WRITE_COORD( pev->origin.y ); // Y + WRITE_COORD( pev->origin.z ); // Z + WRITE_COORD( pev->renderamt ); // radius * 0.1 + WRITE_BYTE( pev->rendercolor.x ); // r + WRITE_BYTE( pev->rendercolor.y ); // g + WRITE_BYTE( pev->rendercolor.z ); // b + WRITE_BYTE( iTime ); // time * 10 + WRITE_COORD( decay ); // decay * 0.1 + MESSAGE_END( ); +} + +void SFX_Zap ( entvars_t *pev, const Vector &vecSrc, const Vector &vecDest ) +{ + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BEAMPOINTS); + WRITE_COORD(vecSrc.x); + WRITE_COORD(vecSrc.y); + WRITE_COORD(vecSrc.z); + WRITE_COORD(vecDest.x); + WRITE_COORD(vecDest.y); + WRITE_COORD(vecDest.z); + WRITE_SHORT( pev->team ); + WRITE_BYTE( 0 ); //framestart + WRITE_BYTE( (int)pev->framerate); // framerate + WRITE_BYTE( (int)(pev->armorvalue*10.0) ); // life + WRITE_BYTE( pev->button ); // width + WRITE_BYTE( pev->impulse ); // noise + WRITE_BYTE( (int)pev->rendercolor.x ); // r, g, b + WRITE_BYTE( (int)pev->rendercolor.y ); // r, g, b + WRITE_BYTE( (int)pev->rendercolor.z ); // r, g, b + WRITE_BYTE( pev->renderamt ); // brightness + WRITE_BYTE( pev->speed ); // speed + MESSAGE_END(); +} + +void SFX_Ring ( entvars_t *pev, entvars_t *pev2 ) +{ + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BEAMRING ); + WRITE_SHORT( ENTINDEX(ENT(pev)) ); + WRITE_SHORT( ENTINDEX(ENT(pev2)) ); + WRITE_SHORT( pev->team ); + WRITE_BYTE( 0 ); // framestart + WRITE_BYTE( 20 ); // framerate + WRITE_BYTE( (int)(pev->armorvalue*10) ); // life + WRITE_BYTE( pev->button ); // width + WRITE_BYTE( pev->impulse ); // noise + WRITE_BYTE( (int)pev->rendercolor.x ); // r, g, b + WRITE_BYTE( (int)pev->rendercolor.y ); // r, g, b + WRITE_BYTE( (int)pev->rendercolor.z ); // r, g, b + WRITE_BYTE( pev->renderamt ); // brightness + WRITE_BYTE( pev->speed ); // speed + MESSAGE_END(); +} \ No newline at end of file diff --git a/server/global/sfx.h b/server/global/sfx.h new file mode 100644 index 00000000..67c208e5 --- /dev/null +++ b/server/global/sfx.h @@ -0,0 +1,56 @@ +//======================================================================= +// Copyright (C) Shambler Team 2004 +// sfx.h - different type special effects +// e.g. explodes, sparks, smoke e.t.c +//======================================================================= +#ifndef SFX_H +#define SFX_H + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "baseweapon.h" +#include "player.h" +#include "defaults.h" +#include "shake.h" +#include "basebeams.h" +#include "te_temp.h" + +// Break Model Defines + +#define BREAK_TYPEMASK 0x4F +#define BREAK_GLASS 0x01 +#define BREAK_METAL 0x02 +#define BREAK_FLESH 0x04 +#define BREAK_WOOD 0x08 + +#define BREAK_SMOKE 0x10 +#define BREAK_TRANS 0x20 +#define BREAK_CONCRETE 0x40 +#define BREAK_2 0x80 + +// Colliding temp entity sounds +#define BOUNCE_GLASS BREAK_GLASS +#define BOUNCE_METAL BREAK_METAL +#define BOUNCE_FLESH BREAK_FLESH +#define BOUNCE_WOOD BREAK_WOOD +#define BOUNCE_SHRAP 0x10 +#define BOUNCE_SHELL 0x20 +#define BOUNCE_CONCRETE BREAK_CONCRETE +#define BOUNCE_SHOTSHELL 0x80 + +// Temp entity bounce sound types +#define TE_BOUNCE_NULL 0 +#define TE_BOUNCE_SHELL 1 +#define TE_BOUNCE_SHOTSHELL 2 + +void SFX_Explode( short model, Vector origin, float scale, int flags = 0 ); +void SFX_Trail( int entindex, short model, Vector color = Vector(200, 200, 200), float life = 30); +void SFX_MakeGibs( int shards, Vector pos, Vector size, Vector velocity, float time = 25, int flags = 0); +void SFX_EjectBrass ( const Vector &vecOrigin, const Vector &vecVelocity, float rotation, int model, int soundtype ); +void SFX_Decal( const Vector &vecOrigin, int decalIndex, int entityIndex, int modelIndex ); +void SFX_Light ( entvars_t *pev, float iTime, float decay, int attachment = 0 ); +void SFX_Zap ( entvars_t *pev, const Vector &vecSrc, const Vector &vecDest ); +void SFX_Ring ( entvars_t *pev, entvars_t *pev2 ); + +#endif //SFX_H \ No newline at end of file diff --git a/server/global/shake.h b/server/global/shake.h new file mode 100644 index 00000000..6ebdf337 --- /dev/null +++ b/server/global/shake.h @@ -0,0 +1,41 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef SHAKE_H +#define SHAKE_H + +// Screen / View effects + +// screen shake +extern int gmsgShake; + +// +// Commands for the screen shake effect. +// + + +// Fade in/out +extern int gmsgFade; + +// This structure is sent over the net to describe a screen fade event +typedef struct +{ + unsigned short duration; // FIXED 4.12 seconds duration + unsigned short holdTime; // FIXED 4.12 seconds duration until reset (fade & hold) + short fadeFlags; // flags + byte r, g, b, a; // fade to color ( max alpha ) +} ScreenFade; + +#endif // SHAKE_H + diff --git a/server/global/soundent.h b/server/global/soundent.h new file mode 100644 index 00000000..40aaf59e --- /dev/null +++ b/server/global/soundent.h @@ -0,0 +1,99 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +//========================================================= +// Soundent.h - the entity that spawns when the world +// spawns, and handles the world's active and free sound +// lists. +//========================================================= +#ifndef SOUNDENT_H +#define SOUNDENT_H + +#define MAX_WORLD_SOUNDS 64 // maximum number of sounds handled by the world at one time. + +#define bits_SOUND_NONE 0 +#define bits_SOUND_COMBAT ( 1 << 0 )// gunshots, explosions +#define bits_SOUND_WORLD ( 1 << 1 )// door opening/closing, glass breaking +#define bits_SOUND_PLAYER ( 1 << 2 )// all noises generated by player. walking, shooting, falling, splashing +#define bits_SOUND_CARCASS ( 1 << 3 )// dead body +#define bits_SOUND_MEAT ( 1 << 4 )// gib or pork chop +#define bits_SOUND_DANGER ( 1 << 5 )// pending danger. Grenade that is about to explode, explosive barrel that is damaged, falling crate +#define bits_SOUND_GARBAGE ( 1 << 6 )// trash cans, banana peels, old fast food bags. + +#define bits_ALL_SOUNDS 0xFFFFFFFF + +#define SOUNDLIST_EMPTY -1 + +#define SOUNDLISTTYPE_FREE 1// identifiers passed to functions that can operate on either list, to indicate which list to operate on. +#define SOUNDLISTTYPE_ACTIVE 2 + +#define SOUND_NEVER_EXPIRE -1 // with this set as a sound's ExpireTime, the sound will never expire. + +//========================================================= +// CSound - an instance of a sound in the world. +//========================================================= +class CSound +{ +public: + + void Clear ( void ); + void Reset ( void ); + + Vector m_vecOrigin; // sound's location in space + int m_iType; // what type of sound this is + int m_iVolume; // how loud the sound is + float m_flExpireTime; // when the sound should be purged from the list + int m_iNext; // index of next sound in this list ( Active or Free ) + int m_iNextAudible; // temporary link that monsters use to build a list of audible sounds + + BOOL FIsSound( void ); + BOOL FIsScent( void ); +}; + +//========================================================= +// CSoundEnt - a single instance of this entity spawns when +// the world spawns. The SoundEnt's job is to update the +// world's Free and Active sound lists. +//========================================================= +class CSoundEnt : public CBaseEntity +{ +public: + + void Precache ( void ); + void Spawn( void ); + void Think( void ); + void Initialize ( void ); + + static void InsertSound ( int iType, const Vector &vecOrigin, int iVolume, float flDuration ); + static void FreeSound ( int iSound, int iPrevious ); + static int ActiveList( void );// return the head of the active list + static int FreeList( void );// return the head of the free list + static CSound* SoundPointerForIndex( int iIndex );// return a pointer for this index in the sound list + static int ClientSoundIndex ( edict_t *pClient ); + + BOOL IsEmpty( void ) { return m_iActiveSound == SOUNDLIST_EMPTY; } + int ISoundsInList ( int iListType ); + int IAllocSound ( void ); + virtual int ObjectCaps( void ) { return FCAP_DONT_SAVE; } + + int m_iFreeSound; // index of the first sound in the free sound list + int m_iActiveSound; // indes of the first sound in the active sound list + int m_cLastActiveSounds; // keeps track of the number of active sounds at the last update. (for diagnostic work) + BOOL m_fShowReport; // if true, dump information about free/active sounds. + +private: + CSound m_SoundPool[ MAX_WORLD_SOUNDS ]; +}; + +#endif //SOUNDENT_H \ No newline at end of file diff --git a/server/global/te_temp.h b/server/global/te_temp.h new file mode 100644 index 00000000..0b60751b --- /dev/null +++ b/server/global/te_temp.h @@ -0,0 +1,448 @@ +#pragma once + +// attempt to be remove +// +// temp entity events +// +#define TE_BEAMPOINTS 0 // beam effect between two points +// coord coord coord (start position) +// coord coord coord (end position) +// short (sprite index) +// byte (starting frame) +// byte (frame rate in 0.1's) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte (noise amplitude in 0.01's) +// byte,byte,byte (color) +// byte (brightness) +// byte (scroll speed in 0.1's) + +#define TE_BEAMENTPOINT 1 // beam effect between point and entity +// short (start entity) +// coord coord coord (end position) +// short (sprite index) +// byte (starting frame) +// byte (frame rate in 0.1's) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte (noise amplitude in 0.01's) +// byte,byte,byte (color) +// byte (brightness) +// byte (scroll speed in 0.1's) + +#define TE_GUNSHOT 2 // particle effect plus ricochet sound +// coord coord coord (position) + +#define TE_EXPLOSION 3 // additive sprite, 2 dynamic lights, flickering particles, explosion sound, move vertically 8 pps +// coord coord coord (position) +// short (sprite index) +// byte (scale in 0.1's) +// byte (framerate) +// byte (flags) +// +// The Explosion effect has some flags to control performance/aesthetic features: +#define TE_EXPLFLAG_NONE 0 // all flags clear makes default Half-Life explosion +#define TE_EXPLFLAG_NOADDITIVE 1 // sprite will be drawn opaque (ensure that the sprite you send is a non-additive sprite) +#define TE_EXPLFLAG_NODLIGHTS 2 // do not render dynamic lights +#define TE_EXPLFLAG_NOSOUND 4 // do not play client explosion sound +#define TE_EXPLFLAG_NOPARTICLES 8 // do not draw particles + + +#define TE_TAREXPLOSION 4 // Quake1 "tarbaby" explosion with sound +// coord coord coord (position) + +#define TE_SMOKE 5 // alphablend sprite, move vertically 30 pps +// coord coord coord (position) +// short (sprite index) +// byte (scale in 0.1's) +// byte (framerate) + +#define TE_TRACER 6 // tracer effect from point to point +// coord, coord, coord (start) +// coord, coord, coord (end) + +#define TE_LIGHTNING 7 // TE_BEAMPOINTS with simplified parameters +// coord, coord, coord (start) +// coord, coord, coord (end) +// byte (life in 0.1's) +// byte (width in 0.1's) +// byte (amplitude in 0.01's) +// short (sprite model index) + +#define TE_BEAMENTS 8 +// short (start entity) +// short (end entity) +// short (sprite index) +// byte (starting frame) +// byte (frame rate in 0.1's) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte (noise amplitude in 0.01's) +// byte,byte,byte (color) +// byte (brightness) +// byte (scroll speed in 0.1's) + +#define TE_SPARKS 9 // 8 random tracers with gravity, ricochet sprite +// coord coord coord (position) + +#define TE_LAVASPLASH 10 // Quake1 lava splash +// coord coord coord (position) + +#define TE_TELEPORT 11 // Quake1 teleport splash +// coord coord coord (position) + +#define TE_EXPLOSION2 12 // Quake1 colormaped (base palette) particle explosion with sound +// coord coord coord (position) +// byte (starting color) +// byte (num colors) + +#define TE_BSPDECAL 13 // Decal from the .BSP file +// coord, coord, coord (x,y,z), decal position (center of texture in world) +// short (texture index of precached decal texture name) +// short (entity index) +// [optional - only included if previous short is non-zero (not the world)] short (index of model of above entity) + +#define TE_IMPLOSION 14 // tracers moving toward a point +// coord, coord, coord (position) +// byte (radius) +// byte (count) +// byte (life in 0.1's) + +#define TE_SPRITETRAIL 15 // line of moving glow sprites with gravity, fadeout, and collisions +// coord, coord, coord (start) +// coord, coord, coord (end) +// short (sprite index) +// byte (count) +// byte (life in 0.1's) +// byte (scale in 0.1's) +// byte (velocity along vector in 10's) +// byte (randomness of velocity in 10's) + +#define TE_BEAM 16 // obsolete + +#define TE_SPRITE 17 // additive sprite, plays 1 cycle +// coord, coord, coord (position) +// short (sprite index) +// byte (scale in 0.1's) +// byte (brightness) + +#define TE_BEAMSPRITE 18 // A beam with a sprite at the end +// coord, coord, coord (start position) +// coord, coord, coord (end position) +// short (beam sprite index) +// short (end sprite index) + +#define TE_BEAMTORUS 19 // screen aligned beam ring, expands to max radius over lifetime +// coord coord coord (center position) +// coord coord coord (axis and radius) +// short (sprite index) +// byte (starting frame) +// byte (frame rate in 0.1's) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte (noise amplitude in 0.01's) +// byte,byte,byte (color) +// byte (brightness) +// byte (scroll speed in 0.1's) + +#define TE_BEAMDISK 20 // disk that expands to max radius over lifetime +// coord coord coord (center position) +// coord coord coord (axis and radius) +// short (sprite index) +// byte (starting frame) +// byte (frame rate in 0.1's) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte (noise amplitude in 0.01's) +// byte,byte,byte (color) +// byte (brightness) +// byte (scroll speed in 0.1's) + +#define TE_BEAMCYLINDER 21 // cylinder that expands to max radius over lifetime +// coord coord coord (center position) +// coord coord coord (axis and radius) +// short (sprite index) +// byte (starting frame) +// byte (frame rate in 0.1's) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte (noise amplitude in 0.01's) +// byte,byte,byte (color) +// byte (brightness) +// byte (scroll speed in 0.1's) + +#define TE_BEAMFOLLOW 22 // create a line of decaying beam segments until entity stops moving +// short (entity:attachment to follow) +// short (sprite index) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte,byte,byte (color) +// byte (brightness) + +#define TE_GLOWSPRITE 23 +// coord, coord, coord (pos) short (model index) byte (scale / 10) + +#define TE_BEAMRING 24 // connect a beam ring to two entities +// short (start entity) +// short (end entity) +// short (sprite index) +// byte (starting frame) +// byte (frame rate in 0.1's) +// byte (life in 0.1's) +// byte (line width in 0.1's) +// byte (noise amplitude in 0.01's) +// byte,byte,byte (color) +// byte (brightness) +// byte (scroll speed in 0.1's) + +#define TE_STREAK_SPLASH 25 // oriented shower of tracers +// coord coord coord (start position) +// coord coord coord (direction vector) +// byte (color) +// short (count) +// short (base speed) +// short (ramdon velocity) + +#define TE_BEAMHOSE 26 // obsolete + +#define TE_DLIGHT 27 // dynamic light, effect world, minor entity effect +// coord, coord, coord (pos) +// byte (radius in 10's) +// byte byte byte (color) +// byte (life in 10's) +// byte (decay rate in 10's) + +#define TE_ELIGHT 28 // point entity light, no world effect +// short (entity:attachment to follow) +// coord coord coord (initial position) +// coord (radius) +// byte byte byte (color) +// byte (life in 0.1's) +// coord (decay rate) + +#define TE_TEXTMESSAGE 29 +// short 1.2.13 x (-1 = center) +// short 1.2.13 y (-1 = center) +// byte Effect 0 = fade in/fade out + // 1 is flickery credits + // 2 is write out (training room) + +// 4 bytes r,g,b,a color1 (text color) +// 4 bytes r,g,b,a color2 (effect color) +// ushort 8.8 fadein time +// ushort 8.8 fadeout time +// ushort 8.8 hold time +// optional ushort 8.8 fxtime (time the highlight lags behing the leading text in effect 2) +// string text message (512 chars max sz string) +#define TE_LINE 30 +// coord, coord, coord startpos +// coord, coord, coord endpos +// short life in 0.1 s +// 3 bytes r, g, b + +#define TE_BOX 31 +// coord, coord, coord boxmins +// coord, coord, coord boxmaxs +// short life in 0.1 s +// 3 bytes r, g, b + +#define TE_KILLBEAM 99 // kill all beams attached to entity +// short (entity) + +#define TE_LARGEFUNNEL 100 +// coord coord coord (funnel position) +// short (sprite index) +// short (flags) + +#define TE_BLOODSTREAM 101 // particle spray +// coord coord coord (start position) +// coord coord coord (spray vector) +// byte (color) +// byte (speed) + +#define TE_SHOWLINE 102 // line of particles every 5 units, dies in 30 seconds +// coord coord coord (start position) +// coord coord coord (end position) + +#define TE_BLOOD 103 // particle spray +// coord coord coord (start position) +// coord coord coord (spray vector) +// byte (color) +// byte (speed) + +#define TE_DECAL 104 // Decal applied to a brush entity (not the world) +// coord, coord, coord (x,y,z), decal position (center of texture in world) +// byte (texture index of precached decal texture name) +// short (entity index) + +#define TE_FIZZ 105 // create alpha sprites inside of entity, float upwards +// short (entity) +// short (sprite index) +// byte (density) + +#define TE_MODEL 106 // create a moving model that bounces and makes a sound when it hits +// coord, coord, coord (position) +// coord, coord, coord (velocity) +// angle (initial yaw) +// short (model index) +// byte (bounce sound type) +// byte (life in 0.1's) + +#define TE_EXPLODEMODEL 107 // spherical shower of models, picks from set +// coord, coord, coord (origin) +// coord (velocity) +// short (model index) +// short (count) +// byte (life in 0.1's) + +#define TE_BREAKMODEL 108 // box of models or sprites +// coord, coord, coord (position) +// coord, coord, coord (size) +// coord, coord, coord (velocity) +// byte (random velocity in 10's) +// short (sprite or model index) +// byte (count) +// byte (life in 0.1 secs) +// byte (flags) + +#define TE_GUNSHOTDECAL 109 // decal and ricochet sound +// coord, coord, coord (position) +// short (entity index???) +// byte (decal???) + +#define TE_SPRITE_SPRAY 110 // spay of alpha sprites +// coord, coord, coord (position) +// coord, coord, coord (velocity) +// short (sprite index) +// byte (count) +// byte (speed) +// byte (noise) + +#define TE_ARMOR_RICOCHET 111 // quick spark sprite, client ricochet sound. +// coord, coord, coord (position) +// byte (scale in 0.1's) + +#define TE_PLAYERDECAL 112 // ??? +// byte (playerindex) +// coord, coord, coord (position) +// short (entity???) +// byte (decal number???) +// [optional] short (model index???) + +#define TE_BUBBLES 113 // create alpha sprites inside of box, float upwards +// coord, coord, coord (min start position) +// coord, coord, coord (max start position) +// coord (float height) +// short (model index) +// byte (count) +// coord (speed) + +#define TE_BUBBLETRAIL 114 // create alpha sprites along a line, float upwards +// coord, coord, coord (min start position) +// coord, coord, coord (max start position) +// coord (float height) +// short (model index) +// byte (count) +// coord (speed) + +#define TE_BLOODSPRITE 115 // spray of opaque sprite1's that fall, single sprite2 for 1..2 secs (this is a high-priority tent) +// coord, coord, coord (position) +// short (sprite1 index) +// short (sprite2 index) +// byte (color) +// byte (scale) + +#define TE_WORLDDECAL 116 // Decal applied to the world brush +// coord, coord, coord (x,y,z), decal position (center of texture in world) +// byte (texture index of precached decal texture name) + +#define TE_WORLDDECALHIGH 117 // Decal (with texture index > 256) applied to world brush +// coord, coord, coord (x,y,z), decal position (center of texture in world) +// byte (texture index of precached decal texture name - 256) + +#define TE_DECALHIGH 118 // Same as TE_DECAL, but the texture index was greater than 256 +// coord, coord, coord (x,y,z), decal position (center of texture in world) +// byte (texture index of precached decal texture name - 256) +// short (entity index) + +#define TE_PROJECTILE 119 // Makes a projectile (like a nail) (this is a high-priority tent) +// coord, coord, coord (position) +// coord, coord, coord (velocity) +// short (modelindex) +// byte (life) +// byte (owner) projectile won't collide with owner (if owner == 0, projectile will hit any client). + +#define TE_SPRAY 120 // Throws a shower of sprites or models +// coord, coord, coord (position) +// coord, coord, coord (direction) +// short (modelindex) +// byte (count) +// byte (speed) +// byte (noise) +// byte (rendermode) + +#define TE_PLAYERSPRITES 121 // sprites emit from a player's bounding box (ONLY use for players!) +// byte (playernum) +// short (sprite modelindex) +// byte (count) +// byte (variance) (0 = no variance in size) (10 = 10% variance in size) + +#define TE_PARTICLEBURST 122 // very similar to lavasplash. +// coord (origin) +// short (radius) +// byte (particle color) +// byte (duration * 10) (will be randomized a bit) + +#define TE_FIREFIELD 123 // makes a field of fire. +// coord (origin) +// short (radius) (fire is made in a square around origin. -radius, -radius to radius, radius) +// short (modelindex) +// byte (count) +// byte (flags) +// byte (duration (in seconds) * 10) (will be randomized a bit) +// +// to keep network traffic low, this message has associated flags that fit into a byte: +#define TEFIRE_FLAG_ALLFLOAT 1 // all sprites will drift upwards as they animate +#define TEFIRE_FLAG_SOMEFLOAT 2 // some of the sprites will drift upwards. (50% chance) +#define TEFIRE_FLAG_LOOP 4 // if set, sprite plays at 15 fps, otherwise plays at whatever rate stretches the animation over the sprite's duration. +#define TEFIRE_FLAG_ALPHA 8 // if set, sprite is rendered alpha blended at 50% else, opaque +#define TEFIRE_FLAG_PLANAR 16 // if set, all fire sprites have same initial Z instead of randomly filling a cube. + +#define TE_PLAYERATTACHMENT 124 // attaches a TENT to a player (this is a high-priority tent) +// byte (entity index of player) +// coord (vertical offset) ( attachment origin.z = player origin.z + vertical offset ) +// short (model index) +// short (life * 10 ); + +#define TE_KILLPLAYERATTACHMENTS 125 // will expire all TENTS attached to a player. +// byte (entity index of player) + +#define TE_MULTIGUNSHOT 126 // much more compact shotgun message +// This message is used to make a client approximate a 'spray' of gunfire. +// Any weapon that fires more than one bullet per frame and fires in a bit of a spread is +// a good candidate for MULTIGUNSHOT use. (shotguns) +// +// NOTE: This effect makes the client do traces for each bullet, these client traces ignore +// entities that have studio models.Traces are 4096 long. +// +// coord (origin) +// coord (origin) +// coord (origin) +// coord (direction) +// coord (direction) +// coord (direction) +// coord (x noise * 100) +// coord (y noise * 100) +// byte (count) +// byte (bullethole decal texture index) + +#define TE_USERTRACER 127 // larger message than the standard tracer, but allows some customization. +// coord (origin) +// coord (origin) +// coord (origin) +// coord (velocity) +// coord (velocity) +// coord (velocity) +// byte ( life * 10 ) +// byte ( color ) this is an index into an array of color vectors in the engine. (0 - ) +// byte ( length * 10 ) \ No newline at end of file diff --git a/server/global/utils.cpp b/server/global/utils.cpp new file mode 100644 index 00000000..2dbab2c2 --- /dev/null +++ b/server/global/utils.cpp @@ -0,0 +1,3048 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// utils.cpp - Utility code. +// Really not optional after all. +//======================================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "defaults.h" +#include "saverestore.h" +#include +#include "shake.h" +#include "decals.h" +#include "player.h" +#include "baseweapon.h" +#include "gamerules.h" +#include "client.h" + +FILE_GLOBAL char st_szNextMap[MAP_MAX_NAME]; +FILE_GLOBAL char st_szNextSpot[MAP_MAX_NAME]; +extern DLL_GLOBAL BOOL NewLevel; +int giAmmoIndex = 0; +BOOL CanAffect; + +//========================================================= +// COM_Functions (text files parsing) +//========================================================= +#define COM_Format(x) va_list szCommand; \ + static char value[1024]; \ + va_start( szCommand, x ); \ + vsprintf( value, x, szCommand ); \ + va_end( szCommand ); + +void Msg( char *message, ... ) +{ + COM_Format( message ); + g_engfuncs.pfnAlertMessage( at_console, "%s", value ); +} + +void DevMsg( char *message, ... ) +{ + COM_Format( message ); + g_engfuncs.pfnAlertMessage( at_aiconsole, "%s", value ); +} + +struct +{ + char token[1024]; + int length; + int symbol; +} parse; + +char *COM_Parse( char *data ) +{ + parse.length = 0; + parse.token[0] = 0; + + if( !data ) return NULL; +skip: + while((parse.symbol = *data) <= ' ') + { + if( parse.symbol == 0 ) return NULL; + data++; + } + if( parse.symbol == '/' && data[1] == '/' ) + { + while( *data && *data != '\n' ) data++; + goto skip; + } + if( parse.symbol == '\"' ) + { + data++; + while(true) + { + parse.symbol = *data++; + if( parse.symbol == '\"' || !parse.symbol ) + { + parse.token[parse.length] = 0; + return data; + } + parse.token[parse.length] = parse.symbol; + parse.length++; + } + } + + if( parse.symbol == '{' || parse.symbol == '}' || parse.symbol == ')' || parse.symbol == '(' || parse.symbol == '\'' || parse.symbol == ',' ) + { + parse.token[parse.length] = parse.symbol; + parse.length++; + parse.token[parse.length] = 0; + return data+1; + } + do + { + parse.token[parse.length] = parse.symbol; + data++; + parse.length++; + parse.symbol = *data; + if( parse.symbol == '{' || parse.symbol == '}' || parse.symbol == ')' || parse.symbol == '(' || parse.symbol == '\'' || parse.symbol == ',' ) break; + } while( parse.symbol > 32 ); + + parse.token[parse.length] = 0; + return data; +} + +char *COM_ParseFile( char *data, char *token ) +{ + char *return_data = COM_Parse( data ); + strcpy( token, parse.token ); + return return_data; +} + +void COM_FreeFile (char *buffer) +{ + FREE_FILE( buffer ); +} + +//========================================================= +// check for null ents +//========================================================= + +inline BOOL FNullEnt(EOFFSET eoffset) { return eoffset == 0; } +inline BOOL FNullEnt(const edict_t* pent) { return pent == NULL || FNullEnt(OFFSET(pent)); } +inline BOOL FNullEnt(entvars_t* pev) { return pev == NULL || FNullEnt(OFFSET(pev)); } +inline BOOL FNullEnt( CBaseEntity *ent ) { return ent == NULL || FNullEnt( ent->edict()); } + +//========================================================= +// calculate brush model origin +//========================================================= +Vector VecBModelOrigin( entvars_t* pevBModel ) +{ + return (pevBModel->absmin + pevBModel->absmax) * 0.5; +} + +BOOL FClassnameIs(CBaseEntity *pEnt, const char* szClassname) +{ + return FStrEq(STRING(pEnt->pev->classname), szClassname); +} + +//======================================================================== +// UTIL_FireTargets - supported prefix "+", "-", "!", ">", "<", "?". +// supported also this and self pointers - e.g. "fadein(mywall)" +//======================================================================== +void UTIL_FireTargets( int targetName, CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + //overload version UTIL_FireTargets + if (!targetName) return;//no execute code, if target blank + + UTIL_FireTargets( STRING(targetName), pActivator, pCaller, useType, value); +} + +void UTIL_FireTargets( const char *targetName, CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value) +{ + + const char *inputTargetName = targetName; + CBaseEntity *inputActivator = pActivator; + CBaseEntity *pTarget = NULL; + int i,j, found = false; + char szBuf[80]; + + if ( !targetName )return; + + //HACKHACK + if(FStrEq(targetName, "tr_endchange" )) + { + SERVER_COMMAND("game_over\n"); + return; + } + + if (targetName[0] == '+') + { + targetName++; + useType = USE_ON; + } + else if (targetName[0] == '-') + { + targetName++; + useType = USE_OFF; + } + else if (targetName[0] == '!') + { + targetName++; + useType = USE_REMOVE; + } + else if (targetName[0] == '<') + { + targetName++; + useType = USE_SET; + } + else if (targetName[0] == '>') + { + targetName++; + useType = USE_RESET; + } + else if (targetName[0] == '?') + { + targetName++; + useType = USE_SHOWINFO; + } + + pTarget = UTIL_FindEntityByTargetname(pTarget, targetName, pActivator); + + if( !pTarget )//smart field name ? + { + //try to extract value from name (it's more usefully than "locus" specifier) + for (i = 0; targetName[i]; i++) + { + if (targetName[i] == '.')//value specifier + { + value = atof(&targetName[i+1]); + sprintf(szBuf, targetName); + szBuf[i] = 0; + targetName = szBuf; + pTarget = UTIL_FindEntityByTargetname(NULL, targetName, inputActivator); + break; + } + } + if( !pTarget )//try to extract activator specified + { + for (i = 0; targetName[i]; i++) + { + if (targetName[i] == '(') + { + i++; + for (j = i; targetName[j]; j++) + { + if (targetName[j] == ')') + { + strncpy(szBuf, targetName+i, j-i); + szBuf[j-i] = 0; + pActivator = UTIL_FindEntityByTargetname(NULL, szBuf, inputActivator); + if (!pActivator) return; //it's a locus specifier, but the locus is invalid. + found = true; + break; + } + } + if (!found) ALERT(at_error, "Missing ')' in targetname: %s", inputTargetName); + break; + } + } + if (!found) return; // no, it's not a locus specifier. + + strncpy(szBuf, targetName, i-1); + szBuf[i-1] = 0; + targetName = szBuf; + pTarget = UTIL_FindEntityByTargetname(NULL, targetName, inputActivator); + + if (!pTarget)return; // it's a locus specifier all right, but the target's invalid. + } + } + + DevMsg( "Firing: (%s) with %s and value %g\n", targetName, GetStringForUseType( useType ), value ); + + do //start firing targets + { + if ( !(pTarget->pev->flags & FL_KILLME) ) // Don't use dying ents + { + if (useType == USE_REMOVE) UTIL_Remove( pTarget ); + else pTarget->Use( pActivator, pCaller, useType, value ); + } + pTarget = UTIL_FindEntityByTargetname(pTarget, targetName, inputActivator); + + } while (pTarget); +} + +//========================================================= +// UTIL_StripToken - for redundant keynames +//========================================================= +void UTIL_StripToken( const char *pKey, char *pDest ) +{ + int i = 0; + + while ( pKey[i] && pKey[i] != '#' ) + { + pDest[i] = pKey[i]; + i++; + } + pDest[i] = 0; +} + + +char* GetStringForUseType( USE_TYPE useType ) +{ + switch(useType) + { + case USE_ON: return "USE_ON"; + case USE_OFF: return "USE_OFF"; + case USE_TOGGLE: return "USE_TOGGLE"; + case USE_REMOVE: return "USE_REMOVE"; + case USE_SET: return "USE_SET"; + case USE_RESET: return "USE_RESET"; + case USE_SHOWINFO: return "USE_SHOWINFO"; + default: + return "UNKNOWN USE_TYPE!"; + } +} + +char* GetStringForState( STATE state ) +{ + switch(state) + { + case STATE_ON: return "ON"; + case STATE_OFF: return "OFF"; + case STATE_TURN_ON: return "TURN ON"; + case STATE_TURN_OFF: return "TURN OFF"; + case STATE_IN_USE: return "IN USE"; + case STATE_DEAD: return "DEAD"; + default: + return "UNKNOWN STATE!"; + } +} + +char* GetStringForDecalName( int decalname ) +{ + char *name = ""; + int m_decal = UTIL_LoadDecalPreset( decalname ); + switch(m_decal) + { + case 1: name = gDecals[ DECAL_GUNSHOT1 + RANDOM_LONG(0,4)].name; break; + case 2: name = gDecals[ DECAL_BLOOD1 + RANDOM_LONG(0,5)].name; break; + case 3: name = gDecals[ DECAL_YBLOOD1 + RANDOM_LONG(0,5)].name; break; + case 4: name = gDecals[ DECAL_GLASSBREAK1 + RANDOM_LONG(0,2)].name; break; + case 5: name = gDecals[ DECAL_BIGSHOT1 + RANDOM_LONG(0,4)].name; break; + case 6: name = gDecals[ DECAL_SCORCH1 + RANDOM_LONG(0,1)].name; break; + case 7: name = gDecals[ DECAL_SPIT1 + RANDOM_LONG(0,1)].name; break; + default: name = (char *)STRING( decalname ); break; + } + return name; +} + +void PrintStringForDamage( int dmgbits ) +{ + char szDmgBits[256]; + strcat(szDmgBits, "DAMAGE: " ); + if( dmgbits & DMG_GENERIC) strcat(szDmgBits, "Generic " ); + if( dmgbits & DMG_CRUSH) strcat(szDmgBits, "Crush " ); + if( dmgbits & DMG_BULLET) strcat(szDmgBits, "Bullet " ); + if( dmgbits & DMG_SLASH) strcat(szDmgBits, "Slash " ); + if( dmgbits & DMG_BURN) strcat(szDmgBits, "Burn " ); + if( dmgbits & DMG_FREEZE) strcat(szDmgBits, "Freeze " ); + if( dmgbits & DMG_FALL) strcat(szDmgBits, "Fall " ); + if( dmgbits & DMG_BLAST) strcat(szDmgBits, "Blast " ); + if( dmgbits & DMG_CLUB) strcat(szDmgBits, "Club " ); + if( dmgbits & DMG_SHOCK) strcat(szDmgBits, "Shock " ); + if( dmgbits & DMG_SONIC) strcat(szDmgBits, "Sonic " ); + if( dmgbits & DMG_ENERGYBEAM) strcat(szDmgBits, "Energy Beam " ); + if( dmgbits & DMG_NEVERGIB) strcat(szDmgBits, "Never Gib " ); + if( dmgbits & DMG_ALWAYSGIB) strcat(szDmgBits, "Always Gib " ); + if( dmgbits & DMG_DROWN) strcat(szDmgBits, "Drown " ); + if( dmgbits & DMG_PARALYZE) strcat(szDmgBits, "Paralyze Gas " ); + if( dmgbits & DMG_NERVEGAS) strcat(szDmgBits, "Nerve Gas " ); + if( dmgbits & DMG_POISON) strcat(szDmgBits, "Poison " ); + if( dmgbits & DMG_RADIATION) strcat(szDmgBits, "Radiation " ); + if( dmgbits & DMG_DROWNRECOVER) strcat(szDmgBits, "Drown Recover " ); + if( dmgbits & DMG_ACID) strcat(szDmgBits, "Acid " ); + if( dmgbits & DMG_SLOWBURN) strcat(szDmgBits, "Slow Burn " ); + if( dmgbits & DMG_SLOWFREEZE) strcat(szDmgBits, "Slow Freeze " ); + if( dmgbits & DMG_MORTAR) strcat(szDmgBits, "Mortar " ); + if( dmgbits & DMG_NUCLEAR) strcat(szDmgBits, "Nuclear Explode " ); + Msg("%s\n", szDmgBits ); +} +char* GetContentsString( int contents ) +{ + switch(contents) + { + case -1: return "EMPTY"; + case -2: return "SOLID"; + case -3: return "WATER"; + case -4: return "SLIME"; + case -5: return "LAVA"; + case -6: return "SKY"; + case -16: return "LADDER"; + case -17: return "FLYFIELD"; + case -18: return "GRAVITY_FLYFIELD"; + case -19: return "FOG"; + case -20: return "SPECIAL 1"; + case -21: return "SPECIAL 2"; + case -22: return "SPECIAL 3"; + default: + return "NO CONTENTS!"; + } +} + +char* GetStringForGlobalState( GLOBALESTATE state ) +{ + switch(state) + { + case GLOBAL_ON: return "GLOBAL ON"; + case GLOBAL_OFF: return "GLOBAL OFF"; + case GLOBAL_DEAD: return "GLOBAL DEAD"; + default: + return "UNKNOWN STATE!"; + } +} + +void UTIL_Remove( CBaseEntity *pEntity ) +{ + if ( !pEntity ) return; + + pEntity->UpdateOnRemove(); + pEntity->pev->flags |= FL_KILLME; + pEntity->pev->targetname = 0; + pEntity->pev->health = 0; +} + +void UTIL_AngularVector( CBaseEntity *pEnt ) +{ + Vector movedir; + + UTIL_MakeVectors( pEnt->pev->angles ); + movedir.z = fabs(gpGlobals->v_forward.x); + movedir.x = fabs(gpGlobals->v_forward.y); + movedir.y = fabs(gpGlobals->v_forward.z); + + pEnt->pev->movedir = movedir; + pEnt->pev->angles = g_vecZero; +} + +void UTIL_LinearVector( CBaseEntity *pEnt ) +{ + if( pEnt->pev->angles == Vector( 0, -1, 0 )) + { + pEnt->pev->movedir = Vector( 0, 0, 1 ); + } + else if( pEnt->pev->angles == Vector( 0, -2, 0 )) + { + pEnt->pev->movedir = Vector( 0, 0, -1); + } + else + { + UTIL_MakeVectors( pEnt->pev->angles ); + pEnt->pev->movedir = gpGlobals->v_forward; + } + pEnt->pev->angles = g_vecZero; +} + +Vector UTIL_GetAngleDistance( Vector vecAngles, float distance ) +{ + //set one length vector + if(vecAngles.x != 0) vecAngles.x = 1; + if(vecAngles.y != 0) vecAngles.y = 1; + if(vecAngles.z != 0) vecAngles.z = 1; + return vecAngles * distance; +} + +Vector UTIL_RandomVector(void) +{ + Vector out; + out.x = RANDOM_FLOAT(-1.0, 1.0); + out.y = RANDOM_FLOAT(-1.0, 1.0); + out.z = RANDOM_FLOAT(-1.0, 1.0); + return out; +} + +Vector UTIL_RandomVector( Vector vmin, Vector vmax ) +{ + Vector out; + out.x = RANDOM_FLOAT( vmin.x, vmax.x ); + out.y = RANDOM_FLOAT( vmin.y, vmax.y ); + out.z = RANDOM_FLOAT( vmin.z, vmax.z ); + return out; +} + +CBaseEntity *UTIL_FindGlobalEntity( string_t classname, string_t globalname ) +{ + CBaseEntity *pReturn = UTIL_FindEntityByString( NULL, "globalname", STRING(globalname) ); + if ( pReturn ) + { + if ( !FClassnameIs( pReturn->pev, STRING(classname) ) ) + { + ALERT( at_debug, "Global entity found %s, wrong class %s\n", STRING(globalname), STRING(pReturn->pev->classname) ); + pReturn = NULL; + } + } + + return pReturn; +} + +void UTIL_FindBreakable( CBaseEntity *Brush ) +{ + Vector mins = Brush->pev->absmin; + Vector maxs = Brush->pev->absmax; + mins.z = Brush->pev->absmax.z; + maxs.z += 8; + + CBaseEntity *pList[256]; + int count = UTIL_EntitiesInBox( pList, 256, mins, maxs, FL_ONGROUND ); + if ( count ) + { + for ( int i = 0; i < count; i++ ) + { + ClearBits( pList[i]->pev->flags, FL_ONGROUND ); + pList[i]->pev->groundentity = NULL; + } + } +} + +CBaseEntity *UTIL_FindEntityForward( CBaseEntity *pMe ) +{ + TraceResult tr; + + UTIL_MakeVectors(pMe->pev->v_angle); + UTIL_TraceLine(pMe->pev->origin + pMe->pev->view_ofs,pMe->pev->origin + pMe->pev->view_ofs + gpGlobals->v_forward * 8192,dont_ignore_monsters, pMe->edict(), &tr ); + if ( tr.flFraction != 1.0 && !FNullEnt( tr.pHit) ) + { + CBaseEntity *pHit = CBaseEntity::Instance( tr.pHit ); + return pHit; + } + return NULL; +} + +void UTIL_FindHullIntersection( const Vector &vecSrc, TraceResult &tr, float *mins, float *maxs, edict_t *pEntity ) +{ + int i, j, k; + float distance; + float *minmaxs[2] = {mins, maxs}; + TraceResult tmpTrace; + Vector vecHullEnd = tr.vecEndPos; + Vector vecEnd; + + distance = 1e6f; + + vecHullEnd = vecSrc + ((vecHullEnd - vecSrc)*2); + UTIL_TraceLine( vecSrc, vecHullEnd, dont_ignore_monsters, pEntity, &tmpTrace ); + if ( tmpTrace.flFraction < 1.0 ) + { + tr = tmpTrace; + return; + } + + for ( i = 0; i < 2; i++ ) + { + for ( j = 0; j < 2; j++ ) + { + for ( k = 0; k < 2; k++ ) + { + vecEnd.x = vecHullEnd.x + minmaxs[i][0]; + vecEnd.y = vecHullEnd.y + minmaxs[j][1]; + vecEnd.z = vecHullEnd.z + minmaxs[k][2]; + + UTIL_TraceLine( vecSrc, vecEnd, dont_ignore_monsters, pEntity, &tmpTrace ); + if ( tmpTrace.flFraction < 1.0 ) + { + float thisDistance = (tmpTrace.vecEndPos - vecSrc).Length(); + if ( thisDistance < distance ) + { + tr = tmpTrace; + distance = thisDistance; + } + } + } + } + } +} + +edict_t *UTIL_FindLandmark( string_t iLandmarkName ) +{ + return UTIL_FindLandmark( STRING( iLandmarkName)); +} + +edict_t *UTIL_FindLandmark( const char *pLandmarkName ) +{ + CBaseEntity *pLandmark; + + pLandmark = UTIL_FindEntityByTargetname( NULL, pLandmarkName ); + while ( pLandmark ) + { + // Found the landmark + if ( FClassnameIs( pLandmark->pev, "info_landmark" ) ) + return ENT(pLandmark->pev); + else pLandmark = UTIL_FindEntityByTargetname( pLandmark, pLandmarkName ); + } + Msg("ERROR: Can't find landmark %s\n", pLandmarkName ); + return NULL; +} + +int UTIL_FindTransition( CBaseEntity *pEntity, string_t iVolumeName ) +{ + return UTIL_FindTransition( pEntity, (char *)STRING( iVolumeName )); +} + +int UTIL_FindTransition( CBaseEntity *pEntity, char *pVolumeName ) +{ + CBaseEntity *pVolume; + + if ( pEntity->ObjectCaps() & FCAP_FORCE_TRANSITION ) return 1; + + // If you're following another entity, follow it through the transition (weapons follow the player) + if ( pEntity->pev->movetype == MOVETYPE_FOLLOW && pEntity->pev->aiment != NULL) + { + pEntity = CBaseEntity::Instance( pEntity->pev->aiment ); + } + + int inVolume = 1; // Unless we find a trigger_transition, everything is in the volume + + pVolume = UTIL_FindEntityByTargetname( NULL, pVolumeName ); + while ( pVolume ) + { + if ( FClassnameIs( pVolume->pev, "trigger_transition" ) ) + { + if ( pVolume->Intersects( pEntity ) ) // It touches one, it's in the volume + return 1; + else inVolume = 0; // Found a trigger_transition, but I don't intersect it + } + pVolume = UTIL_FindEntityByTargetname( pVolume, pVolumeName ); + } + return inVolume; +} + +//======================================================================== +// UTIL_ClearPTR - clear all pointers before changelevel +//======================================================================== +void UTIL_ClearPTR( void ) +{ + CBaseEntity *pEntity = NULL; + + for ( int i = 1; i <= gpGlobals->maxEntities; i++ ) + { + edict_t *pEntityEdict = INDEXENT( i ); + if ( pEntityEdict && !pEntityEdict->free && !FStringNull(pEntityEdict->v.globalname) ) + { + pEntity = CBaseEntity::Instance( pEntityEdict ); + } + if (!pEntity) continue; + else pEntity->ClearPointers(); + } +} + +//======================================================================== +// UTIL_ChangeLevel - used for loading next level +//======================================================================== +void UTIL_ChangeLevel( string_t mapname, string_t spotname ) +{ + UTIL_ChangeLevel((char *)STRING( mapname ), (char *)STRING( spotname )); +} + +void UTIL_ChangeLevel( const char *szNextMap, const char *szNextSpot ) +{ + edict_t *pentLandmark; + LEVELLIST levels[16]; + + ASSERT(!FStrEq(szNextMap, "")); + + // Don't work in deathmatch + if ( IsMultiplayer()) return; + // Some people are firing these multiple times in a frame, disable + if ( NewLevel ) return; + + CBaseEntity *pPlayer = UTIL_PlayerByIndex( 1 ); + if (!UTIL_FindTransition( pPlayer, (char *)szNextSpot )) + { + DevMsg( "Player isn't in the transition volume %s, aborting\n", szNextSpot ); + return; + } + + // This object will get removed in the call to CHANGE_LEVEL, copy the params into "safe" memory + strcpy(st_szNextMap, szNextMap); + st_szNextSpot[0] = 0; // Init landmark to NULL + + // look for a landmark entity + pentLandmark = UTIL_FindLandmark( szNextSpot ); + if ( !FNullEnt( pentLandmark ) ) + { + strcpy(st_szNextSpot, szNextSpot); + gpGlobals->spotOffset = VARS(pentLandmark)->origin; + } + + //try to found bsp file before loading nextlevel + char path[128]; + + sprintf(path, "maps/%s.bsp", st_szNextMap); + byte *data = LOAD_FILE(path, NULL); + if (data) + { + UTIL_ClearPTR(); + FREE_FILE( data ); + DevMsg( "CHANGE LEVEL: %s %s\n", st_szNextMap, st_szNextSpot ); + CHANGE_LEVEL( st_szNextMap, st_szNextSpot ); + } + else Msg("Warning! Map %s not found!\n", st_szNextMap ); + NewLevel = TRUE;//bit who indiactes new level +} + +//======================================================================== +// Parent system utils +// Used for physics code too +//======================================================================== +void UTIL_MarkChild ( CBaseEntity *pEnt, BOOL correctSpeed, BOOL desired ) +{ + if(desired) //post affect + { + SetBits(pEnt->pFlags, PF_DESIRED); + if (CanAffect) + { //apply post affect now + PostFrameAffect( pEnt ); + return; + } + } + else + { + SetBits(pEnt->pFlags, PF_AFFECT); + + if (correctSpeed) + SetBits (pEnt->pFlags, PF_CORECTSPEED); + else ClearBits (pEnt->pFlags, PF_CORECTSPEED); + } + LinkChild( pEnt );//link children +} + +void UTIL_SetThink ( CBaseEntity *pEnt ) +{ + SetBits (pEnt->pFlags, PF_SETTHINK); + pEnt->DontThink(); + UTIL_MarkChild( pEnt ); +} + +void UTIL_SetPostAffect( CBaseEntity *pEnt ) +{ + SetBits(pEnt->pFlags, PF_POSTAFFECT); + UTIL_MarkChild( pEnt ); +} + +void UTIL_SetAction( CBaseEntity *pEnt ) +{ + SetBits(pEnt->pFlags, PF_ACTION); + UTIL_MarkChild( pEnt ); +} +//======================================================================== +// Assign origin and angles +//======================================================================== +void UTIL_AssignOrigin( CBaseEntity *pEntity, const Vector vecOrigin, BOOL bInitiator) +{ + Vector vecDiff = vecOrigin - pEntity->pev->origin; + + UTIL_SetOrigin(pEntity, vecOrigin ); + + if (bInitiator && pEntity->m_pParent) + { + pEntity->OffsetOrigin = pEntity->pev->origin - pEntity->m_pParent->pev->origin; + } + if (pEntity->m_pChild) + { + CBaseEntity* pChild = pEntity->m_pChild; + + Vector vecTemp; + while (pChild) + { + if (pChild->pev->movetype != MOVETYPE_PUSH || pChild->pev->velocity == pEntity->pev->velocity) + { + UTIL_AssignOrigin( pChild, vecOrigin + pChild->OffsetOrigin, FALSE ); + } + else + { + vecTemp = vecDiff + pChild->pev->origin; + UTIL_AssignOrigin( pChild, vecTemp, FALSE ); + } + pChild = pChild->m_pNextChild; + } + } +} +void UTIL_AssignAngles( CBaseEntity *pEntity, const Vector vecAngles, BOOL bInitiator) +{ + Vector vecDiff = vecAngles - pEntity->pev->angles; + + UTIL_SetAngles(pEntity, vecAngles); + + if (bInitiator && pEntity->m_pParent) + pEntity->OffsetAngles = vecAngles - pEntity->m_pParent->pev->angles; + + if (pEntity->m_pChild) // now I've moved pEntity, does anything else have to move with it? + { + CBaseEntity* pChild = pEntity->m_pChild; + Vector vecTemp; + while (pChild) + { + if (pChild->pev->avelocity == pEntity->pev->avelocity) + UTIL_AssignAngles( pChild, vecAngles + pChild->OffsetAngles, FALSE ); + else + { + vecTemp = vecDiff + pChild->pev->angles; + UTIL_AssignAngles( pChild, vecTemp, FALSE ); + } + pChild = pChild->m_pNextChild; + } + } +} + +//======================================================================== +// Set origin and angles +//======================================================================== +void UTIL_MergePos ( CBaseEntity *pEnt, int loopbreaker ) +{ + if (loopbreaker <= 0)return; + if (!pEnt->m_pParent)return; + + Vector forward, right, up, vecOrg, vecAngles; + + UTIL_MakeVectorsPrivate( pEnt->m_pParent->pev->angles, forward, right, up ); + + if(pEnt->m_pParent->pev->flags & FL_MONSTER) + vecOrg = pEnt->PostOrigin = pEnt->m_pParent->pev->origin + (forward * pEnt->OffsetOrigin.x) + ( right * pEnt->OffsetOrigin.y) + (up * pEnt->OffsetOrigin.z); + else vecOrg = pEnt->PostOrigin = pEnt->m_pParent->pev->origin + (forward * pEnt->OffsetOrigin.x) + (-right * pEnt->OffsetOrigin.y) + (up * pEnt->OffsetOrigin.z); + + vecAngles = pEnt->PostAngles = pEnt->m_pParent->pev->angles + pEnt->OffsetAngles; + SetBits(pEnt->pFlags, PF_POSTAFFECT | PF_DESIRED ); + + if ( pEnt->m_pChild ) + { + CBaseEntity *pMoving = pEnt->m_pChild; + int sloopbreaker = MAX_CHILDS; + while (pMoving) + { + UTIL_MergePos(pMoving, loopbreaker - 1 ); + pMoving = pMoving->m_pNextChild; + sloopbreaker--; + if (sloopbreaker <= 0)break; + } + } + + if(pEnt->pFlags & PF_MERGEPOS) + { + UTIL_AssignOrigin( pEnt, vecOrg ); + UTIL_AssignAngles( pEnt, vecAngles ); + ClearBits(pEnt->pFlags, PF_MERGEPOS); + } + if(pEnt->pFlags & PF_POSTORG) + { + pEnt->pev->origin = vecOrg; + pEnt->pev->angles = vecAngles; + } +} + +void UTIL_ComplexRotate( CBaseEntity *pParent, CBaseEntity *pChild, const Vector &dest, float m_flTravelTime ) +{ + float time = m_flTravelTime; + Vector vel = pParent->pev->velocity; + + // Attempt at getting the train to rotate properly around the origin of the trackchange + if ( time <= 0 ) return; + + Vector offset = pChild->pev->origin - pParent->pev->origin; + Vector delta = dest - pParent->pev->angles; + // Transform offset into local coordinates + UTIL_MakeInvVectors( delta, gpGlobals ); + Vector local; + local.x = DotProduct( offset, gpGlobals->v_forward ); + local.y = DotProduct( offset, gpGlobals->v_right ); + local.z = DotProduct( offset, gpGlobals->v_up ); + + local = local - offset; + pChild->pev->velocity = vel + (local * (1.0 / time)); + pChild->pev->avelocity = pParent->pev->avelocity; +} + +void UTIL_SetOrigin( CBaseEntity *pEntity, const Vector &vecOrigin ) +{ + SET_ORIGIN(ENT(pEntity->pev), vecOrigin ); +} + +void UTIL_SetAngles( CBaseEntity *pEntity, const Vector &vecAngles ) +{ + pEntity->pev->angles = vecAngles; +} + +void UTIL_SynchDoors( CBaseEntity *pEntity ) +{ + CBaseDoor *pDoor = NULL; + + if ( !FStringNull( pEntity->pev->targetname ) ) + { + while(1) + { + pDoor = (CBaseDoor *)UTIL_FindEntityByTargetname (pDoor, STRING(pEntity->pev->targetname)); + + if ( pDoor != pEntity ) + { + if (FNullEnt(pDoor)) break; + + if ( FClassnameIs ( pDoor, "func_door" ) && pDoor->m_flWait >= 0 ) + { + if (pDoor->pev->velocity == pEntity->pev->velocity) + { + UTIL_SetOrigin( pDoor, pEntity->pev->origin ); + UTIL_SetVelocity( pDoor, g_vecZero ); + } + if (pDoor->GetState() == STATE_TURN_ON) pDoor->DoorGoDown(); + else if (pDoor->GetState() == STATE_TURN_OFF) pDoor->DoorGoUp(); + } + if( FClassnameIs ( pDoor, "func_door_rotating" ) && pDoor->m_flWait >= 0 ) + { + if(pDoor->pev->avelocity == pEntity->pev->avelocity) + { + UTIL_SetAngles( pDoor, pEntity->pev->angles ); + UTIL_SetAvelocity( pDoor, g_vecZero ); + } + if (pDoor->GetState() == STATE_TURN_ON) pDoor->DoorGoDown(); + else if (pDoor->GetState() == STATE_TURN_OFF) pDoor->DoorGoUp(); + } + } + } + } +} + +//======================================================================== +// Set childs velocity and avelocity +//======================================================================== +void UTIL_SetChildVelocity ( CBaseEntity *pEnt, const Vector vecSet, int loopbreaker ) +{ + if (loopbreaker <= 0)return; + if (!pEnt->m_pParent)return; + + Vector vecNew; + vecNew = (pEnt->pev->velocity - pEnt->m_pParent->pev->velocity) + vecSet; + + if ( pEnt->m_pChild ) + { + CBaseEntity *pMoving = pEnt->m_pChild; + int sloopbreaker = MAX_CHILDS; + while (pMoving) + { + UTIL_SetChildVelocity(pMoving, vecNew, loopbreaker - 1 ); + pMoving = pMoving->m_pNextChild; + sloopbreaker--; + if (sloopbreaker <= 0)break; + } + } + pEnt->pev->velocity = vecNew; +} + +void UTIL_SetVelocity ( CBaseEntity *pEnt, const Vector vecSet ) +{ + Vector vecNew; + if (pEnt->m_pParent) + vecNew = vecSet + pEnt->m_pParent->pev->velocity; + else vecNew = vecSet; + + if ( pEnt->m_pChild ) + { + CBaseEntity *pMoving = pEnt->m_pChild; + int sloopbreaker = MAX_CHILDS; + while (pMoving) + { + UTIL_SetChildVelocity(pMoving, vecNew, MAX_CHILDS ); + if(vecSet != g_vecZero)SetBits(pMoving->pFlags, PF_PARENTMOVE); + else ClearBits(pMoving->pFlags, PF_PARENTMOVE); + + pMoving = pMoving->m_pNextChild; + sloopbreaker--; + if (sloopbreaker <= 0)break; + } + } + pEnt->pev->velocity = vecNew; +} + +void UTIL_SetChildAvelocity ( CBaseEntity *pEnt, const Vector vecSet, int loopbreaker ) +{ + if (loopbreaker <= 0)return; + if (!pEnt->m_pParent)return; + + Vector vecNew = (pEnt->pev->avelocity - pEnt->m_pParent->pev->avelocity) + vecSet; + + if ( pEnt->m_pChild ) + { + CBaseEntity *pMoving = pEnt->m_pChild; + int sloopbreaker = MAX_CHILDS; + while (pMoving) + { + UTIL_SetChildAvelocity(pMoving, vecNew, loopbreaker - 1 ); + pMoving = pMoving->m_pNextChild; + sloopbreaker--; + if (sloopbreaker <= 0)break; + } + } + + pEnt->pev->avelocity = vecNew; +} + +void UTIL_SetAvelocity ( CBaseEntity *pEnt, const Vector vecSet ) +{ + Vector vecNew; + + if (pEnt->m_pParent) + vecNew = vecSet + pEnt->m_pParent->pev->avelocity; + else vecNew = vecSet; + + if ( pEnt->m_pChild ) + { + CBaseEntity *pMoving = pEnt->m_pChild; + int sloopbreaker = MAX_CHILDS; + while (pMoving) + { + UTIL_SetChildAvelocity(pMoving, vecNew, MAX_CHILDS ); + UTIL_MergePos( pMoving ); + if(vecSet != g_vecZero)SetBits(pMoving->pFlags, PF_PARENTMOVE); + else ClearBits(pMoving->pFlags, PF_PARENTMOVE); + + pMoving = pMoving->m_pNextChild; + sloopbreaker--; + if (sloopbreaker <= 0)break; + } + } + pEnt->pev->avelocity = vecNew; +} + +//======================================================================== +// Precache and set resources - add check for present or invalid name +// Don't crash game if model not found or no specified +//======================================================================== +void UTIL_SetModel( edict_t *e, string_t s, char *c )//set default model if not found +{ + if (FStringNull( s )) UTIL_SetModel( e, c ); + else UTIL_SetModel( e, s ); +} +void UTIL_SetModel( edict_t *e, string_t model ){ UTIL_SetModel( e, STRING(model)); } +void UTIL_SetModel( edict_t *e, const char *model ) +{ + //if(g_serveractive && g_engfuncs.pfnModelIndex( model ) >= 0) return; + if(!model || !*model) + { + g_engfuncs.pfnSetModel(e, "models/common/null.mdl"); + return; + } + //is this brush model? + if (model[0] == '*') + { + g_engfuncs.pfnSetModel(e, model); + return; + } + + //verify file exists + byte *data = LOAD_FILE((char*)model, NULL); + if (data) + { + FREE_FILE( data ); + g_engfuncs.pfnSetModel(e, model); + return; + } + + int namelen = strlen(model) - 1; + if (model[namelen] == 'l') + { + //this is model + g_engfuncs.pfnSetModel(e, "models/common/error.mdl"); + } + else if (model[namelen] == 'r') + { + //this is sprite + g_engfuncs.pfnSetModel(e, "sprites/error.spr"); + } + else + { + //set null model + g_engfuncs.pfnSetModel(e, "models/common/null.mdl"); + } +} + +int UTIL_PrecacheModel( string_t s, char *e )//precache default model if not found +{ + if (FStringNull( s )) + return UTIL_PrecacheModel( e ); + return UTIL_PrecacheModel( s ); +} +int UTIL_PrecacheModel( string_t s ){ return UTIL_PrecacheModel( (char*)STRING(s)); } +int UTIL_PrecacheModel( char* s ) +{ + if(!s || !*s) + { + ALERT(at_console,"Warning: modelname not specified\n"); + return g_sModelIndexNullModel; //set null model + } + //no need to precacahe brush + if (s[0] == '*') return 0; + + //verify file exists + byte *data = LOAD_FILE(s, NULL); + if (data) + { + FREE_FILE( data ); + return g_engfuncs.pfnPrecacheModel(s); + } + + int namelen = strlen(s) - 1; + if (s[namelen] == 'l') + { + //this is model + ALERT(at_console,"Warning: model \"%s\" not found!\n",s); + return g_sModelIndexErrorModel; + } + else if (s[namelen] == 'r') + { + //this is sprite + ALERT(at_console,"Warning: sprite \"%s\" not found!\n",s); + return g_sModelIndexErrorSprite; + } + else + { + //unknown format + ALERT(at_console,"Warning: invalid name \"%s\"!\n",s); + return g_sModelIndexNullModel; //set null model + } +} + +//======================================================================== +// Precaches the ammo and queues the ammo info for sending to clients +//======================================================================== +void AddAmmoName( const char *szAmmoname ) +{ + // make sure it's not already in the registry + for ( int i = 0; i < MAX_AMMO_SLOTS; i++ ) + { + if ( !CBasePlayerWeapon::AmmoInfoArray[i].pszName) continue; + if ( stricmp( CBasePlayerWeapon::AmmoInfoArray[i].pszName, szAmmoname ) == 0 ) + return; // ammo already in registry, just quite + } + giAmmoIndex++; + ASSERT( giAmmoIndex < MAX_AMMO_SLOTS ); + if ( giAmmoIndex >= MAX_AMMO_SLOTS ) + giAmmoIndex = 0; + + CBasePlayerWeapon::AmmoInfoArray[giAmmoIndex].pszName = szAmmoname; + CBasePlayerWeapon::AmmoInfoArray[giAmmoIndex].iId = giAmmoIndex;// yes, this info is redundant +} + +//======================================================================== +// Precaches entity from other entity +//======================================================================== +void UTIL_PrecacheEntity( string_t szClassname ) { UTIL_PrecacheEntity( (char *)STRING(szClassname) ); } +void UTIL_PrecacheEntity( const char *szClassname ) +{ + edict_t *pent; + int istr = ALLOC_STRING( szClassname ); + + // check for virtual entities + if( FUNCTION_FROM_NAME( szClassname )) + { + pent = CREATE_NAMED_ENTITY(istr); + if ( FNullEnt( pent )) return; + } + else if( !strncmp( szClassname, "weapon_", 7 )) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("weapon_generic")); + if ( FNullEnt( pent )) return; //this never gonna called anymore. just in case + pent->v.netname = istr; + } + else //unknown error + { + Msg("can't create %s\n", szClassname ); + return; + } + + CBaseEntity *pEntity = CBaseEntity::Instance (VARS( pent )); + if (pEntity) pEntity->Precache(); + REMOVE_ENTITY(pent); +} + +//======================================================================== +// Precaches aurora particle and set it +//======================================================================== +int UTIL_PrecacheAurora( string_t s ) { return UTIL_PrecacheAurora( (char *)STRING(s)); } +int UTIL_PrecacheAurora( const char *s ) +{ + char path[128]; //path length + sprintf(path, "scripts/aurora/%s.aur", s); + + byte *data = LOAD_FILE(path, NULL); + if (data) + { + FREE_FILE( data ); + return ALLOC_STRING(path); + } + else //otherwise + { + if(!s || !*s)Msg( "Warning: Aurora not specified!\n", s); + else Msg( "Warning: Aurora %s not found!\n", s); + return ALLOC_STRING( "scripts/aurora/error.aur"); + } +} + +void UTIL_SetAurora( CBaseEntity *pAttach, int aur, int attachment ) +{ + MESSAGE_BEGIN( MSG_ALL, gmsgParticle ); + WRITE_BYTE( pAttach->entindex() ); + WRITE_STRING( STRING(aur) ); + MESSAGE_END(); +} +//======================================================================== +// Set client beams +//======================================================================== +void UTIL_SetBeams( char *szFile, CBaseEntity *pStart, CBaseEntity *pEnd ) +{ + MESSAGE_BEGIN( MSG_ALL, gmsgBeams ); + WRITE_STRING( szFile ); + WRITE_BYTE( pStart->entindex());//beam start entity + WRITE_BYTE( pEnd->entindex() );//beam end entity + MESSAGE_END(); +} + +//======================================================================== +// Precaches and play sound +//======================================================================== +int UTIL_PrecacheSound( string_t s, char *e )//precache default model if not found +{ + if (FStringNull( s )) + return UTIL_PrecacheSound( e ); + return UTIL_PrecacheSound( s ); +} +int UTIL_PrecacheSound( string_t s ){ return UTIL_PrecacheSound( (char*)STRING(s)); } +int UTIL_PrecacheSound( char* s ) +{ + if(!s || !*s) return MAKE_STRING("common/null.wav"); //set null sound + if(*s == '!') return MAKE_STRING(s); //sentence - just make string + + //LOAD_FILE_FOR_ME will cause problems for some users. Disabled + g_engfuncs.pfnPrecacheSound(s); + return MAKE_STRING(s); + + //NOTE: Engine function as predicted for sound folder + //But LOAD_FILE don't known about this. Set it manualy + + char path[256]; //path size + sprintf(path, "sound/%s", s); + + //verify file exists + byte *data = LOAD_FILE(path, NULL); + if (data) + { + FREE_FILE( data ); + g_engfuncs.pfnPrecacheSound(s); + return MAKE_STRING(s); + } + int namelen = strlen(s) - 1; + if (s[namelen] == 'v') + { + //this is sound + ALERT(at_console,"Warning: sound \"%s\" not found!\n",s); + g_engfuncs.pfnPrecacheSound("common/null.wav"); + return MAKE_STRING("common/null.wav"); //set null sound + } + else + { + //unknown format + ALERT(at_console,"Warning: invalid name \"%s\"!\n",s); + g_engfuncs.pfnPrecacheSound("common/null.wav"); + return MAKE_STRING("common/null.wav"); //set null sound + } +} + +int UTIL_LoadSoundPreset( string_t pString ) { return UTIL_LoadSoundPreset( (char*)STRING(pString) ); } +int UTIL_LoadSoundPreset( const char *pString ) +{ + //try to load direct sound path + //so we supported 99 presets... + int m_sound, namelen = strlen(pString) - 1; + + if(namelen > 2)//yes, it's sound path + m_sound = ALLOC_STRING( pString ); + else if(pString[0] == '!')//sentence + m_sound = ALLOC_STRING( pString ); + else m_sound = atoi(pString);//no, it's preset + return m_sound; +} + +int UTIL_LoadDecalPreset( string_t pString ) { return UTIL_LoadDecalPreset( (char*)STRING(pString) ); } +int UTIL_LoadDecalPreset( const char *pString ) +{ + //try to load direct sound path + //so we supported 9 decal groups... + int m_decal, namelen = strlen(pString) - 1; + + if(namelen > 1)//yes, it's decal name + m_decal = ALLOC_STRING( pString ); + else m_decal = atoi(pString);//no, it's preset + return m_decal; +} + +float UTIL_CalcDistance( Vector vecAngles ) +{ + for(int i = 0; i < 3; i++ ) + { + if(vecAngles[i] < -180) vecAngles[i] += 360; + else if(vecAngles[i] > 180) vecAngles[i] -= 360; + } + return vecAngles.Length(); +} + +//======================================================================== +// Watches for pWatching enetity and rotate pWatcher entity angles +//======================================================================== +void UTIL_WatchTarget( CBaseEntity *pWatcher, CBaseEntity *pTarget) +{ + Vector vecGoal = UTIL_VecToAngles( (pTarget->pev->origin - pWatcher->pev->origin).Normalize() ); + vecGoal.x = -vecGoal.x; + + if (pWatcher->pev->angles.y > 360) pWatcher->pev->angles.y -= 360; + if (pWatcher->pev->angles.y < 0) pWatcher->pev->angles.y += 360; + + float dx = vecGoal.x - pWatcher->pev->angles.x; + float dy = vecGoal.y - pWatcher->pev->angles.y; + + if (dx < -180) dx += 360; + if (dx > 180) dx = dx - 360; + + if (dy < -180) dy += 360; + if (dy > 180) dy = dy - 360; + + pWatcher->pev->avelocity.x = dx * pWatcher->pev->speed * gpGlobals->frametime; + pWatcher->pev->avelocity.y = dy * pWatcher->pev->speed * gpGlobals->frametime; +} + +float UTIL_Approach( float target, float value, float speed ) +{ + float delta = target - value; + + if ( delta > speed ) + value += speed; + else if ( delta < -speed ) + value -= speed; + else + value = target; + + return value; +} + + +float UTIL_ApproachAngle( float target, float value, float speed ) +{ + target = UTIL_AngleMod( target ); + value = UTIL_AngleMod( target ); + + float delta = target - value; + + // Speed is assumed to be positive + if ( speed < 0 ) + speed = -speed; + + if ( delta < -180 ) + delta += 360; + else if ( delta > 180 ) + delta -= 360; + + if ( delta > speed ) + value += speed; + else if ( delta < -speed ) + value -= speed; + else + value = target; + + return value; +} + + +float UTIL_AngleDistance( float next, float cur ) +{ + float delta = next - cur; + + while ( delta < -180 ) + delta += 360; + while ( delta > 180 ) + delta -= 360; + + return delta; +} + +BOOL UTIL_EntIsVisible( entvars_t* pev, entvars_t* pevTarget) +{ + Vector vecSpot1 = pev->origin + pev->view_ofs; + Vector vecSpot2 = pevTarget->origin + pevTarget->view_ofs; + TraceResult tr; + + UTIL_TraceLine( vecSpot1, vecSpot2, ignore_monsters, ENT(pev), &tr ); + + // FIXME: rewrote this relationship + if( tr.iContents & MASK_WATER ) + return FALSE; // sight line crossed contents + if( tr.flFraction == 1 ) return TRUE; + return FALSE; +} + +//======================================================================== +// Place all system resourses here +//======================================================================== +void UTIL_PrecacheResourse( void ) +{ + //constant precaches + + //null and errors stuff + g_sModelIndexErrorModel = UTIL_PrecacheModel("models/common/error.mdl");//last crash point + g_sModelIndexErrorSprite = UTIL_PrecacheModel("sprites/error.spr"); + g_sModelIndexNullModel = UTIL_PrecacheModel("models/common/null.mdl"); + g_sModelIndexNullSprite = UTIL_PrecacheModel("sprites/null.spr"); + + //global sprites and models + g_sModelIndexFireball = UTIL_PrecacheModel ("sprites/explode.spr");// fireball + g_sModelIndexWExplosion = UTIL_PrecacheModel ("sprites/wxplode.spr");// underwater fireball + g_sModelIndexSmoke = UTIL_PrecacheModel ("sprites/steam1.spr");// smoke + g_sModelIndexBubbles = UTIL_PrecacheModel ("sprites/bubble.spr");//bubbles + g_sModelIndexLaser = UTIL_PrecacheModel( "sprites/laserbeam.spr" ); + g_sModelIndexBloodSpray = UTIL_PrecacheModel ("sprites/bloodspray.spr"); + g_sModelIndexBloodDrop = UTIL_PrecacheModel ("sprites/blood.spr"); + + //player items and weapons + memset( CBasePlayerWeapon::ItemInfoArray, 0, sizeof(CBasePlayerWeapon::ItemInfoArray) ); + memset( CBasePlayerWeapon::AmmoInfoArray, 0, sizeof(CBasePlayerWeapon::AmmoInfoArray) ); + giAmmoIndex = 0; + + //custom precaches + char token[256]; + char *pfile = (char *)LOAD_FILE( "scripts/precache.txt", NULL ); + if(pfile) + { + while ( pfile ) + { + if ( !stricmp( token, "entity" )) + { + pfile = COM_ParseFile(pfile, token); + UTIL_PrecacheEntity( ALLOC_STRING(token) ); + } + else if ( !stricmp( token, "dmentity" )) + { + pfile = COM_ParseFile(pfile, token); + if( IsDeatchmatch()) UTIL_PrecacheEntity( ALLOC_STRING( token )); + } + else if ( !stricmp( token, "model" )) + { + pfile = COM_ParseFile(pfile, token); + UTIL_PrecacheModel( ALLOC_STRING(token) ); + } + else if ( !stricmp( token, "dmmodel" )) + { + pfile = COM_ParseFile(pfile, token); + if( IsDeatchmatch()) UTIL_PrecacheModel( ALLOC_STRING( token )); + } + else if ( !stricmp( token, "sound" )) + { + pfile = COM_ParseFile(pfile, token); + UTIL_PrecacheSound( ALLOC_STRING( token )); + } + else if ( !stricmp( token, "dmsound" )) + { + pfile = COM_ParseFile( pfile, token ); + if( IsDeatchmatch()) UTIL_PrecacheSound( ALLOC_STRING( token )); + } + else if ( !stricmp( token, "aurora" )) + { + pfile = COM_ParseFile(pfile, token); + UTIL_PrecacheAurora( ALLOC_STRING( token )); + } + pfile = COM_ParseFile(pfile, token); + } + COM_FreeFile(pfile); + } +} + +BOOL IsMultiplayer ( void ) +{ + if( g_pGameRules->IsMultiplayer() ) + return TRUE; + return FALSE; +} + +BOOL IsDeatchmatch ( void ) +{ + if( g_pGameRules->IsDeathmatch() ) + return TRUE; + return FALSE; +} + +float UTIL_WeaponTimeBase( void ) +{ + return gpGlobals->time; +} + +static unsigned int glSeed = 0; + +unsigned int seed_table[ 256 ] = +{ + 28985, 27138, 26457, 9451, 17764, 10909, 28790, 8716, 6361, 4853, 17798, 21977, 19643, 20662, 10834, 20103, + 27067, 28634, 18623, 25849, 8576, 26234, 23887, 18228, 32587, 4836, 3306, 1811, 3035, 24559, 18399, 315, + 26766, 907, 24102, 12370, 9674, 2972, 10472, 16492, 22683, 11529, 27968, 30406, 13213, 2319, 23620, 16823, + 10013, 23772, 21567, 1251, 19579, 20313, 18241, 30130, 8402, 20807, 27354, 7169, 21211, 17293, 5410, 19223, + 10255, 22480, 27388, 9946, 15628, 24389, 17308, 2370, 9530, 31683, 25927, 23567, 11694, 26397, 32602, 15031, + 18255, 17582, 1422, 28835, 23607, 12597, 20602, 10138, 5212, 1252, 10074, 23166, 19823, 31667, 5902, 24630, + 18948, 14330, 14950, 8939, 23540, 21311, 22428, 22391, 3583, 29004, 30498, 18714, 4278, 2437, 22430, 3439, + 28313, 23161, 25396, 13471, 19324, 15287, 2563, 18901, 13103, 16867, 9714, 14322, 15197, 26889, 19372, 26241, + 31925, 14640, 11497, 8941, 10056, 6451, 28656, 10737, 13874, 17356, 8281, 25937, 1661, 4850, 7448, 12744, + 21826, 5477, 10167, 16705, 26897, 8839, 30947, 27978, 27283, 24685, 32298, 3525, 12398, 28726, 9475, 10208, + 617, 13467, 22287, 2376, 6097, 26312, 2974, 9114, 21787, 28010, 4725, 15387, 3274, 10762, 31695, 17320, + 18324, 12441, 16801, 27376, 22464, 7500, 5666, 18144, 15314, 31914, 31627, 6495, 5226, 31203, 2331, 4668, + 12650, 18275, 351, 7268, 31319, 30119, 7600, 2905, 13826, 11343, 13053, 15583, 30055, 31093, 5067, 761, + 9685, 11070, 21369, 27155, 3663, 26542, 20169, 12161, 15411, 30401, 7580, 31784, 8985, 29367, 20989, 14203, + 29694, 21167, 10337, 1706, 28578, 887, 3373, 19477, 14382, 675, 7033, 15111, 26138, 12252, 30996, 21409, + 25678, 18555, 13256, 23316, 22407, 16727, 991, 9236, 5373, 29402, 6117, 15241, 27715, 19291, 19888, 19847 +}; + +unsigned int U_Random( void ) +{ + glSeed *= 69069; + glSeed += seed_table[ glSeed & 0xff ]; + + return ( ++glSeed & 0x0fffffff ); +} + +void U_Srand( unsigned int seed ) +{ + glSeed = seed_table[ seed & 0xff ]; +} + +/* +===================== +UTIL_SharedRandomLong +===================== +*/ +int UTIL_SharedRandomLong( unsigned int seed, int low, int high ) +{ + unsigned int range; + + U_Srand( (int)seed + low + high ); + + range = high - low + 1; + if ( !(range - 1) ) + { + return low; + } + else + { + int offset; + int rnum; + + rnum = U_Random(); + + offset = rnum % range; + + return (low + offset); + } +} + +/* +===================== +UTIL_SharedRandomFloat +===================== +*/ +float UTIL_SharedRandomFloat( unsigned int seed, float low, float high ) +{ + // + unsigned int range; + + U_Srand( (int)seed + *(int *)&low + *(int *)&high ); + + U_Random(); + U_Random(); + + range = high - low; + if ( !range ) + { + return low; + } + else + { + int tensixrand; + float offset; + + tensixrand = U_Random() & 65535; + + offset = (float)tensixrand / 65536.0; + + return (low + offset * range ); + } +} + +#ifdef DEBUG +edict_t *DBG_EntOfVars( const entvars_t *pev ) +{ + if (pev->pContainingEntity != NULL) + return pev->pContainingEntity; + ALERT(at_debug, "entvars_t pContainingEntity is NULL, calling into engine"); + edict_t* pent = (*g_engfuncs.pfnFindEntityByVars)((entvars_t*)pev); + if (pent == NULL) + ALERT(at_debug, "DAMN! Even the engine couldn't FindEntityByVars!"); + ((entvars_t *)pev)->pContainingEntity = pent; + return pent; +} +#endif //DEBUG + + +#ifdef DEBUG + void +DBG_AssertFunction( + BOOL fExpr, + const char* szExpr, + const char* szFile, + int szLine, + const char* szMessage) + { + if (fExpr) + return; + char szOut[512]; + if (szMessage != NULL) + sprintf(szOut, "ASSERT FAILED:\n %s \n(%s@%d)\n%s", szExpr, szFile, szLine, szMessage); + else + sprintf(szOut, "ASSERT FAILED:\n %s \n(%s@%d)", szExpr, szFile, szLine); + ALERT(at_debug, szOut); + } +#endif // DEBUG + +BOOL UTIL_GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pCurrentWeapon ) +{ + return g_pGameRules->GetNextBestWeapon( pPlayer, pCurrentWeapon ); +} + +// ripped this out of the engine +float UTIL_AngleMod(float a) +{ + if (a < 0) + { + a = a + 360 * ((int)(a / 360) + 1); + } + else if (a >= 360) + { + a = a - 360 * ((int)(a / 360)); + } + // a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535); + return a; +} + +float UTIL_AngleDiff( float destAngle, float srcAngle ) +{ + float delta; + + delta = destAngle - srcAngle; + if ( destAngle > srcAngle ) + { + if ( delta >= 180 ) + delta -= 360; + } + else + { + if ( delta <= -180 ) + delta += 360; + } + return delta; +} + +Vector UTIL_VecToAngles( const Vector &vec ) +{ + float rgflVecOut[3]; + VEC_TO_ANGLES(vec, rgflVecOut); + return Vector(rgflVecOut); +} + +//LRC - pass in a normalised axis vector and a number of degrees, and this returns the corresponding +// angles value for an entity. +inline Vector UTIL_AxisRotationToAngles( const Vector &vecAxis, float flDegs ) +{ + Vector vecTemp = UTIL_AxisRotationToVec( vecAxis, flDegs ); + float rgflVecOut[3]; + //ugh, mathsy. + rgflVecOut[0] = asin(vecTemp.z) * (-180.0 / M_PI); + rgflVecOut[1] = acos(vecTemp.x) * (180.0 / M_PI); + if (vecTemp.y < 0) + rgflVecOut[1] = -rgflVecOut[1]; + rgflVecOut[2] = 0; //for now + return Vector(rgflVecOut); +} + +//LRC - as above, but returns the position of point 1 0 0 under the given rotation +Vector UTIL_AxisRotationToVec( const Vector &vecAxis, float flDegs ) +{ + float rgflVecOut[3]; + float flRads = flDegs * (M_PI / 180.0); + float c = cos(flRads); + float s = sin(flRads); + float v = vecAxis.x * (1-c); + //ugh, more maths. Thank goodness for internet geometry sites... + rgflVecOut[0] = vecAxis.x*v + c; + rgflVecOut[1] = vecAxis.y*v + vecAxis.z*s; + rgflVecOut[2] = vecAxis.z*v - vecAxis.y*s; + return Vector(rgflVecOut); +} + +// float UTIL_MoveToOrigin( edict_t *pent, const Vector vecGoal, float flDist, int iMoveType ) +void UTIL_MoveToOrigin( edict_t *pent, const Vector &vecGoal, float flDist, int iMoveType ) +{ + float rgfl[3]; + vecGoal.CopyToArray(rgfl); +// return MOVE_TO_ORIGIN ( pent, rgfl, flDist, iMoveType ); + MOVE_TO_ORIGIN ( pent, rgfl, flDist, iMoveType ); +} + + +int UTIL_EntitiesInBox( CBaseEntity **pList, int listMax, const Vector &mins, const Vector &maxs, int flagMask ) +{ + edict_t *pEdict = g_engfuncs.pfnPEntityOfEntIndex( 1 ); + CBaseEntity *pEntity; + int count; + + count = 0; + + if ( !pEdict ) + return count; + + for ( int i = 1; i < gpGlobals->maxEntities; i++, pEdict++ ) + { + if ( pEdict->free ) // Not in use + continue; + + if ( flagMask && !(pEdict->v.flags & flagMask) ) // Does it meet the criteria? + continue; + + if ( mins.x > pEdict->v.absmax.x || + mins.y > pEdict->v.absmax.y || + mins.z > pEdict->v.absmax.z || + maxs.x < pEdict->v.absmin.x || + maxs.y < pEdict->v.absmin.y || + maxs.z < pEdict->v.absmin.z ) + continue; + + pEntity = CBaseEntity::Instance(pEdict); + if ( !pEntity ) + continue; + + pList[ count ] = pEntity; + count++; + + if ( count >= listMax ) + return count; + } + + return count; +} + + +int UTIL_MonstersInSphere( CBaseEntity **pList, int listMax, const Vector ¢er, float radius ) +{ + edict_t *pEdict = g_engfuncs.pfnPEntityOfEntIndex( 1 ); + CBaseEntity *pEntity; + int count; + float distance, delta; + + count = 0; + float radiusSquared = radius * radius; + + if ( !pEdict ) + return count; + + for ( int i = 1; i < gpGlobals->maxEntities; i++, pEdict++ ) + { + if ( pEdict->free ) // Not in use + continue; + + if ( !(pEdict->v.flags & (FL_CLIENT|FL_MONSTER)) ) // Not a client/monster ? + continue; + + // Use origin for X & Y since they are centered for all monsters + // Now X + delta = center.x - pEdict->v.origin.x;//(pEdict->v.absmin.x + pEdict->v.absmax.x)*0.5; + delta *= delta; + + if ( delta > radiusSquared ) + continue; + distance = delta; + + // Now Y + delta = center.y - pEdict->v.origin.y;//(pEdict->v.absmin.y + pEdict->v.absmax.y)*0.5; + delta *= delta; + + distance += delta; + if ( distance > radiusSquared ) + continue; + + // Now Z + delta = center.z - (pEdict->v.absmin.z + pEdict->v.absmax.z)*0.5; + delta *= delta; + + distance += delta; + if ( distance > radiusSquared ) + continue; + + pEntity = CBaseEntity::Instance(pEdict); + if ( !pEntity ) + continue; + + pList[ count ] = pEntity; + count++; + + if ( count >= listMax ) + return count; + } + + + return count; +} + + +CBaseEntity *UTIL_FindEntityInSphere( CBaseEntity *pStartEntity, const Vector &vecCenter, float flRadius ) +{ + edict_t *pentEntity; + + if (pStartEntity) + pentEntity = pStartEntity->edict(); + else + pentEntity = NULL; + + pentEntity = FIND_ENTITY_IN_SPHERE( pentEntity, vecCenter, flRadius); + + if (!FNullEnt(pentEntity)) + return CBaseEntity::Instance(pentEntity); + return NULL; +} + +CBaseEntity *UTIL_FindPlayerInSphere( const Vector &vecCenter, float flRadius ) +{ + edict_t *pentEntity = NULL; + + pentEntity = FIND_ENTITY_IN_SPHERE( pentEntity, vecCenter, flRadius); + while(!FNullEnt(pentEntity)) + { + if(pentEntity->v.flags & FL_CLIENT) break; //found player + pentEntity = FIND_ENTITY_IN_SPHERE( pentEntity, vecCenter, flRadius); + } + if (!FNullEnt(pentEntity)) + return CBaseEntity::Instance(pentEntity); + return NULL; +} + +CBasePlayer *UTIL_FindPlayerInPVS( edict_t *pent ) +{ + edict_t *pentPlayer = FIND_CLIENT_IN_PVS( pent ); + CBasePlayer *pPlayer = NULL; + + if(!FNullEnt(pentPlayer)) //get pointer to player + pPlayer = GetClassPtr((CBasePlayer *)VARS(pentPlayer)); + return pPlayer; +} + +CBaseEntity *UTIL_FindEntityByString( CBaseEntity *pStartEntity, const char *szKeyword, const char *szValue ) +{ + edict_t *pentEntity; + CBaseEntity *pEntity; + + if (pStartEntity) + pentEntity = pStartEntity->edict(); + else + pentEntity = NULL; + + for (;;) + { + // Don't change this to use UTIL_FindEntityByString! + pentEntity = FIND_ENTITY_BY_STRING( pentEntity, szKeyword, szValue ); + + // if pentEntity (the edict) is null, we're at the end of the entities. Give up. + if (FNullEnt(pentEntity)) + { + return NULL; + } + else + { + // ...but if only pEntity (the classptr) is null, we've just got one dud, so we try again. + pEntity = CBaseEntity::Instance(pentEntity); + if (pEntity) + return pEntity; + } + } +} + +CBaseEntity *UTIL_FindEntityByClassname( CBaseEntity *pStartEntity, const char *szName ) +{ + return UTIL_FindEntityByString( pStartEntity, "classname", szName ); +} + +CBaseEntity *UTIL_FindEntityByTargetname( CBaseEntity *pStartEntity, const char *szName ) +{ + return UTIL_FindEntityByString( pStartEntity, "targetname", szName ); +} + +CBaseEntity *UTIL_FindEntityByTargetname( CBaseEntity *pStartEntity, const char *szName, CBaseEntity *pActivator ) +{ + return UTIL_FindEntityByTargetname( pStartEntity, szName ); +} + +CBaseEntity *UTIL_FindEntityByTarget( CBaseEntity *pStartEntity, const char *szName ) +{ + return UTIL_FindEntityByString( pStartEntity, "target", szName ); +} + +CBaseEntity *UTIL_FindEntityGeneric( const char *szWhatever, Vector &vecSrc, float flRadius ) +{ + CBaseEntity *pEntity = NULL; + + pEntity = UTIL_FindEntityByTargetname( NULL, szWhatever ); + if (pEntity) + return pEntity; + + CBaseEntity *pSearch = NULL; + float flMaxDist2 = flRadius * flRadius; + while ((pSearch = UTIL_FindEntityByClassname( pSearch, szWhatever )) != NULL) + { + float flDist2 = (pSearch->pev->origin - vecSrc).Length(); + flDist2 = flDist2 * flDist2; + if (flMaxDist2 > flDist2) + { + pEntity = pSearch; + flMaxDist2 = flDist2; + } + } + return pEntity; +} + + +// returns a CBaseEntity pointer to a player by index. Only returns if the player is spawned and connected +// otherwise returns NULL +// Index is 1 based +CBaseEntity *UTIL_PlayerByIndex( int playerIndex ) +{ + CBaseEntity *pPlayer = NULL; + + if ( playerIndex > 0 && playerIndex <= gpGlobals->maxClients ) + { + edict_t *pPlayerEdict = INDEXENT( playerIndex ); + if ( pPlayerEdict && !pPlayerEdict->free ) + { + pPlayer = CBaseEntity::Instance( pPlayerEdict ); + } + } + + return pPlayer; +} + + +void UTIL_MakeVectors( const Vector &vecAngles ) +{ + MAKE_VECTORS( vecAngles ); +} + + +void UTIL_MakeAimVectors( const Vector &vecAngles ) +{ + float rgflVec[3]; + vecAngles.CopyToArray(rgflVec); + rgflVec[0] = -rgflVec[0]; + MAKE_VECTORS(rgflVec); +} + + +#define SWAP(a,b,temp) ((temp)=(a),(a)=(b),(b)=(temp)) + +void UTIL_MakeInvVectors( const Vector &vec, globalvars_t *pgv ) +{ + MAKE_VECTORS(vec); + + float tmp; + pgv->v_right = pgv->v_right * -1; + + SWAP(pgv->v_forward.y, pgv->v_right.x, tmp); + SWAP(pgv->v_forward.z, pgv->v_up.x, tmp); + SWAP(pgv->v_right.z, pgv->v_up.y, tmp); +} + + +void UTIL_EmitAmbientSound( edict_t *entity, const Vector &vecOrigin, const char *samp, float vol, float attenuation, int fFlags, int pitch ) +{ + float rgfl[3]; + vecOrigin.CopyToArray(rgfl); + + if (samp && *samp == '!') + { + char name[32]; + if (SENTENCEG_Lookup(samp, name) >= 0) + EMIT_AMBIENT_SOUND(entity, rgfl, name, vol, attenuation, fFlags, pitch); + } + else + EMIT_AMBIENT_SOUND(entity, rgfl, samp, vol, attenuation, fFlags, pitch); +} + +// Shake the screen of all clients within radius +// radius == 0, shake all clients +// UNDONE: Allow caller to shake clients not ONGROUND? +// UNDONE: Fix falloff model (disabled)? +// UNDONE: Affect user controls? +//LRC UNDONE: Work during trigger_camera? +//----------------------------------------------------------------------------- +// Compute shake amplitude +//----------------------------------------------------------------------------- +float ComputeShakeAmplitude( const Vector ¢er, const Vector &shakePt, float amplitude, float radius ) +{ + if( radius <= 0 ) + return amplitude; + + float localAmplitude = -1; + Vector delta = center - shakePt; + float distance = delta.Length(); + + if( distance <= radius ) + { + // make the amplitude fall off over distance + float flPerc = 1.0 - (distance / radius); + localAmplitude = amplitude * flPerc; + } + + return localAmplitude; +} + +void UTIL_ScreenShake( const Vector ¢er, float amplitude, float frequency, float duration, float radius, ShakeCommand_t eCommand, BOOL bAirShake ) +{ + int i; + float localAmplitude; + + for( i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *pPlayer = UTIL_PlayerByIndex( i ); + + // + // Only shake players that are on the ground. + // + if( !pPlayer || (!bAirShake && !FBitSet( pPlayer->pev->flags, FL_ONGROUND ))) + { + continue; + } + + localAmplitude = ComputeShakeAmplitude( center, pPlayer->pev->origin, amplitude, radius ); + + // This happens if the player is outside the radius, in which case we should ignore + // all commands + if( localAmplitude < 0 ) continue; + + if (( localAmplitude > 0 ) || ( eCommand == SHAKE_STOP )) + { + if ( eCommand == SHAKE_STOP ) localAmplitude = 0; + + MESSAGE_BEGIN( MSG_ONE, gmsgShake, NULL, pPlayer->edict() ); + WRITE_BYTE( eCommand ); // shake command (SHAKE_START, STOP, FREQUENCY, AMPLITUDE) + WRITE_FLOAT( localAmplitude );// shake magnitude/amplitude + WRITE_FLOAT( frequency ); // shake noise frequency + WRITE_FLOAT( duration ); // shake lasts this long + MESSAGE_END(); + } + } +} + +void UTIL_ScreenShake( const Vector ¢er, float amplitude, float frequency, float duration, float radius ) +{ + UTIL_ScreenShake( center, amplitude, frequency, duration, radius, SHAKE_START, FALSE ); +} + +void UTIL_ScreenShakeAll( const Vector ¢er, float amplitude, float frequency, float duration ) +{ + UTIL_ScreenShake( center, amplitude, frequency, duration, 0, SHAKE_START, FALSE ); +} + +void UTIL_ScreenFadeAll( const Vector &color, float fadeTime, float fadeHold, int alpha, int flags ) +{ + if(IsMultiplayer()) + { + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + UTIL_ScreenFade( color, fadeTime, fadeHold, alpha, flags, i ); + } + else UTIL_ScreenFade( color, fadeTime, fadeHold, alpha, flags ); +} + + +void UTIL_ScreenFade( const Vector &color, float fadeTime, float fadeHold, int alpha, int flags, int playernum ) +{ + CBasePlayer *pPlayer = (CBasePlayer*)UTIL_PlayerByIndex( playernum ); + if ( pPlayer ) + { + if(flags & FFADE_CUSTOMVIEW) + { + if(pPlayer->viewFlags == 0) return; + ClearBits( flags, FFADE_CUSTOMVIEW ); //don't send this flag to engine!!! + } + if(flags & FFADE_OUT && fadeHold == 0) SetBits( flags, FFADE_STAYOUT ); + else ClearBits( flags, FFADE_STAYOUT ); + + pPlayer->m_FadeColor = color; + pPlayer->m_FadeAlpha = alpha; + pPlayer->m_iFadeFlags = flags; + pPlayer->m_iFadeTime = fadeTime; + pPlayer->m_iFadeHold = fadeHold; + pPlayer->fadeNeedsUpdate = TRUE; + } +} + + +void UTIL_HudMessage( CBaseEntity *pEntity, const hudtextparms_t &textparms, const char *pMessage ) +{ + if ( !pEntity || !pEntity->IsNetClient() ) + return; + + MESSAGE_BEGIN( MSG_ONE, SVC_TEMPENTITY, NULL, pEntity->edict() ); + WRITE_BYTE( TE_TEXTMESSAGE ); + WRITE_BYTE( textparms.channel & 0xFF ); + + WRITE_SHORT( FixedSigned16( textparms.x, 1<<13 ) ); + WRITE_SHORT( FixedSigned16( textparms.y, 1<<13 ) ); + WRITE_BYTE( textparms.effect ); + + WRITE_BYTE( textparms.r1 ); + WRITE_BYTE( textparms.g1 ); + WRITE_BYTE( textparms.b1 ); + WRITE_BYTE( textparms.a1 ); + + WRITE_BYTE( textparms.r2 ); + WRITE_BYTE( textparms.g2 ); + WRITE_BYTE( textparms.b2 ); + WRITE_BYTE( textparms.a2 ); + + WRITE_SHORT( FixedUnsigned16( textparms.fadeinTime, 1<<8 ) ); + WRITE_SHORT( FixedUnsigned16( textparms.fadeoutTime, 1<<8 ) ); + WRITE_SHORT( FixedUnsigned16( textparms.holdTime, 1<<8 ) ); + + if ( textparms.effect == 2 ) + WRITE_SHORT( FixedUnsigned16( textparms.fxTime, 1<<8 ) ); + + if ( strlen( pMessage ) < 512 ) + { + WRITE_STRING( pMessage ); + } + else + { + char tmp[512]; + strncpy( tmp, pMessage, 511 ); + tmp[511] = 0; + WRITE_STRING( tmp ); + } + MESSAGE_END(); +} + +void UTIL_HudMessageAll( const hudtextparms_t &textparms, const char *pMessage ) +{ + int i; + + for ( i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *pPlayer = UTIL_PlayerByIndex( i ); + if ( pPlayer ) + UTIL_HudMessage( pPlayer, textparms, pMessage ); + } +} + + +extern int gmsgTextMsg, gmsgSayText; +void UTIL_ClientPrintAll( int msg_dest, const char *msg_name, const char *param1, const char *param2, const char *param3, const char *param4 ) +{ + MESSAGE_BEGIN( MSG_ALL, gmsgTextMsg ); + WRITE_BYTE( msg_dest ); + WRITE_STRING( msg_name ); + + if ( param1 ) + WRITE_STRING( param1 ); + if ( param2 ) + WRITE_STRING( param2 ); + if ( param3 ) + WRITE_STRING( param3 ); + if ( param4 ) + WRITE_STRING( param4 ); + + MESSAGE_END(); +} + +void ClientPrint( entvars_t *client, int msg_dest, const char *msg_name, const char *param1, const char *param2, const char *param3, const char *param4 ) +{ + MESSAGE_BEGIN( MSG_ONE, gmsgTextMsg, NULL, client ); + WRITE_BYTE( msg_dest ); + WRITE_STRING( msg_name ); + + if ( param1 ) + WRITE_STRING( param1 ); + if ( param2 ) + WRITE_STRING( param2 ); + if ( param3 ) + WRITE_STRING( param3 ); + if ( param4 ) + WRITE_STRING( param4 ); + + MESSAGE_END(); +} + +void UTIL_SayText( const char *pText, CBaseEntity *pEntity ) +{ + if ( !pEntity->IsNetClient() ) + return; + + MESSAGE_BEGIN( MSG_ONE, gmsgSayText, NULL, pEntity->edict() ); + WRITE_BYTE( pEntity->entindex() ); + WRITE_STRING( pText ); + MESSAGE_END(); +} + +void UTIL_SayTextAll( const char *pText, CBaseEntity *pEntity ) +{ + MESSAGE_BEGIN( MSG_ALL, gmsgSayText, NULL ); + WRITE_BYTE( pEntity->entindex() ); + WRITE_STRING( pText ); + MESSAGE_END(); +} + + +char *UTIL_dtos1( int d ) +{ + static char buf[8]; + sprintf( buf, "%d", d ); + return buf; +} + +char *UTIL_dtos2( int d ) +{ + static char buf[8]; + sprintf( buf, "%d", d ); + return buf; +} + +char *UTIL_dtos3( int d ) +{ + static char buf[8]; + sprintf( buf, "%d", d ); + return buf; +} + +char *UTIL_dtos4( int d ) +{ + static char buf[8]; + sprintf( buf, "%d", d ); + return buf; +} + +void UTIL_ShowMessage( const char *pString, CBaseEntity *pEntity ) +{ + if ( !pEntity || !pEntity->IsNetClient() ) + return; + + MESSAGE_BEGIN( MSG_ONE, gmsgHudText, NULL, pEntity->edict() ); + WRITE_STRING( pString ); + MESSAGE_END(); +} + + +void UTIL_ShowMessageAll( const char *pString ) +{ + // loop through all players + + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *pPlayer = UTIL_PlayerByIndex( i ); + if ( pPlayer ) UTIL_ShowMessage( pString, pPlayer ); + } +} +void UTIL_SetFogAll( Vector color, int iFadeTime, int iStartDist, int iEndDist ) +{ + // loop through all players + + if(IsMultiplayer()) + { + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + UTIL_SetFog( color, iFadeTime, iStartDist, iEndDist, i ); + } + else UTIL_SetFog( color, iFadeTime, iStartDist, iEndDist ); +} + +void UTIL_SetFog( Vector color, int iFadeTime, int iStartDist, int iEndDist, int playernum ) +{ + CBasePlayer *pPlayer = (CBasePlayer*)UTIL_PlayerByIndex( playernum ); + if ( pPlayer ) + { + if(pPlayer->m_FogFadeTime != 0) return;//fading in progress !!!TODO: make smooth re-fading + if(IsMultiplayer()) iFadeTime = 0; //disable fading in multiplayer + if( iFadeTime > 0 ) + { + pPlayer->m_iFogEndDist = FOG_LIMIT; + pPlayer->m_iFogFinalEndDist = iEndDist; + } + else if( iFadeTime < 0 ) + { + pPlayer->m_iFogEndDist = iEndDist; + pPlayer->m_iFogFinalEndDist = iEndDist; + } + else pPlayer->m_iFogEndDist = iEndDist; + pPlayer->m_iFogStartDist = iStartDist; + pPlayer->m_FogColor = color; + pPlayer->m_FogFadeTime = iFadeTime; + pPlayer->m_flStartTime = gpGlobals->time; + pPlayer->fogNeedsUpdate = TRUE; + } +} + +// Overloaded to add IGNORE_GLASS +void UTIL_TraceLine( const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, IGNORE_GLASS ignoreGlass, edict_t *pentIgnore, TraceResult *ptr ) +{ + TRACE_LINE( vecStart, vecEnd, (igmon == ignore_monsters ? TRUE : FALSE) | (ignoreGlass?0x100:0), pentIgnore, ptr ); +} + + +void UTIL_TraceLine( const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, edict_t *pentIgnore, TraceResult *ptr ) +{ + TRACE_LINE( vecStart, vecEnd, (igmon == ignore_monsters ? TRUE : FALSE), pentIgnore, ptr ); +} + + +void UTIL_TraceHull( const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, int hullNumber, edict_t *pentIgnore, TraceResult *ptr ) +{ + TRACE_HULL( vecStart, vecEnd, (igmon == ignore_monsters ? TRUE : FALSE), hullNumber, pentIgnore, ptr ); +} + +void UTIL_TraceModel( const Vector &vecStart, const Vector &vecEnd, int hullNumber, edict_t *pentModel, TraceResult *ptr ) +{ + g_engfuncs.pfnTraceModel( vecStart, vecEnd, pentModel, ptr ); +} + + +TraceResult UTIL_GetGlobalTrace( void ) +{ + TraceResult tr; + + tr.fAllSolid = gpGlobals->trace_allsolid; + tr.fStartSolid = gpGlobals->trace_startsolid; + tr.fStartStuck = gpGlobals->trace_startstuck; + tr.iContents = gpGlobals->trace_contents; + tr.iStartContents = gpGlobals->trace_start_contents; + tr.flFraction = gpGlobals->trace_fraction; + tr.flPlaneDist = gpGlobals->trace_plane_dist; + tr.pHit = gpGlobals->trace_ent; + tr.vecEndPos = gpGlobals->trace_endpos; + tr.vecPlaneNormal = gpGlobals->trace_plane_normal; + tr.pTexName = gpGlobals->trace_texture; + tr.iHitgroup = gpGlobals->trace_hitgroup; + return tr; +} + + +void UTIL_SetSize( entvars_t *pev, const Vector &vecMin, const Vector &vecMax ) +{ + SET_SIZE( ENT(pev), vecMin, vecMax ); +} + + +float UTIL_VecToYaw( const Vector &vec ) +{ + return VEC_TO_YAW(vec); +} + +void UTIL_SetEdictOrigin( edict_t *pEdict, const Vector &vecOrigin ) +{ + SET_ORIGIN(pEdict, vecOrigin ); +} + +// 'links' the entity into the world + + +void UTIL_ParticleEffect( const Vector &vecOrigin, const Vector &vecDirection, ULONG ulColor, ULONG ulCount ) +{ + PARTICLE_EFFECT( vecOrigin, vecDirection, (float)ulColor, (float)ulCount ); +} + + +float UTIL_SplineFraction( float value, float scale ) +{ + value = scale * value; + float valueSquared = value * value; + + // Nice little ease-in, ease-out spline-like curve + return 3 * valueSquared - 2 * valueSquared * value; +} + + +char* UTIL_VarArgs( char *format, ... ) +{ + va_list argptr; + static char string[1024]; + + va_start (argptr, format); + vsprintf (string, format,argptr); + va_end (argptr); + + return string; +} + +Vector UTIL_GetAimVector( edict_t *pent, float flSpeed ) +{ + Vector tmp; + GET_AIM_VECTOR(pent, flSpeed, tmp); + return tmp; +} + +BOOL UTIL_IsMasterTriggered(string_t iszMaster, CBaseEntity *pActivator) +{ + int i, j, found = false; + const char *szMaster; + char szBuf[80]; + CBaseEntity *pMaster; + int reverse = false; + + + if (iszMaster) + { + //Msg( "IsMasterTriggered(%s, %s \"%s\")\n", STRING(iszMaster), STRING(pActivator->pev->classname), STRING(pActivator->pev->targetname)); + szMaster = STRING(iszMaster); + if (szMaster[0] == '~') //inverse master + { + reverse = true; + szMaster++; + } + + pMaster = UTIL_FindEntityByTargetname( NULL, szMaster ); + if ( !pMaster ) + { + for (i = 0; szMaster[i]; i++) + { + if (szMaster[i] == '(') + { + for (j = i+1; szMaster[j]; j++) + { + if (szMaster[j] == ')') + { + strncpy(szBuf, szMaster+i+1, (j-i)-1); + szBuf[(j-i)-1] = 0; + pActivator = UTIL_FindEntityByTargetname( NULL, szBuf ); + found = true; + break; + } + } + if (!found) // no ) found + { + ALERT(at_error, "Missing ')' in master \"%s\"\n", szMaster); + return FALSE; + } + break; + } + } + if (!found) // no ( found + { + ALERT(at_debug, "Master \"%s\" not found!\n",szMaster); + return TRUE; + } + + strncpy(szBuf, szMaster, i); + szBuf[i] = 0; + pMaster = UTIL_FindEntityByTargetname( NULL, szBuf ); + } + + if (pMaster) + { + if (reverse) + return (pMaster->GetState( pActivator ) != STATE_ON); + else + return (pMaster->GetState( pActivator ) == STATE_ON); + } + } + + // if the entity has no master (or the master is missing), just say yes. + return TRUE; +} + +BOOL UTIL_ShouldShowBlood( int color ) +{ + if ( color != DONT_BLEED ) + { + if ( color == BLOOD_COLOR_RED ) + { + if ( CVAR_GET_FLOAT("violence_hblood") != 0 ) + return TRUE; + } + else + { + if ( CVAR_GET_FLOAT("violence_ablood") != 0 ) + return TRUE; + } + } + return FALSE; +} + +int UTIL_PointContents( const Vector &vec ) +{ + return POINT_CONTENTS( vec ); +} + +void UTIL_BloodStream( const Vector &origin, const Vector &direction, int color, int amount ) +{ + if ( !UTIL_ShouldShowBlood( color ) ) + return; + + + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, origin ); + WRITE_BYTE( TE_BLOODSTREAM ); + WRITE_COORD( origin.x ); + WRITE_COORD( origin.y ); + WRITE_COORD( origin.z ); + WRITE_COORD( direction.x ); + WRITE_COORD( direction.y ); + WRITE_COORD( direction.z ); + WRITE_BYTE( color ); + WRITE_BYTE( min( amount, 255 ) ); + MESSAGE_END(); +} + +void UTIL_BloodDrips( const Vector &origin, const Vector &direction, int color, int amount ) +{ + if ( !UTIL_ShouldShowBlood( color ) ) + return; + + if ( color == DONT_BLEED || amount == 0 ) + return; + + if ( g_Language == LANGUAGE_GERMAN && color == BLOOD_COLOR_RED ) + color = 0; + + if ( g_pGameRules->IsMultiplayer() ) + { + // scale up blood effect in multiplayer for better visibility + amount *= 2; + } + + if ( amount > 255 ) + amount = 255; + + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, origin ); + WRITE_BYTE( TE_BLOODSPRITE ); + WRITE_COORD( origin.x); // pos + WRITE_COORD( origin.y); + WRITE_COORD( origin.z); + WRITE_SHORT( g_sModelIndexBloodSpray ); // initial sprite model + WRITE_SHORT( g_sModelIndexBloodDrop ); // droplet sprite models + WRITE_BYTE( color ); // color index into host_basepal + WRITE_BYTE( min( max( 3, amount / 10 ), 16 ) ); // size + MESSAGE_END(); +} + +Vector UTIL_RandomBloodVector( void ) +{ + Vector direction; + + direction.x = RANDOM_FLOAT ( -1, 1 ); + direction.y = RANDOM_FLOAT ( -1, 1 ); + direction.z = RANDOM_FLOAT ( 0, 1 ); + + return direction; +} + + +void UTIL_BloodDecalTrace( TraceResult *pTrace, int bloodColor ) +{ + if ( UTIL_ShouldShowBlood( bloodColor ) ) + { + if ( bloodColor == BLOOD_COLOR_RED ) + UTIL_DecalTrace( pTrace, DECAL_BLOOD1 + RANDOM_LONG(0,5) ); + else + UTIL_DecalTrace( pTrace, DECAL_YBLOOD1 + RANDOM_LONG(0,5) ); + } +} + + +void UTIL_DecalTrace( TraceResult *pTrace, int decalNumber ) +{ + short entityIndex; + int index; + int message; + + if ( decalNumber < 0 ) + return; + + index = gDecals[ decalNumber ].index; + + if ( index < 0 ) + return; + + if (pTrace->flFraction == 1.0) + return; + + // Only decal BSP models + if ( pTrace->pHit ) + { + CBaseEntity *pEntity = CBaseEntity::Instance( pTrace->pHit ); + if ( pEntity && !pEntity->IsBSPModel() ) + return; + entityIndex = ENTINDEX( pTrace->pHit ); + } + else + entityIndex = 0; + + message = TE_DECAL; + if ( entityIndex != 0 ) + { + if ( index > 255 ) + { + message = TE_DECALHIGH; + index -= 256; + } + } + else + { + message = TE_WORLDDECAL; + if ( index > 255 ) + { + message = TE_WORLDDECALHIGH; + index -= 256; + } + } + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( message ); + WRITE_COORD( pTrace->vecEndPos.x ); + WRITE_COORD( pTrace->vecEndPos.y ); + WRITE_COORD( pTrace->vecEndPos.z ); + WRITE_BYTE( index ); + if ( entityIndex ) + WRITE_SHORT( entityIndex ); + MESSAGE_END(); +} + +/* +============== +UTIL_PlayerDecalTrace + +A player is trying to apply his custom decal for the spray can. +Tell connected clients to display it, or use the default spray can decal +if the custom can't be loaded. +============== +*/ +void UTIL_PlayerDecalTrace( TraceResult *pTrace, int playernum, int decalNumber, BOOL bIsCustom ) +{ + int index; + + if (!bIsCustom) + { + if ( decalNumber < 0 ) + return; + + index = gDecals[ decalNumber ].index; + if ( index < 0 ) + return; + } + else + index = decalNumber; + + if (pTrace->flFraction == 1.0) + return; + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_PLAYERDECAL ); + WRITE_BYTE ( playernum ); + WRITE_COORD( pTrace->vecEndPos.x ); + WRITE_COORD( pTrace->vecEndPos.y ); + WRITE_COORD( pTrace->vecEndPos.z ); + WRITE_SHORT( (short)ENTINDEX(pTrace->pHit) ); + WRITE_BYTE( index ); + MESSAGE_END(); +} + +void UTIL_GunshotDecalTrace( TraceResult *pTrace, int decalNumber ) +{ + if ( decalNumber < 0 ) + return; + + int index = gDecals[ decalNumber ].index; + if ( index < 0 ) + return; + + if (pTrace->flFraction == 1.0) + return; + + MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, pTrace->vecEndPos ); + WRITE_BYTE( TE_GUNSHOTDECAL ); + WRITE_COORD( pTrace->vecEndPos.x ); + WRITE_COORD( pTrace->vecEndPos.y ); + WRITE_COORD( pTrace->vecEndPos.z ); + WRITE_SHORT( (short)ENTINDEX(pTrace->pHit) ); + WRITE_BYTE( index ); + MESSAGE_END(); +} + + +void UTIL_Sparks( const Vector &position ) +{ + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, position ); + WRITE_BYTE( TE_SPARKS ); + WRITE_COORD( position.x ); + WRITE_COORD( position.y ); + WRITE_COORD( position.z ); + MESSAGE_END(); +} + +void UTIL_Explode( const Vector ¢er, edict_t *pOwner, int radius, int name ) +{ + CBaseEntity *pExplosion = CBaseEntity::Create( "env_explosion", center, g_vecZero, pOwner ); + if(pExplosion) + { + if(name) pExplosion->pev->classname = name; + pExplosion->pev->dmg = radius; + pExplosion->pev->owner = pOwner; + pExplosion->pev->spawnflags |= SF_FIREONCE; //remove entity after explode + pExplosion->Use( NULL, NULL, USE_ON, 0 ); + } +} + +void UTIL_Ricochet( const Vector &position, float scale ) +{ + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, position ); + WRITE_BYTE( TE_ARMOR_RICOCHET ); + WRITE_COORD( position.x ); + WRITE_COORD( position.y ); + WRITE_COORD( position.z ); + WRITE_BYTE( (int)(scale*10) ); + MESSAGE_END(); +} + + +BOOL UTIL_TeamsMatch( const char *pTeamName1, const char *pTeamName2 ) +{ + // Everyone matches unless it's teamplay + if ( !g_pGameRules->IsTeamplay() ) + return TRUE; + + // Both on a team? + if ( *pTeamName1 != 0 && *pTeamName2 != 0 ) + { + if ( !stricmp( pTeamName1, pTeamName2 ) ) // Same Team? + return TRUE; + } + + return FALSE; +} + +//LRC - moved here from barney.cpp +BOOL UTIL_IsFacing( entvars_t *pevTest, const Vector &reference ) +{ + Vector vecDir = (reference - pevTest->origin); + vecDir.z = 0; + vecDir = vecDir.Normalize(); + Vector forward, angle; + angle = pevTest->v_angle; + angle.x = 0; + UTIL_MakeVectorsPrivate( angle, forward, NULL, NULL ); + // He's facing me, he meant it + if ( DotProduct( forward, vecDir ) > 0.96 ) // +/- 15 degrees or so + { + return TRUE; + } + return FALSE; +} + +void UTIL_StringToVector( float *pVector, const char *pString ) +{ + char *pstr, *pfront, tempString[128]; + int j; + + strcpy( tempString, pString ); + pstr = pfront = tempString; + + for ( j = 0; j < 3; j++ ) // lifted from pr_edict.c + { + pVector[j] = atof( pfront ); + + while ( *pstr && *pstr != ' ' ) + pstr++; + if (!*pstr) + break; + pstr++; + pfront = pstr; + } + if (j < 2) + { + /* + ALERT( at_error, "Bad field in entity!! %s:%s == \"%s\"\n", + pkvd->szClassName, pkvd->szKeyName, pkvd->szValue ); + */ + for (j = j+1;j < 3; j++) + pVector[j] = 0; + } +} + + +//LRC - randomized vectors of the form "0 0 0 .. 1 0 0" +void UTIL_StringToRandomVector( float *pVector, const char *pString ) +{ + char *pstr, *pfront, tempString[128]; + int j; + float pAltVec[3]; + + strcpy( tempString, pString ); + pstr = pfront = tempString; + + for ( j = 0; j < 3; j++ ) // lifted from pr_edict.c + { + pVector[j] = atof( pfront ); + + while ( *pstr && *pstr != ' ' ) pstr++; + if (!*pstr) break; + pstr++; + pfront = pstr; + } + if (j < 2) + { + /* + ALERT( at_error, "Bad field in entity!! %s:%s == \"%s\"\n", + pkvd->szClassName, pkvd->szKeyName, pkvd->szValue ); + */ + for (j = j+1;j < 3; j++) + pVector[j] = 0; + } + else if (*pstr == '.') + { + pstr++; + if (*pstr != '.') return; + pstr++; + if (*pstr != ' ') return; + + UTIL_StringToVector(pAltVec, pstr); + + pVector[0] = RANDOM_FLOAT( pVector[0], pAltVec[0] ); + pVector[1] = RANDOM_FLOAT( pVector[1], pAltVec[1] ); + pVector[2] = RANDOM_FLOAT( pVector[2], pAltVec[2] ); + } +} + + +void UTIL_StringToIntArray( int *pVector, int count, const char *pString ) +{ + char *pstr, *pfront, tempString[128]; + int j; + + strcpy( tempString, pString ); + pstr = pfront = tempString; + + for ( j = 0; j < count; j++ ) // lifted from pr_edict.c + { + pVector[j] = atoi( pfront ); + + while ( *pstr && *pstr != ' ' ) + pstr++; + if (!*pstr) + break; + pstr++; + pfront = pstr; + } + + for ( j++; j < count; j++ ) + { + pVector[j] = 0; + } +} + +Vector UTIL_ClampVectorToBox( const Vector &input, const Vector &clampSize ) +{ + Vector sourceVector = input; + + if ( sourceVector.x > clampSize.x ) + sourceVector.x -= clampSize.x; + else if ( sourceVector.x < -clampSize.x ) + sourceVector.x += clampSize.x; + else + sourceVector.x = 0; + + if ( sourceVector.y > clampSize.y ) + sourceVector.y -= clampSize.y; + else if ( sourceVector.y < -clampSize.y ) + sourceVector.y += clampSize.y; + else + sourceVector.y = 0; + + if ( sourceVector.z > clampSize.z ) + sourceVector.z -= clampSize.z; + else if ( sourceVector.z < -clampSize.z ) + sourceVector.z += clampSize.z; + else + sourceVector.z = 0; + + return sourceVector.Normalize(); +} + + +float UTIL_WaterLevel( const Vector &position, float minz, float maxz ) +{ + Vector midUp = position; + midUp.z = minz; + + if(!( UTIL_PointContents( midUp ) & MASK_WATER )) + return minz; + + midUp.z = maxz; + if( UTIL_PointContents( midUp ) & MASK_WATER ) + return maxz; + + float diff = maxz - minz; + while( diff > 1.0 ) + { + midUp.z = minz + diff/2.0; + if( UTIL_PointContents( midUp ) & MASK_WATER ) + { + minz = midUp.z; + } + else + { + maxz = midUp.z; + } + diff = maxz - minz; + } + return midUp.z; +} + +void UTIL_Bubbles( Vector mins, Vector maxs, int count ) +{ + Vector mid = (mins + maxs) * 0.5; + + float flHeight = UTIL_WaterLevel( mid, mid.z, mid.z + 1024 ); + flHeight = flHeight - mins.z; + + MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, mid ); + WRITE_BYTE( TE_BUBBLES ); + WRITE_COORD( mins.x ); // mins + WRITE_COORD( mins.y ); + WRITE_COORD( mins.z ); + WRITE_COORD( maxs.x ); // maxz + WRITE_COORD( maxs.y ); + WRITE_COORD( maxs.z ); + WRITE_COORD( flHeight ); // height + WRITE_SHORT( g_sModelIndexBubbles ); + WRITE_BYTE( count ); // count + WRITE_COORD( 8 ); // speed + MESSAGE_END(); +} + +void UTIL_BubbleTrail( Vector from, Vector to, int count ) +{ + float flHeight = UTIL_WaterLevel( from, from.z, from.z + 256 ); + flHeight = flHeight - from.z; + + if (flHeight < 8) + { + flHeight = UTIL_WaterLevel( to, to.z, to.z + 256 ); + flHeight = flHeight - to.z; + if (flHeight < 8) + return; + + // UNDONE: do a ploink sound + flHeight = flHeight + to.z - from.z; + } + + if (count > 255) + count = 255; + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BUBBLETRAIL ); + WRITE_COORD( from.x ); // mins + WRITE_COORD( from.y ); + WRITE_COORD( from.z ); + WRITE_COORD( to.x ); // maxz + WRITE_COORD( to.y ); + WRITE_COORD( to.z ); + WRITE_COORD( flHeight ); // height + WRITE_SHORT( g_sModelIndexBubbles ); + WRITE_BYTE( count ); // count + WRITE_COORD( 8 ); // speed + MESSAGE_END(); +} + +BOOL UTIL_IsValidEntity( edict_t *pent ) +{ + if ( !pent || pent->free || (pent->v.flags & FL_KILLME) ) + return FALSE; + return TRUE; +} + +//========================================================= +// UTIL_LogPrintf - Prints a logged message to console. +// Preceded by LOG: ( timestamp ) < message > +//========================================================= +void UTIL_LogPrintf( char *fmt, ... ) +{ + va_list argptr; + static char string[1024]; + + va_start ( argptr, fmt ); + vsprintf ( string, fmt, argptr ); + va_end ( argptr ); + + // Print to server console + ALERT( at_logged, "%s", string ); +} + +//========================================================= +// UTIL_DotPoints - returns the dot product of a line from +// src to check and vecdir. +//========================================================= +float UTIL_DotPoints ( const Vector &vecSrc, const Vector &vecCheck, const Vector &vecDir ) +{ + Vector2D vec2LOS; + + vec2LOS = ( vecCheck - vecSrc ).Make2D(); + vec2LOS = vec2LOS.Normalize(); + + return DotProduct (vec2LOS , ( vecDir.Make2D() ) ); +} + +//for trigger_viewset +int HaveCamerasInPVS( edict_t* edict ) +{ + CBaseEntity *pViewEnt = NULL; + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *pEntity = UTIL_PlayerByIndex( i ); + if (!pEntity) continue; + CBasePlayer *pPlayer = (CBasePlayer *)pEntity; + if (pPlayer && pPlayer->viewFlags & 1) // custom view active + { + pViewEnt = pPlayer->pViewEnt; + if (!pViewEnt->edict())return 0; + + edict_t *view = pViewEnt->edict(); + edict_t *pent = UTIL_EntitiesInPVS( edict ); + + while ( !FNullEnt( pent ) ) + { + if (pent == view)return TRUE; + pent = pent->v.chain; + } + } + } + return 0; +} +void UTIL_SetView( int ViewEntity, int flags ) +{ + //Light version SetView + //Please don't use this in multiplayer + CBaseEntity *m_pPlayer = UTIL_PlayerByIndex( 1 ); + UTIL_SetView( m_pPlayer, ViewEntity, flags ); +} + +void UTIL_SetView( CBaseEntity *pActivator, int ViewEntity, int flags ) +{ + CBaseEntity *pViewEnt = 0; + + if(ViewEntity) + { + //try to find by targetname + pViewEnt = UTIL_FindEntityByString( NULL, "targetname", STRING(ViewEntity)); + //try to find by classname + if(FNullEnt(pViewEnt)) + pViewEnt = UTIL_FindEntityByString( NULL, "classname", STRING(ViewEntity)); + if(pViewEnt && pViewEnt->pev->flags & FL_MONSTER) flags |= MONSTER_VIEW;//detect monster view + } + UTIL_SetView( pActivator, pViewEnt, flags ); +} + +void UTIL_SetView( CBaseEntity *pActivator, CBaseEntity *pViewEnt, int flags ) +{ + ((CBasePlayer *)pActivator)->pViewEnt = pViewEnt; + ((CBasePlayer *)pActivator)->viewFlags = flags; + ((CBasePlayer *)pActivator)->viewNeedsUpdate = 1; +} \ No newline at end of file diff --git a/server/global/utils.h b/server/global/utils.h new file mode 100644 index 00000000..3ea7b23a --- /dev/null +++ b/server/global/utils.h @@ -0,0 +1,765 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// utils.h - Utility code. +// Really not optional after all. +//======================================================================= + +#ifndef UTIL_H +#define UTIL_H + +#include + +#include "te_temp.h" +#include "shake.h" + +#ifndef ACTIVITY_H +#include "activity.h" +#endif + +#ifndef ENGINECALLBACK_H +#include "enginecallback.h" +#endif + +#define MSG_ONE_UNRELIABLE 0 // Send to one client, but don't put in reliable stream, put in unreliable datagram ( could be dropped ) +#define MSG_BROADCAST 1 // unreliable to all +#define MSG_PAS 2 // Ents in PAS of org +#define MSG_PVS 3 // Ents in PVS of org +#define MSG_ONE 4 // reliable to one (msg_entity) +#define MSG_ALL 5 // reliable to all +#define MSG_PAS_R 6 // Reliable to PAS +#define MSG_PVS_R 7 // Reliable to PVS + +inline void MESSAGE_BEGIN( int msg_dest, int msg_type, const float *pOrigin, entvars_t *ent ); // implementation later in this file + +extern globalvars_t *gpGlobals; + +#define MAX_CHILDS 100 + + +inline edict_t *FIND_ENTITY_BY_CLASSNAME(edict_t *entStart, const char *pszName) +{ + return FIND_ENTITY_BY_STRING(entStart, "classname", pszName); +} + +inline edict_t *FIND_ENTITY_BY_TARGETNAME(edict_t *entStart, const char *pszName) +{ + return FIND_ENTITY_BY_STRING(entStart, "targetname", pszName); +} + +// for doing a reverse lookup. Say you have a door, and want to find its button. +inline edict_t *FIND_ENTITY_BY_TARGET(edict_t *entStart, const char *pszName) +{ + return FIND_ENTITY_BY_STRING(entStart, "target", pszName); +} + +// Keeps clutter down a bit, when writing key-value pairs +#define WRITEKEY_INT(pf, szKeyName, iKeyValue) ENGINE_FPRINTF(pf, "\"%s\" \"%d\"\n", szKeyName, iKeyValue) +#define WRITEKEY_FLOAT(pf, szKeyName, flKeyValue) \ + ENGINE_FPRINTF(pf, "\"%s\" \"%f\"\n", szKeyName, flKeyValue) +#define WRITEKEY_STRING(pf, szKeyName, szKeyValue) \ + ENGINE_FPRINTF(pf, "\"%s\" \"%s\"\n", szKeyName, szKeyValue) +#define WRITEKEY_VECTOR(pf, szKeyName, flX, flY, flZ) \ + ENGINE_FPRINTF(pf, "\"%s\" \"%f %f %f\"\n", szKeyName, flX, flY, flZ) + +// Keeps clutter down a bit, when using a float as a bit-vector +#define SetBits(flBitVector, bits) ((flBitVector) = (int)(flBitVector) | (bits)) +#define ClearBits(flBitVector, bits) ((flBitVector) = (int)(flBitVector) & ~(bits)) +#define FBitSet(flBitVector, bit) ((int)(flBitVector) & (bit)) + +// Makes these more explicit, and easier to find +#define FILE_GLOBAL static +#define DLL_GLOBAL + +extern DLL_GLOBAL int g_sModelIndexErrorModel; +extern DLL_GLOBAL int g_sModelIndexErrorSprite; +extern DLL_GLOBAL int g_sModelIndexNullModel; +extern DLL_GLOBAL int g_sModelIndexNullSprite; + +// Until we figure out why "const" gives the compiler problems, we'll just have to use +// this bogus "empty" define to mark things as constant. +#define CONSTANT + +// More explicit than "int" +typedef int EOFFSET; + +// In case it's not alread defined +typedef int BOOL; + +// In case this ever changes +#define M_PI 3.14159265358979323846 + +// Keeps clutter down a bit, when declaring external entity/global method prototypes +#define DECLARE_GLOBAL_METHOD(MethodName) extern void DLLEXPORT MethodName( void ) +#define GLOBAL_METHOD(funcname) void DLLEXPORT funcname(void) + +// This is the glue that hooks .MAP entity class names to our CPP classes +// The _declspec forces them to be exported by name so we can do a lookup with GetProcAddress() +// The function is used to intialize / allocate the object for the entity +#ifdef _WIN32 +#define LINK_ENTITY_TO_CLASS(mapClassName,DLLClassName) \ + extern "C" _declspec( dllexport ) void mapClassName( entvars_t *pev ); \ + void mapClassName( entvars_t *pev ) { GetClassPtr( (DLLClassName *)pev ); } +#else +#define LINK_ENTITY_TO_CLASS(mapClassName,DLLClassName) extern "C" void mapClassName( entvars_t *pev ); void mapClassName( entvars_t *pev ) { GetClassPtr( (DLLClassName *)pev ); } +#endif + + +// +// Conversion among the three types of "entity", including identity-conversions. +// +#ifdef DEBUG + extern edict_t *DBG_EntOfVars(const entvars_t *pev); + inline edict_t *ENT(const entvars_t *pev) { return DBG_EntOfVars(pev); } +#else + inline edict_t *ENT(const entvars_t *pev) { return pev->pContainingEntity; } +#endif +inline edict_t *ENT(edict_t *pent) { return pent; } +inline edict_t *ENT(EOFFSET eoffset) { return (*g_engfuncs.pfnPEntityOfEntOffset)(eoffset); } +inline EOFFSET OFFSET(EOFFSET eoffset) { return eoffset; } +inline EOFFSET OFFSET(const edict_t *pent) +{ +#if _DEBUG + if ( !pent ) + ALERT( at_error, "Bad ent in OFFSET()\n" ); +#endif + return (*g_engfuncs.pfnEntOffsetOfPEntity)(pent); +} +inline EOFFSET OFFSET(entvars_t *pev) +{ +#if _DEBUG + if ( !pev ) + ALERT( at_error, "Bad pev in OFFSET()\n" ); +#endif + return OFFSET(ENT(pev)); +} +inline entvars_t *VARS(entvars_t *pev) { return pev; } + +inline entvars_t *VARS(edict_t *pent) +{ + if ( !pent ) + return NULL; + + return &pent->v; +} + +inline entvars_t* VARS(EOFFSET eoffset) { return VARS(ENT(eoffset)); } +inline int ENTINDEX(edict_t *pEdict) { return (*g_engfuncs.pfnIndexOfEdict)(pEdict); } +inline edict_t* INDEXENT( int iEdictNum ) { return (*g_engfuncs.pfnPEntityOfEntIndex)(iEdictNum); } +inline void MESSAGE_BEGIN( int msg_dest, int msg_type, const float *pOrigin, entvars_t *ent ) { + (*g_engfuncs.pfnMessageBegin)(msg_dest, msg_type, pOrigin, ENT(ent)); +} + + +class CBaseEntity; +class CBasePlayer; + +// Testing the three types of "entity" for nullity +//LRC- four types, rather; see cbase.h +#define eoNullEntity 0 +extern BOOL FNullEnt(EOFFSET eoffset); +extern BOOL FNullEnt(const edict_t* pent); +extern BOOL FNullEnt(entvars_t* pev); +extern BOOL FNullEnt( CBaseEntity *ent ); + +// Testing strings for nullity +#define iStringNull 0 +inline BOOL FStringNull(int iString) { return iString == iStringNull; } +inline BOOL FStringNull(Vector vString) { return vString == Vector(0,0,0); } + +#define cchMapNameMost 32 + +// Dot products for view cone checking +#define VIEW_FIELD_FULL (float)-1.0 // +-180 degrees +#define VIEW_FIELD_WIDE (float)-0.7 // +-135 degrees 0.1 // +-85 degrees, used for full FOV checks +#define VIEW_FIELD_NARROW (float)0.7 // +-45 degrees, more narrow check used to set up ranged attacks +#define VIEW_FIELD_ULTRA_NARROW (float)0.9 // +-25 degrees, more narrow check used to set up ranged attacks + +// All monsters need this data +#define DONT_BLEED -1 +#define BLOOD_COLOR_RED (BYTE)247 +#define BLOOD_COLOR_YELLOW (BYTE)195 +#define BLOOD_COLOR_GREEN BLOOD_COLOR_YELLOW + +typedef enum +{ + MONSTERSTATE_NONE = 0, + MONSTERSTATE_IDLE, + MONSTERSTATE_COMBAT, + MONSTERSTATE_ALERT, + MONSTERSTATE_HUNT, + MONSTERSTATE_PRONE, + MONSTERSTATE_SCRIPT, + MONSTERSTATE_PLAYDEAD, + MONSTERSTATE_DEAD + +} MONSTERSTATE; + +//LRC- the values used for the new "global states" mechanism. +typedef enum +{ + STATE_OFF = 0, // disabled, inactive, invisible, closed, or stateless. Or non-alert monster. + STATE_ON, // enabled, active, visisble, or open. Or alert monster. + STATE_TURN_ON, // door opening, env_fade fading in, etc. + STATE_TURN_OFF, // door closing, monster dying (?). + STATE_IN_USE, // player is in control (train/tank/barney/scientist). + STATE_DEAD, // entity dead +} STATE; + +typedef enum +{ + GLOBAL_OFF = 0, + GLOBAL_ON = 1, + GLOBAL_DEAD = 2 +} GLOBALESTATE; + + //list of use type +typedef enum +{ + USE_OFF = 0, //deactivate entity + USE_ON = 1, //activate entity + USE_SET = 2, //control over entity - e.g tracktrain, tank, camera etc. + USE_RESET = 3, //reset entity to initial position + USE_TOGGLE = 4, //default command - toggle on/off + USE_REMOVE = 5, //remove entity from map + USE_SHOWINFO = 6, //show different info (debug mode) +} USE_TYPE; + +extern char* GetStringForUseType( USE_TYPE useType ); +extern char* GetStringForState( STATE state ); +extern char* GetStringForGlobalState( GLOBALESTATE state ); +extern char* GetContentsString( int contents ); +extern void PrintStringForDamage( int dmgbits ); +extern char* GetStringForDecalName( int decalname ); + +// Misc useful +inline BOOL FStrEq(const char*sz1, const char*sz2) { return (strcmp(sz1, sz2) == 0); } +inline BOOL FClassnameIs(edict_t* pent, const char* szClassname) { return FStrEq(STRING(VARS(pent)->classname), szClassname); } +inline BOOL FClassnameIs(entvars_t* pev, const char* szClassname) { return FStrEq(STRING(pev->classname), szClassname); } + +// Ugly technique to override base member functions +// Normally it's illegal to cast a pointer to a member function of a derived class to a pointer to a +// member function of a base class. static_cast is a sleezy way around that problem. +#ifdef _DEBUG + +#define SetThink( a ) ThinkSet( static_cast (a), #a ) +#define SetTouch( a ) TouchSet( static_cast (a), #a ) +#define SetUse( a ) UseSet( static_cast (a), #a ) +#define SetBlocked( a ) BlockedSet( static_cast (a), #a ) + +#else + +#define SetThink( a ) m_pfnThink = static_cast (a) +#define SetTouch( a ) m_pfnTouch = static_cast (a) +#define SetUse( a ) m_pfnUse = static_cast (a) +#define SetBlocked( a ) m_pfnBlocked = static_cast (a) + +#endif + +// Misc. Prototypes +extern void UTIL_SetSize (entvars_t* pev, const Vector &vecMin, const Vector &vecMax); +extern float UTIL_VecToYaw (const Vector &vec); +extern Vector UTIL_VecToAngles (const Vector &vec); +extern float UTIL_AngleMod (float a); +extern float UTIL_AngleDiff ( float destAngle, float srcAngle ); + +extern Vector UTIL_AxisRotationToAngles (const Vector &vec, float angle); //LRC +extern Vector UTIL_AxisRotationToVec (const Vector &vec, float angle); //LRC + +extern CBaseEntity *UTIL_FindEntityInSphere(CBaseEntity *pStartEntity, const Vector &vecCenter, float flRadius); +extern CBaseEntity *UTIL_FindEntityByString(CBaseEntity *pStartEntity, const char *szKeyword, const char *szValue ); +extern CBaseEntity *UTIL_FindEntityByClassname(CBaseEntity *pStartEntity, const char *szName ); +extern CBaseEntity *UTIL_FindEntityByTargetname(CBaseEntity *pStartEntity, const char *szName ); +extern CBaseEntity *UTIL_FindEntityByTargetname(CBaseEntity *pStartEntity, const char *szName, CBaseEntity *pActivator ); //LRC - for $locus references +extern CBaseEntity *UTIL_FindEntityByTarget(CBaseEntity *pStartEntity, const char *szName ); +extern CBaseEntity *UTIL_FindEntityGeneric(const char *szName, Vector &vecSrc, float flRadius ); +extern CBaseEntity *UTIL_FindGlobalEntity( string_t classname, string_t globalname ); +extern CBaseEntity *UTIL_FindPlayerInSphere( const Vector &vecCenter, float flRadius ); +extern CBasePlayer *UTIL_FindPlayerInPVS( edict_t *pent ); + +// returns a CBaseEntity pointer to a player by index. Only returns if the player is spawned and connected +// otherwise returns NULL +// Index is 1 based +extern CBaseEntity *UTIL_PlayerByIndex( int playerIndex ); + +#define UTIL_EntitiesInPVS(pent) (*g_engfuncs.pfnEntitiesInPVS)(pent) +extern void UTIL_MakeVectors (const Vector &vecAngles); + +// Pass in an array of pointers and an array size, it fills the array and returns the number inserted +extern int UTIL_MonstersInSphere( CBaseEntity **pList, int listMax, const Vector ¢er, float radius ); +extern int UTIL_EntitiesInBox( CBaseEntity **pList, int listMax, const Vector &mins, const Vector &maxs, int flagMask ); + +inline void UTIL_MakeVectorsPrivate( const Vector &vecAngles, float *p_vForward, float *p_vRight, float *p_vUp ) +{ + g_engfuncs.pfnAngleVectors( vecAngles, p_vForward, p_vRight, p_vUp ); +} + +extern void UTIL_MakeAimVectors ( const Vector &vecAngles ); // like MakeVectors, but assumes pitch isn't inverted +extern void UTIL_MakeInvVectors ( const Vector &vec, globalvars_t *pgv ); + +extern void UTIL_SetEdictOrigin ( edict_t *pEdict, const Vector &vecOrigin ); + +extern void UTIL_EmitAmbientSound( edict_t *entity, const Vector &vecOrigin, const char *samp, float vol, float attenuation, int fFlags, int pitch ); +extern void UTIL_ParticleEffect( const Vector &vecOrigin, const Vector &vecDirection, ULONG ulColor, ULONG ulCount ); +extern void UTIL_ScreenShake( const Vector ¢er, float amplitude, float frequency, float duration, float radius ); +extern void UTIL_ScreenShake( const Vector ¢er, float amplitude, float frequency, float duration, float radius, ShakeCommand_t eCommand, BOOL bAirShake ); +extern void UTIL_ScreenShakeAll( const Vector ¢er, float amplitude, float frequency, float duration, ShakeCommand_t eCommand, BOOL bAirShake ); +extern void UTIL_ShowMessage( const char *pString, CBaseEntity *pPlayer ); +extern void UTIL_ShowMessageAll( const char *pString ); + +extern void UTIL_ScreenFadeAll( const Vector &color, float fadeTime, float fadeHold, int alpha, int flags ); +extern void UTIL_ScreenFade( const Vector &color, float fadeTime, float fadeHold, int alpha, int flags, int playernum = 1); + +extern void UTIL_SetFog ( Vector color, int iFadeTime, int iStartDist, int iEndDist, int playernum = 1 ); +extern void UTIL_SetFogAll ( Vector color, int iFadeTime, int iStartDist, int iEndDist ); + +typedef enum { ignore_monsters=1, dont_ignore_monsters=0, missile=2 } IGNORE_MONSTERS; +typedef enum { ignore_glass=1, dont_ignore_glass=0 } IGNORE_GLASS; +extern void UTIL_TraceLine (const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, edict_t *pentIgnore, TraceResult *ptr); +extern void UTIL_TraceLine (const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, IGNORE_GLASS ignoreGlass, edict_t *pentIgnore, TraceResult *ptr); +extern void UTIL_TraceHull (const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, int hullNumber, edict_t *pentIgnore, TraceResult *ptr); +extern TraceResult UTIL_GetGlobalTrace (void); +extern void UTIL_TraceModel (const Vector &vecStart, const Vector &vecEnd, int hullNumber, edict_t *pentModel, TraceResult *ptr); +extern Vector UTIL_GetAimVector (edict_t* pent, float flSpeed); +extern int UTIL_PointContents (const Vector &vec); + +extern int UTIL_IsMasterTriggered (string_t sMaster, CBaseEntity *pActivator); +extern void UTIL_BloodStream( const Vector &origin, const Vector &direction, int color, int amount ); +extern void UTIL_BloodDrips( const Vector &origin, const Vector &direction, int color, int amount ); +extern Vector UTIL_RandomBloodVector( void ); +extern BOOL UTIL_ShouldShowBlood( int bloodColor ); +extern void UTIL_BloodDecalTrace( TraceResult *pTrace, int bloodColor ); +extern void UTIL_DecalTrace( TraceResult *pTrace, int decalNumber ); +extern void UTIL_PlayerDecalTrace( TraceResult *pTrace, int playernum, int decalNumber, BOOL bIsCustom ); +extern void UTIL_GunshotDecalTrace( TraceResult *pTrace, int decalNumber ); +extern void UTIL_Sparks( const Vector &position ); +extern void UTIL_Explode( const Vector ¢er, edict_t *pOwner, int radius, int name = 0 ); +extern void UTIL_Ricochet( const Vector &position, float scale ); +extern void UTIL_StringToVector( float *pVector, const char *pString ); +extern void UTIL_StringToRandomVector( float *pVector, const char *pString ); //LRC +extern void UTIL_StringToIntArray( int *pVector, int count, const char *pString ); +extern Vector UTIL_ClampVectorToBox( const Vector &input, const Vector &clampSize ); +extern float UTIL_Approach( float target, float value, float speed ); +extern float UTIL_ApproachAngle( float target, float value, float speed ); +extern float UTIL_AngleDistance( float next, float cur ); + +extern char *UTIL_VarArgs( char *format, ... ); +extern void UTIL_Remove( CBaseEntity *pEntity ); +extern BOOL UTIL_IsValidEntity( edict_t *pent ); +extern BOOL UTIL_TeamsMatch( const char *pTeamName1, const char *pTeamName2 ); +extern BOOL UTIL_IsFacing( entvars_t *pevTest, const Vector &reference ); //LRC + +// Use for ease-in, ease-out style interpolation (accel/decel) +extern float UTIL_SplineFraction( float value, float scale ); + +// Search for water transition along a vertical line +extern float UTIL_WaterLevel( const Vector &position, float minz, float maxz ); +extern void UTIL_Bubbles( Vector mins, Vector maxs, int count ); +extern void UTIL_BubbleTrail( Vector from, Vector to, int count ); + +// allows precacheing of other entities +// prints a message to each client +extern void UTIL_ClientPrintAll( int msg_dest, const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL ); +inline void UTIL_CenterPrintAll( const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL ) +{ + UTIL_ClientPrintAll( HUD_PRINTCENTER, msg_name, param1, param2, param3, param4 ); +} + +class CBasePlayerWeapon; +class CBasePlayer; +extern BOOL UTIL_GetNextBestWeapon( CBasePlayer *pPlayer, CBasePlayerWeapon *pCurrentWeapon ); + +// prints messages through the HUD +extern void ClientPrint( entvars_t *client, int msg_dest, const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL ); + +// prints a message to the HUD say (chat) +extern void UTIL_SayText( const char *pText, CBaseEntity *pEntity ); +extern void UTIL_SayTextAll( const char *pText, CBaseEntity *pEntity ); + + +typedef struct hudtextparms_s +{ + float x; + float y; + int effect; + byte r1, g1, b1, a1; + byte r2, g2, b2, a2; + float fadeinTime; + float fadeoutTime; + float holdTime; + float fxTime; + int channel; +} hudtextparms_t; + +// prints as transparent 'title' to the HUD +extern void UTIL_HudMessageAll( const hudtextparms_t &textparms, const char *pMessage ); +extern void UTIL_HudMessage( CBaseEntity *pEntity, const hudtextparms_t &textparms, const char *pMessage ); + +// for handy use with ClientPrint params +extern char *UTIL_dtos1( int d ); +extern char *UTIL_dtos2( int d ); +extern char *UTIL_dtos3( int d ); +extern char *UTIL_dtos4( int d ); + +// Writes message to console with timestamp and FragLog header. +extern void UTIL_LogPrintf( char *fmt, ... ); + +// Sorta like FInViewCone, but for nonmonsters. +extern float UTIL_DotPoints ( const Vector &vecSrc, const Vector &vecCheck, const Vector &vecDir ); + +extern void UTIL_StripToken( const char *pKey, char *pDest );// for redundant keynames + +// Misc functions +extern void UTIL_SetMovedir(entvars_t* pev); +extern void UTIL_SynchDoors( CBaseEntity *pEntity ); +extern Vector UTIL_GetMovedir( Vector vecAngles ); +extern Vector VecBModelOrigin( entvars_t* pevBModel ); +extern int BuildChangeList( LEVELLIST *pLevelList, int maxList ); + +// +// How did I ever live without ASSERT? +// +#ifdef DEBUG +void DBG_AssertFunction(BOOL fExpr, const char* szExpr, const char* szFile, int szLine, const char* szMessage); +#define ASSERT(f) DBG_AssertFunction(f, #f, __FILE__, __LINE__, NULL) +#define ASSERTSZ(f, sz) DBG_AssertFunction(f, #f, __FILE__, __LINE__, sz) +#else // !DEBUG +#define ASSERT(f) +#define ASSERTSZ(f, sz) +#endif // !DEBUG + + +extern DLL_GLOBAL const Vector g_vecZero; + +// +// Constants that were used only by QC (maybe not used at all now) +// +// Un-comment only as needed +// +#define LANGUAGE_ENGLISH 0 +#define LANGUAGE_GERMAN 1 +#define LANGUAGE_FRENCH 2 +#define LANGUAGE_BRITISH 3 + +extern DLL_GLOBAL int g_Language; + +#define AMBIENT_SOUND_STATIC 0 // medium radius attenuation +#define AMBIENT_SOUND_EVERYWHERE 1 +#define AMBIENT_SOUND_SMALLRADIUS 2 +#define AMBIENT_SOUND_MEDIUMRADIUS 4 +#define AMBIENT_SOUND_LARGERADIUS 8 +#define AMBIENT_SOUND_START_SILENT 16 +#define AMBIENT_SOUND_NOT_LOOPING 32 + +#define SPEAKER_START_SILENT 1 // wait for trigger 'on' to start announcements + +// duplicated in protocol.h +#define SND_STOP (1<<5) // stop sound or loopsound +#define SND_CHANGE_VOL (1<<6) // change sound vol +#define SND_CHANGE_PITCH (1<<7) // change sound pitch +#define SND_SPAWNING (1<<8) // we're spawing, used in some cases for ambients + +#define LFO_SQUARE 1 +#define LFO_TRIANGLE 2 +#define LFO_RANDOM 3 + +// func_rotating +#define SF_BRUSH_ROTATE_Y_AXIS 0 //!?! (LRC) +#define SF_BRUSH_ROTATE_INSTANT 1 +#define SF_BRUSH_ROTATE_BACKWARDS 2 +#define SF_BRUSH_ROTATE_Z_AXIS 4 +#define SF_BRUSH_ROTATE_X_AXIS 8 +#define SF_PENDULUM_AUTO_RETURN 16 +#define SF_PENDULUM_PASSABLE 32 + +#define SF_BRUSH_ROTATE_SMALLRADIUS 128 +#define SF_BRUSH_ROTATE_MEDIUMRADIUS 256 +#define SF_BRUSH_ROTATE_LARGERADIUS 512 + +#define PUSH_BLOCK_ONLY_X 1 +#define PUSH_BLOCK_ONLY_Y 2 + +#define VEC_HULL_MIN Vector(-16, -16, -36) +#define VEC_HULL_MAX Vector( 16, 16, 36) +#define VEC_HUMAN_HULL_MIN Vector( -16, -16, 0 ) +#define VEC_HUMAN_HULL_MAX Vector( 16, 16, 72 ) +#define VEC_HUMAN_HULL_DUCK Vector( 16, 16, 36 ) + +#define VEC_VIEW Vector( 0, 0, 28 ) + +#define VEC_DUCK_HULL_MIN Vector(-16, -16, -18 ) +#define VEC_DUCK_HULL_MAX Vector( 16, 16, 18) +#define VEC_DUCK_VIEW Vector( 0, 0, 12 ) + +#define SVC_TEMPENTITY 23 +#define SVC_INTERMISSION 30 +#define SVC_CDTRACK 32 +#define SVC_WEAPONANIM 35 +#define SVC_ROOMTYPE 37 +#define SVC_DIRECTOR 51 + +// camera flags +#define CAMERA_ON 1 +#define DRAW_HUD 2 +#define INVERSE_X 4 +#define MONSTER_VIEW 8 + +// triggers +#define SF_TRIGGER_ALLOWMONSTERS 1// monsters allowed to fire this trigger +#define SF_TRIGGER_NOCLIENTS 2// players not allowed to fire this trigger +#define SF_TRIGGER_PUSHABLES 4// only pushables can fire this trigger +#define SF_TRIGGER_EVERYTHING 8// everything else can fire this trigger (e.g. gibs, rockets) + +// func breakable +#define SF_BREAK_TRIGGER_ONLY 1// may only be broken by trigger +#define SF_BREAK_TOUCH 2// can be 'crashed through' by running player (plate glass) +#define SF_BREAK_PRESSURE 4// can be broken by a player standing on it +#define SF_BREAK_CROWBAR 256// instant break if hit with crowbar + +// func_pushable (it's also func_breakable, so don't collide with those flags) +#define SF_PUSH_BREAKABLE 128 +#define SF_PUSH_NOPULL 512//LRC +#define SF_PUSH_USECUSTOMSIZE 0x800000 //LRC, not yet used + +#define SF_LIGHT_START_OFF 1 + +#define SPAWNFLAG_NOMESSAGE 1 +#define SPAWNFLAG_NOTOUCH 1 +#define SPAWNFLAG_DROIDONLY 4 + +#define SPAWNFLAG_USEONLY 1 // can't be touched, must be used (buttons) + +#define TELE_PLAYER_ONLY 1 +#define TELE_SILENT 2 + +#define SF_TRIG_PUSH_ONCE 1 + + +// Sound Utilities + +// sentence groups +#define CBSENTENCENAME_MAX 16 +#define CVOXFILESENTENCEMAX 1536 // max number of sentences in game. NOTE: this must match + +static unsigned short FixedUnsigned16( float value, float scale ) +{ + int output; + + output = value * scale; + if ( output < 0 ) output = 0; + if ( output > 0xFFFF ) output = 0xFFFF; + return (unsigned short)output; +} + +static short FixedSigned16( float value, float scale ) +{ + int output; + + output = value * scale; + if ( output > 32767 ) output = 32767; + if ( output < -32768 ) output = -32768; + return (short)output; +} // CVOXFILESENTENCEMAX in engine\sound.h!!! + +extern char gszallsentencenames[CVOXFILESENTENCEMAX][CBSENTENCENAME_MAX]; +extern int gcallsentences; + +int USENTENCEG_Pick(int isentenceg, char *szfound); +int USENTENCEG_PickSequential(int isentenceg, char *szfound, int ipick, int freset); +void USENTENCEG_InitLRU(unsigned char *plru, int count); + +void SENTENCEG_Init(); +void SENTENCEG_Stop(edict_t *entity, int isentenceg, int ipick); +int SENTENCEG_PlayRndI(edict_t *entity, int isentenceg, float volume, float attenuation, int flags, int pitch); +int SENTENCEG_PlayRndSz(edict_t *entity, const char *szrootname, float volume, float attenuation, int flags, int pitch); +int SENTENCEG_PlaySequentialSz(edict_t *entity, const char *szrootname, float volume, float attenuation, int flags, int pitch, int ipick, int freset); +int SENTENCEG_GetIndex(const char *szrootname); +int SENTENCEG_Lookup(const char *sample, char *sentencenum); + +void TEXTURETYPE_Init(); +char TEXTURETYPE_Find(char *name); +float TEXTURETYPE_PlaySound(TraceResult *ptr, Vector vecSrc, Vector vecEnd, int iBulletType); + +// NOTE: use EMIT_SOUND_DYN to set the pitch of a sound. Pitch of 100 +// is no pitch shift. Pitch > 100 up to 255 is a higher pitch, pitch < 100 +// down to 1 is a lower pitch. 150 to 70 is the realistic range. +// EMIT_SOUND_DYN with pitch != 100 should be used sparingly, as it's not quite as +// fast as EMIT_SOUND (the pitchshift mixer is not native coded). + +void EMIT_SOUND_DYN(edict_t *entity, int channel, const char *sample, float volume, float attenuation, + int flags, int pitch); + + +inline void EMIT_SOUND(edict_t *entity, int channel, const char *sample, float volume, float attenuation) +{ + EMIT_SOUND_DYN(entity, channel, sample, volume, attenuation, 0, PITCH_NORM); +} + +inline void STOP_SOUND(edict_t *entity, int channel, const char *sample) +{ + EMIT_SOUND_DYN(entity, channel, sample, 0, 0, SND_STOP, PITCH_NORM); +} + +void EMIT_SOUND_SUIT(edict_t *entity, const char *sample); +void EMIT_GROUPID_SUIT(edict_t *entity, int isentenceg); +void EMIT_GROUPNAME_SUIT(edict_t *entity, const char *groupname); + +#define PRECACHE_SOUND_ARRAY( a ) \ + { for (int i = 0; i < ARRAYSIZE( a ); i++ ) PRECACHE_SOUND((char *) a [i]); } + +#define EMIT_SOUND_ARRAY_DYN( chan, array ) \ + EMIT_SOUND_DYN ( ENT(pev), chan , array [ RANDOM_LONG(0,ARRAYSIZE( array )-1) ], 1.0, ATTN_NORM, 0, RANDOM_LONG(95,105) ); + +#define RANDOM_SOUND_ARRAY( array ) (array) [ RANDOM_LONG(0,ARRAYSIZE( (array) )-1) ] + +int UTIL_SharedRandomLong( unsigned int seed, int low, int high ); +float UTIL_SharedRandomFloat( unsigned int seed, float low, float high ); + +float UTIL_WeaponTimeBase( void ); +int GetStdLightStyle (int iStyle); //LRC- declared here so it can be used by everything that + // needs to deal with the standard lightstyles. +// for trigger_viewset +int HaveCamerasInPVS( edict_t* edict ); +BOOL IsMultiplayer ( void ); +BOOL IsDeatchmatch ( void ); + +void UTIL_SetView( int ViewEntity = 0, int flags = 0 ); +void UTIL_SetView( CBaseEntity *pActivator, int ViewEntity = 0, int flags = 0 ); +void UTIL_SetView( CBaseEntity *pActivator, CBaseEntity *pViewEnt = 0, int flags = 0 ); + +void UTIL_SetModel( edict_t *e, string_t s, char *c ); +void UTIL_SetModel( edict_t *e, const char *model ); +void UTIL_SetModel( edict_t *e, string_t model ); +int UTIL_PrecacheModel( char* s ); +int UTIL_PrecacheModel( string_t s, char *e ); +int UTIL_PrecacheSound( char* s ); +int UTIL_PrecacheSound( string_t s, char *e ); +int UTIL_PrecacheModel( string_t s ); +int UTIL_PrecacheSound( string_t s ); +void UTIL_PrecacheEntity( const char *szClassname ); + +int UTIL_PrecacheAurora( string_t s ); +int UTIL_PrecacheAurora( const char *s ); +void UTIL_SetAurora( CBaseEntity *pAttach, int aur, int attachment = 0 ); +void UTIL_SetBeams( char *szFile, CBaseEntity *pStart, CBaseEntity *pEnd ); + +extern int giAmmoIndex; + +Vector UTIL_GetAngleDistance( Vector vecAngles, float distance ); +void UTIL_SetThink ( CBaseEntity *pEnt ); + +void UTIL_AssignOrigin( CBaseEntity* pEntity, const Vector vecOrigin, BOOL bInitiator = TRUE); +void UTIL_AssignAngles( CBaseEntity *pEntity, const Vector vecAngles, BOOL bInitiator = TRUE); +void UTIL_ComplexRotate( CBaseEntity *pParent, CBaseEntity *pChild, const Vector &dest, float m_flTravelTime ); + +void UTIL_SetAngles ( CBaseEntity* pEntity, const Vector &vecAngles ); +void UTIL_SetOrigin ( CBaseEntity* pEntity, const Vector &vecOrigin ); + +void UTIL_SetVelocity ( CBaseEntity *pEnt, const Vector vecSet ); +void UTIL_SetAvelocity( CBaseEntity *pEnt, const Vector vecSet ); + +void UTIL_SetChildVelocity( CBaseEntity *pEnt, const Vector vecSet, int loopbreaker ); +void UTIL_SetChildAvelocity( CBaseEntity *pEnt, const Vector vecSet, int loopbreaker ); +void UTIL_MergePos ( CBaseEntity *pEnt, int loopbreaker = MAX_CHILDS); + +#define CHECK_FLAG(x) if(Ent->pFlags & x) strcat(szFlags,#x", ") +#define clamp(a,min,max) ((a < min)?(min):((a > max)?(max):(a))) +#define DECLARE_CLASS( className, baseClassName ) \ + typedef baseClassName BaseClass; \ + typedef className ThisClass; + +//COM_Utils +void UTIL_PrecacheResourse( void ); +void Msg( char *szText, ... ); +void DevMsg( char *szText, ... ); +char *COM_ParseFile( char *data, char *token ); +char *COM_Parse( char *data ); +void COM_FreeFile (char *buffer); + + +//Xash Parent System Flags +#define PF_AFFECT (1<<0) //this is child entity +#define PF_DESIRED (1<<1) +#define PF_ACTION (1<<2) //this is chain entity (set automatically) +#define PF_MOVENONE (1<<3) //this entity has MOVETYPE_NONE before set parent (needs for return) +#define PF_CORECTSPEED (1<<4) //force child to parent coordintes e.g. sprite to attachment +#define PF_MERGEPOS (1<<5) //Merge current pos for entity +#define PF_POSTVELOCITY (1<<6) //apply and correct velocity from parent +#define PF_POSTAVELOCITY (1<<7) //apply and correct avelocity from parent +#define PF_SETTHINK (1<<8) //manually force entity to think +#define PF_POSTAFFECT (1<<9) //apply +#define PF_PARENTMOVE (1<<10) //parent give me velocity or avelocity +#define PF_ANGULAR (1<<11) //parent has angular moving +#define PF_POSTORG (1<<12) //this entity MUST changed origin only +#define PF_LINKCHILD (PF_AFFECT|PF_DESIRED|PF_MERGEPOS|PF_POSTORG) + +//new system flags +#define EFL_DIRTY_ABSTRANSFORM (1<<13) +#define EFL_DIRTY_ABSVELOCITY (1<<14) +#define EFL_DIRTY_ABSANGVELOCITY (1<<15) + +#define POSITION_CHANGED 0x1 +#define ANGLES_CHANGED 0x2 +#define VELOCITY_CHANGED 0x4 + +BOOL FClassnameIs(CBaseEntity *pEnt, const char* szClassname); + +//affects +int FrameAffect( CBaseEntity *pEnt ); +int PostFrameAffect( CBaseEntity *pEnt ); + +//childs affect +void ChildAffect( CBaseEntity *pEnt, Vector vecAdjustVel, Vector vecAdjustAVel ); +void ChildPostAffect ( CBaseEntity *pEnt ); + +//link operations +void LinkChild(CBaseEntity *pEnt); + +void UnlinkFromParent( CBaseEntity *pRemove ); +void TransferChildren( CBaseEntity *pOldParent, CBaseEntity *pNewParent ); +void LinkChild( CBaseEntity *pParent, CBaseEntity *pChild ); +void UnlinkAllChildren( CBaseEntity *pParent ); +bool EntityIsParentOf( CBaseEntity *pParent, CBaseEntity *pEntity ); + +//handles +void HandleAffect( CBaseEntity *pEnt, Vector vecAdjustVel, Vector vecAdjustAVel ); +void HandlePostAffect( CBaseEntity *pEnt ); + +//debug +void GetPFlags( CBaseEntity* Ent ); +void GetPInfo( CBaseEntity* Ent ); + +//Parent utils +void UTIL_MarkChild ( CBaseEntity *pEnt, BOOL correctSpeed = FALSE, BOOL desired = TRUE); +void UTIL_SetPostAffect( CBaseEntity *pEnt ); +void UTIL_SetThink ( CBaseEntity *pEnt ); +void UTIL_SetAction( CBaseEntity *pEnt ); +BOOL SynchLost( CBaseEntity *pEnt );//watching for synchronize + +Vector UTIL_RandomVector(void); +Vector UTIL_RandomVector( Vector vmin, Vector vmax ); +void UTIL_AngularVector( CBaseEntity *pEnt ); +void UTIL_LinearVector( CBaseEntity *pEnt ); +float UTIL_CalcDistance( Vector vecAngles ); +void UTIL_WatchTarget( CBaseEntity *pWatcher, CBaseEntity *pTarget); +void UTIL_FindBreakable( CBaseEntity *Brush ); +edict_t *UTIL_FindLandmark( string_t iLandmarkName ); +edict_t *UTIL_FindLandmark( const char *pLandmarkName ); +int UTIL_FindTransition( CBaseEntity *pEntity, char *pVolumeName ); +int UTIL_FindTransition( CBaseEntity *pEntity, string_t iVolumeName ); +void UTIL_FireTargets( const char *targetName, CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value = 0); +void UTIL_FireTargets( int targetName, CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value = 0); +CBaseEntity *UTIL_FindEntityForward( CBaseEntity *pMe ); +void UTIL_FindHullIntersection( const Vector &vecSrc, TraceResult &tr, float *mins, float *maxs, edict_t *pEntity ); +int UTIL_LoadSoundPreset( string_t pString ); +int UTIL_LoadSoundPreset( const char *pString ); +int UTIL_LoadDecalPreset( string_t pString ); +int UTIL_LoadDecalPreset( const char *pString ); +void UTIL_ChangeLevel( string_t mapname, string_t spotname ); +void UTIL_ChangeLevel( const char *szNextMap, const char *szNextSpot ); +void AddAmmoName( const char *szAmmoname ); + +// precache utils +void UTIL_PrecacheEntity( string_t szClassname ); +BOOL UTIL_EntIsVisible( entvars_t* pev, entvars_t* pevTarget); + +#endif //UTIL_H \ No newline at end of file diff --git a/server/global/vector.h b/server/global/vector.h new file mode 100644 index 00000000..813f0ed7 --- /dev/null +++ b/server/global/vector.h @@ -0,0 +1,281 @@ +//======================================================================= +// Copyright (C) Shambler Team 2005 +// vector.h - shared vector operations +//======================================================================= +#ifndef VECTOR_H +#define VECTOR_H + +#include +#include +#include +#include + +#pragma warning(disable : 4244) // int or float down-conversion + +#ifdef CLIENT_DLL + // Header file containing definition of globalvars_t and entvars_t +typedef int func_t; // +typedef int string_t; // from engine's pr_comp.h; +typedef float vec_t; // needed before including progdefs.h +#endif + +//========================================================= +// 2DVector - used for many pathfinding and many other +// operations that are treated as planar rather than 3d. +//========================================================= +class Vector2D +{ +public: + inline Vector2D(void) { } + inline Vector2D(float X, float Y) { x = X; y = Y; } + inline Vector2D operator+(const Vector2D& v) const { return Vector2D(x+v.x, y+v.y); } + inline Vector2D operator-(const Vector2D& v) const { return Vector2D(x-v.x, y-v.y); } + inline Vector2D operator*(float fl) const { return Vector2D(x*fl, y*fl); } + inline Vector2D operator/(float fl) const { return Vector2D(x/fl, y/fl); } + +#ifdef CLIENT_DLL + inline float Length(void) const { return (float)sqrt(x*x + y*y ); } +#else + inline float Length(void) const { return sqrt(x*x + y*y ); } +#endif + inline Vector2D Normalize ( void ) const + { + Vector2D vec2; + + float flLen = Length(); + if ( flLen == 0 ) + { +#ifdef CLIENT_DLL + return Vector2D( (float)0, (float)0 ); +#else + return Vector2D( 0, 0 ); +#endif + } + else + { + flLen = 1 / flLen; + return Vector2D( x * flLen, y * flLen ); + } + } + vec_t x, y; +}; + +#define nanmask (255<<23) +#define IS_NAN(x) (((*(int *)&x)&nanmask)==nanmask) + +inline float DotProduct(const Vector2D& a, const Vector2D& b) { return( a.x*b.x + a.y*b.y ); } +inline Vector2D operator*(float fl, const Vector2D& v) { return v * fl; } + +//========================================================= +// 3D Vector +//========================================================= +class Vector // same data-layout as engine's vec3_t, +{ // which is a vec_t[3] +public: + // Construction/destruction + inline Vector(void) { } + inline Vector(float X, float Y, float Z) { x = X; y = Y; z = Z; } + inline Vector(const Vector& v) { x = v.x; y = v.y; z = v.z; } + inline Vector(float rgfl[3]) { x = rgfl[0]; y = rgfl[1]; z = rgfl[2]; } + + // Initialization + void Init(vec_t ix=0.0f, vec_t iy=0.0f, vec_t iz=0.0f){ x = ix; y = iy; z = iz; } + + // Operators + inline Vector operator-(void) const { return Vector(-x,-y,-z); } + inline int operator==(const Vector& v) const { return x==v.x && y==v.y && z==v.z; } + inline int operator!=(const Vector& v) const { return !(*this==v); } + inline Vector operator+(const Vector& v) const { return Vector(x+v.x, y+v.y, z+v.z); } + inline Vector operator-(const Vector& v) const { return Vector(x-v.x, y-v.y, z-v.z); } + inline Vector operator*(float fl) const { return Vector(x*fl, y*fl, z*fl); } + inline Vector operator/(float fl) const { return Vector(x/fl, y/fl, z/fl); } + + _forceinline Vector& operator+=(const Vector &v) + { + x+=v.x; y+=v.y; z += v.z; + return *this; + } + _forceinline Vector& operator-=(const Vector &v) + { + x-=v.x; y-=v.y; z -= v.z; + return *this; + } + _forceinline Vector& operator*=(const Vector &v) + { + x *= v.x; y *= v.y; z *= v.z; + return *this; + } + _forceinline Vector& operator*=(float s) + { + x *= s; y *= s; z *= s; + return *this; + } + _forceinline Vector& operator/=(const Vector &v) + { + x /= v.x; y /= v.y; z /= v.z; + return *this; + } + _forceinline Vector& operator/=(float s) + { + float oofl = 1.0f / s; + x *= oofl; y *= oofl; z *= oofl; + return *this; + } +#ifndef CLIENT_DLL + _forceinline Vector& fixangle(void) + { + if(!IS_NAN(x)) + { + while ( x < 0 ) x += 360; + while ( x > 360 ) x -= 360; + } + if(!IS_NAN(y)) + { + while ( y < 0 ) y += 360; + while ( y > 360 ) y -= 360; + } + if(!IS_NAN(z)) + { + while ( z < 0 ) z += 360; + while ( z > 360 ) z -= 360; + } + return *this; + } +#endif + _forceinline Vector MA( float scale, const Vector &start, const Vector &direction ) const + { + return Vector(start.x + scale * direction.x, start.y + scale * direction.y, start.z + scale * direction.z) ; + } + + // Methods + inline void CopyToArray(float* rgfl) const { rgfl[0] = x, rgfl[1] = y, rgfl[2] = z; } +#ifdef CLIENT_DLL + inline float Length(void) const { return (float)sqrt(x*x + y*y + z*z); } +#else + inline float Length(void) const { return sqrt(x*x + y*y + z*z); } +#endif + operator float *() { return &x; } // Vectors will now automatically convert to float * when needed + operator const float *() const { return &x; } // Vectors will now automatically convert to float * when needed + + // array access... + vec_t operator[](int i) const { return ((vec_t*)this)[i];} + vec_t& operator[](int i) { return ((vec_t*)this)[i];} + + inline Vector Normalize(void) const + { + float flLen = Length(); + if (flLen == 0) return Vector(0,0,1); // ???? + flLen = 1 / flLen; + return Vector(x * flLen, y * flLen, z * flLen); + } + vec_t Dot(Vector const& vOther) const + { + return(x*vOther.x+y*vOther.y+z*vOther.z); + } + Vector Cross(const Vector &vOther) const + { + return Vector(y*vOther.z - z*vOther.y, z*vOther.x - x*vOther.z, x*vOther.y - y*vOther.x); + } + inline Vector2D Make2D ( void ) const + { + Vector2D Vec2; + Vec2.x = x; + Vec2.y = y; + return Vec2; + } +#ifdef CLIENT_DLL + inline float Length2D(void) const { return (float)sqrt(x*x + y*y); } +#else + inline float Length2D(void) const { return sqrt(x*x + y*y); } +#endif + // Members + vec_t x, y, z; +}; +inline Vector operator*(float fl, const Vector& v) { return v * fl; } +inline float DotProduct(const Vector& a, const Vector& b) { return(a.x*b.x+a.y*b.y+a.z*b.z); } +inline Vector CrossProduct(const Vector& a, const Vector& b) { return Vector( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x ); } +#define vec3_t Vector + +//========================================================= +// 4D Vector - for matrix operations +//========================================================= +class Vector4D +{ +public: + // Members + vec_t x, y, z, w; + + // Construction/destruction + Vector4D(void){} + Vector4D(vec_t X, vec_t Y, vec_t Z, vec_t W) { x = X; y = Y; z = Z; w = W;} + Vector4D(double X, double Y, double Z, double W) { x = (double)X; y = (double)Y; z = (double)Z; w = (double)W;} + Vector4D(const float *pFloat) { x = pFloat[0]; y = pFloat[1]; z = pFloat[2]; w = pFloat[3];} + + // array access... + vec_t operator[](int i) const { return ((vec_t*)this)[i];} + vec_t& operator[](int i) { return ((vec_t*)this)[i];} + + // equality + bool operator==(const Vector4D& src) const{ return(src.x == x) && (src.y == y) && (src.z == z) && (src.w == w);} + bool operator!=(const Vector4D& src) const{ return(src.x != x) || (src.y != y) || (src.z != z) || (src.w != w);} + + // arithmetic operations + Vector4D& operator+=(const Vector4D &v){ x+=v.x; y+=v.y; z += v.z; return *this;} + Vector4D& operator-=(const Vector4D &v){ x-=v.x; y-=v.y; z -= v.z; return *this;} + Vector4D operator+ (const Vector4D &v)const {Vector4D res; res.x = x + v.x; res.y = y + v.y; res.z = z + v.z; res.w = w; return res;} + Vector4D operator- (const Vector4D &v)const {Vector4D res; res.x = x - v.x; res.y = y - v.y; res.z = z - v.z; res.w = w; return res;} + Vector4D operator* (const Vector4D &v)const {Vector4D res; res.x = y * v.z - z * v.y; res.y = z * v.x - x * v.z; res.z = x * v.y - y * v.x; res.w = w; return res;} + float operator% (const Vector4D &v)const { return (x * v.x + y * v.y + z * v.z); } + Vector4D Scale( float scale)const {Vector4D res; res.x = x * scale; res.y = y * scale; res.z = z * scale; res.w = w;return res; } + Vector4D CompProduct (const Vector4D &v)const {Vector4D res; res.x = x * v.x; res.y = y * v.y; res.z = z * v.z; res.w = w; return res;} +}; + +//========================================================= +// RandomRange - for random values +//========================================================= +class RandomRange +{ +public: + float m_flMax, m_flMin;//class members + + RandomRange() { m_flMin = m_flMax = 0; } + RandomRange(float fValue) { m_flMin = m_flMax = fValue; } + RandomRange(float fMin, float fMax) { m_flMin = fMin; m_flMax = fMax; } + RandomRange( char *szToken ) + { + char *cOneDot = NULL; + m_flMin = m_flMax = 0; + + for (char *c = szToken; *c; c++) + { + if (*c == '.') + { + if (cOneDot != NULL) + { + // found two dots in a row - it's a range + + *cOneDot = 0; // null terminate the first number + m_flMin = atof(szToken); // parse the first number + *cOneDot = '.'; // change it back, just in case + c++; + m_flMax = atof(c); // parse the second number + return; + } + else cOneDot = c; + } + else cOneDot = NULL; + } + + // no range, just record the number + m_flMax = m_flMin = atof(szToken); + } + + // FIXME: float Random() { return RANDOM_FLOAT(m_flMin, m_flMax); } + float Random() { return m_flMin - m_flMax; } + + // array access... + float operator[](int i) const { return ((float*)this)[i];} + float& operator[](int i) { return ((float*)this)[i];} +}; + +#endif \ No newline at end of file diff --git a/sv_dll/legacy.cpp b/server/legacy.cpp similarity index 100% rename from sv_dll/legacy.cpp rename to server/legacy.cpp diff --git a/server/monsters/activity.h b/server/monsters/activity.h new file mode 100644 index 00000000..a6ba9a8c --- /dev/null +++ b/server/monsters/activity.h @@ -0,0 +1,132 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ + +#ifndef ACTIVITY_H +#define ACTIVITY_H + +typedef enum +{ + ACT_RESET = 0, // Set m_Activity to this invalid value to force a reset to m_IdealActivity + ACT_IDLE = 1, + ACT_GUARD, + ACT_WALK, + ACT_RUN, + ACT_FLY, // Fly (and flap if appropriate) + ACT_SWIM, + ACT_HOP, // vertical jump + ACT_LEAP, // long forward jump + ACT_FALL, + ACT_LAND, + ACT_STRAFE_LEFT, + ACT_STRAFE_RIGHT, + ACT_ROLL_LEFT, // tuck and roll, left + ACT_ROLL_RIGHT, // tuck and roll, right + ACT_TURN_LEFT, // turn quickly left (stationary) + ACT_TURN_RIGHT, // turn quickly right (stationary) + ACT_CROUCH, // the act of crouching down from a standing position + ACT_CROUCHIDLE, // holding body in crouched position (loops) + ACT_STAND, // the act of standing from a crouched position + ACT_USE, + ACT_SIGNAL1, + ACT_SIGNAL2, + ACT_SIGNAL3, + ACT_TWITCH, + ACT_COWER, + ACT_SMALL_FLINCH, + ACT_BIG_FLINCH, + ACT_RANGE_ATTACK1, + ACT_RANGE_ATTACK2, + ACT_MELEE_ATTACK1, + ACT_MELEE_ATTACK2, + ACT_RELOAD, + ACT_ARM, // pull out gun, for instance + ACT_DISARM, // reholster gun + ACT_EAT, // monster chowing on a large food item (loop) + ACT_DIESIMPLE, + ACT_DIEBACKWARD, + ACT_DIEFORWARD, + ACT_DIEVIOLENT, + ACT_BARNACLE_HIT, // barnacle tongue hits a monster + ACT_BARNACLE_PULL, // barnacle is lifting the monster ( loop ) + ACT_BARNACLE_CHOMP, // barnacle latches on to the monster + ACT_BARNACLE_CHEW, // barnacle is holding the monster in its mouth ( loop ) + ACT_SLEEP, + ACT_INSPECT_FLOOR, // for active idles, look at something on or near the floor + ACT_INSPECT_WALL, // for active idles, look at something directly ahead of you + ACT_IDLE_ANGRY, // alternate idle animation in which the monster is clearly agitated. (loop) + ACT_WALK_HURT, // limp (loop) + ACT_RUN_HURT, // limp (loop) + ACT_HOVER, // Idle while in flight + ACT_GLIDE, // Fly (don't flap) + ACT_FLY_LEFT, // Turn left in flight + ACT_FLY_RIGHT, // Turn right in flight + ACT_DETECT_SCENT, // this means the monster smells a scent carried by the air + ACT_SNIFF, // this is the act of actually sniffing an item in front of the monster + ACT_BITE, // some large monsters can eat small things in one bite. This plays one time, EAT loops. + ACT_THREAT_DISPLAY, // without attacking, monster demonstrates that it is angry. (Yell, stick out chest, etc ) + ACT_FEAR_DISPLAY, // monster just saw something that it is afraid of + ACT_EXCITED, // for some reason, monster is excited. Sees something he really likes to eat, or whatever + ACT_SPECIAL_ATTACK1,// very monster specific special attacks. + ACT_SPECIAL_ATTACK2, + ACT_COMBAT_IDLE, // agitated idle. + ACT_WALK_SCARED, + ACT_RUN_SCARED, + ACT_VICTORY_DANCE, // killed a player, do a victory dance. + ACT_DIE_HEADSHOT, // die, hit in head. + ACT_DIE_CHESTSHOT, // die, hit in chest + ACT_DIE_GUTSHOT, // die, hit in gut + ACT_DIE_BACKSHOT, // die, hit in back + ACT_FLINCH_HEAD, + ACT_FLINCH_CHEST, + ACT_FLINCH_STOMACH, + ACT_FLINCH_LEFTARM, + ACT_FLINCH_RIGHTARM, + ACT_FLINCH_LEFTLEG, + ACT_FLINCH_RIGHTLEG, + ACT_VM_NONE, // weapon viewmodel animations + ACT_VM_DEPLOY, // deploy + ACT_VM_DEPLOY_EMPTY,// deploy empty weapon + ACT_VM_HOLSTER, // holster empty weapon + ACT_VM_HOLSTER_EMPTY, + ACT_VM_IDLE1, + ACT_VM_IDLE2, + ACT_VM_IDLE3, + ACT_VM_RANGE_ATTACK1, + ACT_VM_RANGE_ATTACK2, + ACT_VM_RANGE_ATTACK3, + ACT_VM_MELEE_ATTACK1, + ACT_VM_MELEE_ATTACK2, + ACT_VM_MELEE_ATTACK3, + ACT_VM_SHOOT_EMPTY, + ACT_VM_START_RELOAD, + ACT_VM_RELOAD, + ACT_VM_RELOAD_EMPTY, + ACT_VM_TURNON, + ACT_VM_TURNOFF, + ACT_VM_PUMP, // pumping gun + ACT_VM_PUMP_EMPTY, + ACT_VM_START_CHARGE, + ACT_VM_CHARGE, + ACT_VM_OVERLOAD, + ACT_VM_IDLE_EMPTY, +} Activity; + +// studio activity map conversion +typedef struct { int type; char *name; } activity_map_t; + +extern activity_map_t activity_map[]; + + +#endif//ACTIVITY_H \ No newline at end of file diff --git a/server/monsters/activitymap.h b/server/monsters/activitymap.h new file mode 100644 index 00000000..af156fde --- /dev/null +++ b/server/monsters/activitymap.h @@ -0,0 +1,121 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ + +activity_map_t activity_map[] = +{ +{ACT_IDLE, "ACT_IDLE" }, +{ACT_GUARD, "ACT_GUARD" }, +{ACT_WALK, "ACT_WALK" }, +{ACT_RUN, "ACT_RUN" }, +{ACT_FLY, "ACT_FLY" }, +{ACT_SWIM, "ACT_SWIM", }, +{ACT_HOP, "ACT_HOP", }, +{ACT_LEAP, "ACT_LEAP" }, +{ACT_FALL, "ACT_FALL" }, +{ACT_LAND, "ACT_LAND" }, +{ACT_STRAFE_LEFT, "ACT_STRAFE_LEFT" }, +{ACT_STRAFE_RIGHT, "ACT_STRAFE_RIGHT" }, +{ACT_ROLL_LEFT, "ACT_ROLL_LEFT" }, +{ACT_ROLL_RIGHT, "ACT_ROLL_RIGHT" }, +{ACT_TURN_LEFT, "ACT_TURN_LEFT" }, +{ACT_TURN_RIGHT, "ACT_TURN_RIGHT" }, +{ACT_CROUCH, "ACT_CROUCH" }, +{ACT_CROUCHIDLE, "ACT_CROUCHIDLE" }, +{ACT_STAND, "ACT_STAND" }, +{ACT_USE, "ACT_USE" }, +{ACT_SIGNAL1, "ACT_SIGNAL1" }, +{ACT_SIGNAL2, "ACT_SIGNAL2" }, +{ACT_SIGNAL3, "ACT_SIGNAL3" }, +{ACT_TWITCH, "ACT_TWITCH" }, +{ACT_COWER, "ACT_COWER" }, +{ACT_SMALL_FLINCH, "ACT_SMALL_FLINCH" }, +{ACT_BIG_FLINCH, "ACT_BIG_FLINCH" }, +{ACT_RANGE_ATTACK1, "ACT_RANGE_ATTACK1" }, +{ACT_RANGE_ATTACK2, "ACT_RANGE_ATTACK2" }, +{ACT_MELEE_ATTACK1, "ACT_MELEE_ATTACK1" }, +{ACT_MELEE_ATTACK2, "ACT_MELEE_ATTACK2" }, +{ACT_RELOAD, "ACT_RELOAD" }, +{ACT_ARM, "ACT_ARM" }, +{ACT_DISARM, "ACT_DISARM" }, +{ACT_EAT, "ACT_EAT" }, +{ACT_DIESIMPLE, "ACT_DIESIMPLE" }, +{ACT_DIEBACKWARD, "ACT_DIEBACKWARD" }, +{ACT_DIEFORWARD, "ACT_DIEFORWARD" }, +{ACT_DIEVIOLENT, "ACT_DIEVIOLENT" }, +{ACT_BARNACLE_HIT, "ACT_BARNACLE_HIT" }, +{ACT_BARNACLE_PULL, "ACT_BARNACLE_PULL" }, +{ACT_BARNACLE_CHOMP, "ACT_BARNACLE_CHOMP" }, +{ACT_BARNACLE_CHEW, "ACT_BARNACLE_CHEW" }, +{ACT_SLEEP, "ACT_SLEEP" }, +{ACT_INSPECT_FLOOR, "ACT_INSPECT_FLOOR" }, +{ACT_INSPECT_WALL, "ACT_INSPECT_WALL" }, +{ACT_IDLE_ANGRY, "ACT_IDLE_ANGRY" }, +{ACT_WALK_HURT, "ACT_WALK_HURT" }, +{ACT_RUN_HURT, "ACT_RUN_HURT" }, +{ACT_HOVER, "ACT_HOVER" }, +{ACT_GLIDE, "ACT_GLIDE" }, +{ACT_FLY_LEFT, "ACT_FLY_LEFT" }, +{ACT_FLY_RIGHT, "ACT_FLY_RIGHT" }, +{ACT_DETECT_SCENT, "ACT_DETECT_SCENT" }, +{ACT_SNIFF, "ACT_SNIFF" }, +{ACT_BITE, "ACT_BITE" }, +{ACT_THREAT_DISPLAY, "ACT_THREAT_DISPLAY" }, +{ACT_FEAR_DISPLAY, "ACT_FEAR_DISPLAY" }, +{ACT_EXCITED, "ACT_EXCITED" }, +{ACT_SPECIAL_ATTACK1, "ACT_SPECIAL_ATTACK1" }, +{ACT_SPECIAL_ATTACK2, "ACT_SPECIAL_ATTACK2" }, +{ACT_COMBAT_IDLE, "ACT_COMBAT_IDLE" }, +{ACT_WALK_SCARED, "ACT_WALK_SCARED" }, +{ACT_RUN_SCARED, "ACT_RUN_SCARED" }, +{ACT_VICTORY_DANCE, "ACT_VICTORY_DANCE" }, +{ACT_DIE_HEADSHOT, "ACT_DIE_HEADSHOT" }, +{ACT_DIE_CHESTSHOT, "ACT_DIE_CHESTSHOT" }, +{ACT_DIE_GUTSHOT, "ACT_DIE_GUTSHOT" }, +{ACT_DIE_BACKSHOT, "ACT_DIE_BACKSHOT" }, +{ACT_FLINCH_HEAD, "ACT_FLINCH_HEAD" }, +{ACT_FLINCH_CHEST, "ACT_FLINCH_CHEST" }, +{ACT_FLINCH_STOMACH, "ACT_FLINCH_STOMACH" }, +{ACT_FLINCH_LEFTARM, "ACT_FLINCH_LEFTARM" }, +{ACT_FLINCH_RIGHTARM, "ACT_FLINCH_RIGHTARM" }, +{ACT_FLINCH_LEFTLEG, "ACT_FLINCH_LEFTLEG" }, +{ACT_FLINCH_RIGHTLEG, "ACT_FLINCH_RIGHTLEG" }, +{ACT_VM_NONE, "ACT_VM_NONE" }, // invalid animation +{ACT_VM_DEPLOY, "ACT_VM_DEPLOY" }, // deploy +{ACT_VM_DEPLOY_EMPTY, "ACT_VM_DEPLOY_EMPTY" }, // deploy empty weapon +{ACT_VM_HOLSTER, "ACT_VM_HOLSTER" }, // holster empty weapon +{ACT_VM_HOLSTER_EMPTY, "ACT_VM_HOLSTER_EMPTY" }, +{ACT_VM_IDLE1, "ACT_VM_IDLE1" }, +{ACT_VM_IDLE2, "ACT_VM_IDLE2" }, +{ACT_VM_IDLE3, "ACT_VM_IDLE3" }, +{ACT_VM_RANGE_ATTACK1, "ACT_VM_RANGE_ATTACK1" }, +{ACT_VM_RANGE_ATTACK2, "ACT_VM_RANGE_ATTACK2" }, +{ACT_VM_RANGE_ATTACK3, "ACT_VM_RANGE_ATTACK3" }, +{ACT_VM_MELEE_ATTACK1, "ACT_VM_MELEE_ATTACK1" }, +{ACT_VM_MELEE_ATTACK2, "ACT_VM_MELEE_ATTACK2" }, +{ACT_VM_MELEE_ATTACK3, "ACT_VM_MELEE_ATTACK3" }, +{ACT_VM_SHOOT_EMPTY, "ACT_VM_SHOOT_EMPTY" }, +{ACT_VM_START_RELOAD, "ACT_VM_START_RELOAD" }, +{ACT_VM_RELOAD, "ACT_VM_RELOAD" }, +{ACT_VM_RELOAD_EMPTY, "ACT_VM_RELOAD_EMPTY" }, +{ACT_VM_TURNON, "ACT_VM_TURNON" }, +{ACT_VM_TURNOFF, "ACT_VM_TURNOFF" }, +{ACT_VM_PUMP, "ACT_VM_PUMP" }, // user animations +{ACT_VM_PUMP_EMPTY, "ACT_VM_PUMP_EMPTY" }, +{ACT_VM_START_CHARGE, "ACT_VM_START_CHARGE" }, +{ACT_VM_CHARGE, "ACT_VM_CHARGE" }, +{ACT_VM_OVERLOAD, "ACT_VM_OVERLOAD" }, +{ACT_VM_IDLE_EMPTY, "ACT_VM_IDLE_EMPTY" }, +{0, NULL }, +}; diff --git a/server/monsters/ai_sound.cpp b/server/monsters/ai_sound.cpp new file mode 100644 index 00000000..ef0533de --- /dev/null +++ b/server/monsters/ai_sound.cpp @@ -0,0 +1,379 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "soundent.h" + + +LINK_ENTITY_TO_CLASS( soundent, CSoundEnt ); + +CSoundEnt *pSoundEnt; + +//========================================================= +// CSound - Clear - zeros all fields for a sound +//========================================================= +void CSound :: Clear ( void ) +{ + m_vecOrigin = g_vecZero; + m_iType = 0; + m_iVolume = 0; + m_flExpireTime = 0; + m_iNext = SOUNDLIST_EMPTY; + m_iNextAudible = 0; +} + +//========================================================= +// Reset - clears the volume, origin, and type for a sound, +// but doesn't expire or unlink it. +//========================================================= +void CSound :: Reset ( void ) +{ + m_vecOrigin = g_vecZero; + m_iType = 0; + m_iVolume = 0; + m_iNext = SOUNDLIST_EMPTY; +} + +//========================================================= +// FIsSound - returns TRUE if the sound is an Audible sound +//========================================================= +BOOL CSound :: FIsSound ( void ) +{ + if ( m_iType & ( bits_SOUND_COMBAT | bits_SOUND_WORLD | bits_SOUND_PLAYER | bits_SOUND_DANGER ) ) + { + return TRUE; + } + + return FALSE; +} + +//========================================================= +// FIsScent - returns TRUE if the sound is actually a scent +//========================================================= +BOOL CSound :: FIsScent ( void ) +{ + if ( m_iType & ( bits_SOUND_CARCASS | bits_SOUND_MEAT | bits_SOUND_GARBAGE ) ) + { + return TRUE; + } + + return FALSE; +} + +//========================================================= +// Spawn +//========================================================= +void CSoundEnt :: Spawn( void ) +{ + pev->solid = SOLID_NOT; + Initialize(); + + SetNextThink( 1 ); +} + +//========================================================= +// Think - at interval, the entire active sound list is checked +// for sounds that have ExpireTimes less than or equal +// to the current world time, and these sounds are deallocated. +//========================================================= +void CSoundEnt :: Think ( void ) +{ + int iSound; + int iPreviousSound; + + SetNextThink( 0.3 );// how often to check the sound list. + + iPreviousSound = SOUNDLIST_EMPTY; + iSound = m_iActiveSound; + + while ( iSound != SOUNDLIST_EMPTY ) + { + if ( m_SoundPool[ iSound ].m_flExpireTime <= gpGlobals->time && m_SoundPool[ iSound ].m_flExpireTime != SOUND_NEVER_EXPIRE ) + { + int iNext = m_SoundPool[ iSound ].m_iNext; + + // move this sound back into the free list + FreeSound( iSound, iPreviousSound ); + + iSound = iNext; + } + else + { + iPreviousSound = iSound; + iSound = m_SoundPool[ iSound ].m_iNext; + } + } + + if ( m_fShowReport ) + { + ALERT ( at_aiconsole, "Soundlist: %d / %d (%d)\n", ISoundsInList( SOUNDLISTTYPE_ACTIVE ),ISoundsInList( SOUNDLISTTYPE_FREE ), ISoundsInList( SOUNDLISTTYPE_ACTIVE ) - m_cLastActiveSounds ); + m_cLastActiveSounds = ISoundsInList ( SOUNDLISTTYPE_ACTIVE ); + } + +} + +//========================================================= +// Precache - dummy function +//========================================================= +void CSoundEnt :: Precache ( void ) +{ +} + +//========================================================= +// FreeSound - clears the passed active sound and moves it +// to the top of the free list. TAKE CARE to only call this +// function for sounds in the Active list!! +//========================================================= +void CSoundEnt :: FreeSound ( int iSound, int iPrevious ) +{ + if ( !pSoundEnt ) + { + // no sound ent! + return; + } + + if ( iPrevious != SOUNDLIST_EMPTY ) + { + // iSound is not the head of the active list, so + // must fix the index for the Previous sound +// pSoundEnt->m_SoundPool[ iPrevious ].m_iNext = m_SoundPool[ iSound ].m_iNext; + pSoundEnt->m_SoundPool[ iPrevious ].m_iNext = pSoundEnt->m_SoundPool[ iSound ].m_iNext; + } + else + { + // the sound we're freeing IS the head of the active list. + pSoundEnt->m_iActiveSound = pSoundEnt->m_SoundPool [ iSound ].m_iNext; + } + + // make iSound the head of the Free list. + pSoundEnt->m_SoundPool[ iSound ].m_iNext = pSoundEnt->m_iFreeSound; + pSoundEnt->m_iFreeSound = iSound; +} + +//========================================================= +// IAllocSound - moves a sound from the Free list to the +// Active list returns the index of the alloc'd sound +//========================================================= +int CSoundEnt :: IAllocSound( void ) +{ + int iNewSound; + + if ( m_iFreeSound == SOUNDLIST_EMPTY ) + { + // no free sound! + ALERT ( at_debug, "Free Sound List is full!\n" ); + return SOUNDLIST_EMPTY; + } + + // there is at least one sound available, so move it to the + // Active sound list, and return its SoundPool index. + + iNewSound = m_iFreeSound;// copy the index of the next free sound + + m_iFreeSound = m_SoundPool[ m_iFreeSound ].m_iNext;// move the index down into the free list. + + m_SoundPool[ iNewSound ].m_iNext = m_iActiveSound;// point the new sound at the top of the active list. + + m_iActiveSound = iNewSound;// now make the new sound the top of the active list. You're done. + + return iNewSound; +} + +//========================================================= +// InsertSound - Allocates a free sound and fills it with +// sound info. +//========================================================= +void CSoundEnt :: InsertSound ( int iType, const Vector &vecOrigin, int iVolume, float flDuration ) +{ + int iThisSound; + + if ( !pSoundEnt ) + { + // no sound ent! + return; + } + + iThisSound = pSoundEnt->IAllocSound(); + + if ( iThisSound == SOUNDLIST_EMPTY ) + { + ALERT ( at_debug, "Could not AllocSound() for InsertSound() (DLL)\n" ); + return; + } + + pSoundEnt->m_SoundPool[ iThisSound ].m_vecOrigin = vecOrigin; + pSoundEnt->m_SoundPool[ iThisSound ].m_iType = iType; + pSoundEnt->m_SoundPool[ iThisSound ].m_iVolume = iVolume; + pSoundEnt->m_SoundPool[ iThisSound ].m_flExpireTime = gpGlobals->time + flDuration; +} + +//========================================================= +// Initialize - clears all sounds and moves them into the +// free sound list. +//========================================================= +void CSoundEnt :: Initialize ( void ) +{ + int i; + int iSound; + + m_cLastActiveSounds; + m_iFreeSound = 0; + m_iActiveSound = SOUNDLIST_EMPTY; + + for ( i = 0 ; i < MAX_WORLD_SOUNDS ; i++ ) + {// clear all sounds, and link them into the free sound list. + m_SoundPool[ i ].Clear(); + m_SoundPool[ i ].m_iNext = i + 1; + } + + m_SoundPool[ i - 1 ].m_iNext = SOUNDLIST_EMPTY;// terminate the list here. + + + // now reserve enough sounds for each client + for ( i = 0 ; i < gpGlobals->maxClients ; i++ ) + { + iSound = pSoundEnt->IAllocSound(); + + if ( iSound == SOUNDLIST_EMPTY ) + { + ALERT ( at_debug, "Could not AllocSound() for Client Reserve! (DLL)\n" ); + return; + } + + pSoundEnt->m_SoundPool[ iSound ].m_flExpireTime = SOUND_NEVER_EXPIRE; + } + + if ( CVAR_GET_FLOAT("displaysoundlist") == 1 ) + { + m_fShowReport = TRUE; + } + else + { + m_fShowReport = FALSE; + } +} + +//========================================================= +// ISoundsInList - returns the number of sounds in the desired +// sound list. +//========================================================= +int CSoundEnt :: ISoundsInList ( int iListType ) +{ + int i; + int iThisSound; + + if ( iListType == SOUNDLISTTYPE_FREE ) + { + iThisSound = m_iFreeSound; + } + else if ( iListType == SOUNDLISTTYPE_ACTIVE ) + { + iThisSound = m_iActiveSound; + } + else + { + ALERT ( at_debug, "Unknown Sound List Type!\n" ); + } + + if ( iThisSound == SOUNDLIST_EMPTY ) + { + return 0; + } + + i = 0; + + while ( iThisSound != SOUNDLIST_EMPTY ) + { + i++; + + iThisSound = m_SoundPool[ iThisSound ].m_iNext; + } + + return i; +} + +//========================================================= +// ActiveList - returns the head of the active sound list +//========================================================= +int CSoundEnt :: ActiveList ( void ) +{ + if ( !pSoundEnt ) + { + return SOUNDLIST_EMPTY; + } + + return pSoundEnt->m_iActiveSound; +} + +//========================================================= +// FreeList - returns the head of the free sound list +//========================================================= +int CSoundEnt :: FreeList ( void ) +{ + if ( !pSoundEnt ) + { + return SOUNDLIST_EMPTY; + } + + return pSoundEnt->m_iFreeSound; +} + +//========================================================= +// SoundPointerForIndex - returns a pointer to the instance +// of CSound at index's position in the sound pool. +//========================================================= +CSound* CSoundEnt :: SoundPointerForIndex( int iIndex ) +{ + if ( !pSoundEnt ) + { + return NULL; + } + + if ( iIndex > ( MAX_WORLD_SOUNDS - 1 ) ) + { + ALERT ( at_debug, "SoundPointerForIndex() - Index too large!\n" ); + return NULL; + } + + if ( iIndex < 0 ) + { + ALERT ( at_debug, "SoundPointerForIndex() - Index < 0!\n" ); + return NULL; + } + + return &pSoundEnt->m_SoundPool[ iIndex ]; +} + +//========================================================= +// Clients are numbered from 1 to MAXCLIENTS, but the client +// reserved sounds in the soundlist are from 0 to MAXCLIENTS - 1, +// so this function ensures that a client gets the proper index +// to his reserved sound in the soundlist. +//========================================================= +int CSoundEnt :: ClientSoundIndex ( edict_t *pClient ) +{ + int iReturn = ENTINDEX( pClient ) - 1; + +#ifdef _DEBUG + if ( iReturn < 0 || iReturn > gpGlobals->maxClients ) + { + ALERT ( at_debug, "** ClientSoundIndex returning a bogus value! **\n" ); + } +#endif // _DEBUG + + return iReturn; +} diff --git a/server/monsters/animating.cpp b/server/monsters/animating.cpp new file mode 100644 index 00000000..cfdb9250 --- /dev/null +++ b/server/monsters/animating.cpp @@ -0,0 +1,342 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +/* + +===== monsters.cpp ======================================================== + + Monster-related utility code + +*/ + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "animation.h" +#include "saverestore.h" + +TYPEDESCRIPTION CBaseAnimating::m_SaveData[] = +{ + DEFINE_FIELD( CBaseMonster, m_flFrameRate, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMonster, m_flGroundSpeed, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMonster, m_flLastEventCheck, FIELD_TIME ), + DEFINE_FIELD( CBaseMonster, m_fSequenceFinished, FIELD_BOOLEAN ), + DEFINE_FIELD( CBaseMonster, m_fSequenceLoops, FIELD_BOOLEAN ), +}; + +IMPLEMENT_SAVERESTORE( CBaseAnimating, CBaseLogic ); + + +//========================================================= +// StudioFrameAdvance - advance the animation frame up to the current time +// if an flInterval is passed in, only advance animation that number of seconds +//========================================================= +float CBaseAnimating :: StudioFrameAdvance ( float flInterval ) +{ + if (flInterval == 0.0) + { + flInterval = (gpGlobals->time - pev->animtime); + if (flInterval <= 0.001) + { + pev->animtime = gpGlobals->time; + return 0.0; + } + } + if (! pev->animtime) + flInterval = 0.0; + + pev->frame += flInterval * m_flFrameRate * pev->framerate; + pev->animtime = gpGlobals->time; + + if (pev->frame < 0.0 || pev->frame >= 256.0) + { + if (m_fSequenceLoops) + pev->frame -= (int)(pev->frame / 256.0) * 256.0; + else + pev->frame = (pev->frame < 0.0) ? 0 : 255; + m_fSequenceFinished = TRUE; // just in case it wasn't caught in GetEvents + } + + return flInterval; +} + +float CBaseAnimating :: SequenceDuration( int iSequence ) +{ + dstudiohdr_t *pstudiohdr; + void *pmodel = GET_MODEL_PTR(ENT(pev)); + pstudiohdr = (dstudiohdr_t *)pmodel; + + if (!pstudiohdr) + return 0; + + dstudioseqdesc_t *pseqdesc; + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + iSequence; + return (float)pseqdesc->numframes/(float)pseqdesc->fps; +} + +//========================================================= +// LookupActivity +//========================================================= +int CBaseAnimating :: LookupActivity ( int activity ) +{ + ASSERT( activity != 0 ); + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + return ::LookupActivity( pmodel, pev, activity ); +} + +//========================================================= +// LookupActivityHeaviest +// +// Get activity with highest 'weight' +// +//========================================================= +int CBaseAnimating :: LookupActivityHeaviest ( int activity ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + return ::LookupActivityHeaviest( pmodel, pev, activity ); +} + +//========================================================= +//========================================================= +int CBaseAnimating :: LookupSequence ( const char *label ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + return ::LookupSequence( pmodel, label ); +} + + +//========================================================= +//========================================================= +void CBaseAnimating :: ResetSequenceInfo ( ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + GetSequenceInfo( pmodel, pev, &m_flFrameRate, &m_flGroundSpeed ); + m_fSequenceLoops = ((GetSequenceFlags() & STUDIO_LOOPING) != 0); + pev->animtime = gpGlobals->time; + pev->framerate = 1.0; + m_fSequenceFinished = FALSE; + m_flLastEventCheck = gpGlobals->time; +} + + + +//========================================================= +//========================================================= +BOOL CBaseAnimating :: GetSequenceFlags( ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + return ::GetSequenceFlags( pmodel, pev ); +} + +//========================================================= +// DispatchAnimEvents +//========================================================= +void CBaseAnimating :: DispatchAnimEvents ( float flInterval ) +{ + MonsterEvent_t event; + + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + if ( !pmodel ) + { + ALERT( at_aiconsole, "Gibbed monster is thinking!\n" ); + return; + } + + // FIXME: I have to do this or some events get missed, and this is probably causing the problem below + flInterval = 0.1; + + // FIX: this still sometimes hits events twice + float flStart = pev->frame + (m_flLastEventCheck - pev->animtime) * m_flFrameRate * pev->framerate; + float flEnd = pev->frame + flInterval * m_flFrameRate * pev->framerate; + m_flLastEventCheck = pev->animtime + flInterval; + + m_fSequenceFinished = FALSE; + if (flEnd >= 256 || flEnd <= 0.0) + m_fSequenceFinished = TRUE; + + int index = 0; + + while ( (index = GetAnimationEvent( pmodel, pev, &event, flStart, flEnd, index ) ) != 0 ) + { + HandleAnimEvent( &event ); + } +} + + +//========================================================= +//========================================================= +float CBaseAnimating :: SetBoneController ( int iController, float flValue ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + return SetController( pmodel, pev, iController, flValue ); +} + +//========================================================= +//========================================================= +void CBaseAnimating :: InitBoneControllers ( void ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + SetController( pmodel, pev, 0, 0.0 ); + SetController( pmodel, pev, 1, 0.0 ); + SetController( pmodel, pev, 2, 0.0 ); + SetController( pmodel, pev, 3, 0.0 ); +} + +//========================================================= +//========================================================= +float CBaseAnimating :: SetBlending ( int iBlender, float flValue ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + return ::SetBlending( pmodel, pev, iBlender, flValue ); +} + +//========================================================= +//========================================================= +void CBaseAnimating :: GetBonePosition ( int iBone, Vector &origin, Vector &angles ) +{ + GET_BONE_POSITION( ENT(pev), iBone, origin, angles ); +} + +//========================================================= +//========================================================= +BOOL CBaseAnimating :: GetAttachment ( int iAttachment, Vector &origin, Vector &angles ) +{ + GET_ATTACHMENT( ENT(pev), iAttachment, origin, angles ); + return TRUE; +} + +//========================================================= +//========================================================= +int CBaseAnimating :: FindTransition( int iEndingSequence, int iGoalSequence, int *piDir ) +{ + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + if (piDir == NULL) + { + int iDir; + int sequence = ::FindTransition( pmodel, iEndingSequence, iGoalSequence, &iDir ); + if (iDir != 1) + return -1; + else + return sequence; + } + + return ::FindTransition( pmodel, iEndingSequence, iGoalSequence, piDir ); +} + +//========================================================= +//========================================================= +void CBaseAnimating :: GetAutomovement( Vector &origin, Vector &angles, float flInterval ) +{ + +} + +void CBaseAnimating :: SetBodygroup( int iGroup, int iValue ) +{ + ::SetBodygroup( GET_MODEL_PTR( ENT(pev) ), pev, iGroup, iValue ); +} + +int CBaseAnimating :: GetBodygroup( int iGroup ) +{ + return ::GetBodygroup( GET_MODEL_PTR( ENT(pev) ), pev, iGroup ); +} + +int CBaseAnimating :: GetBoneCount( void ) +{ + return ::GetBoneCount( GET_MODEL_PTR(ENT(pev)) ); +} + +void CBaseAnimating :: SetBones( float (*data)[3], int datasize ) +{ + ::SetBones( GET_MODEL_PTR( ENT(pev) ), data, datasize ); +} + +int CBaseAnimating :: ExtractBbox( int sequence, float *mins, float *maxs ) +{ + return ::ExtractBbox( GET_MODEL_PTR( ENT(pev) ), sequence, mins, maxs ); +} + +//========================================================= +//========================================================= + +void CBaseAnimating :: SetSequenceBox( void ) +{ + Vector mins, maxs; + + // Get sequence bbox + if ( ExtractBbox( pev->sequence, mins, maxs ) ) + { + // expand box for rotation + // find min / max for rotations + float yaw = pev->angles.y * (M_PI / 180.0); + + Vector xvector, yvector; + xvector.x = cos(yaw); + xvector.y = sin(yaw); + yvector.x = -sin(yaw); + yvector.y = cos(yaw); + Vector bounds[2]; + + bounds[0] = mins; + bounds[1] = maxs; + + Vector rmin( 9999, 9999, 9999 ); + Vector rmax( -9999, -9999, -9999 ); + Vector base, transformed; + + for (int i = 0; i <= 1; i++ ) + { + base.x = bounds[i].x; + for ( int j = 0; j <= 1; j++ ) + { + base.y = bounds[j].y; + for ( int k = 0; k <= 1; k++ ) + { + base.z = bounds[k].z; + + // transform the point + transformed.x = xvector.x*base.x + yvector.x*base.y; + transformed.y = xvector.y*base.x + yvector.y*base.y; + transformed.z = base.z; + + if (transformed.x < rmin.x) + rmin.x = transformed.x; + if (transformed.x > rmax.x) + rmax.x = transformed.x; + if (transformed.y < rmin.y) + rmin.y = transformed.y; + if (transformed.y > rmax.y) + rmax.y = transformed.y; + if (transformed.z < rmin.z) + rmin.z = transformed.z; + if (transformed.z > rmax.z) + rmax.z = transformed.z; + } + } + } + rmin.z = 0; + rmax.z = rmin.z + 1; + UTIL_SetSize( pev, rmin, rmax ); + } +} + diff --git a/server/monsters/animation.cpp b/server/monsters/animation.cpp new file mode 100644 index 00000000..ab4499b5 --- /dev/null +++ b/server/monsters/animation.cpp @@ -0,0 +1,562 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#include +#include +#include + +#include "extdll.h" +#include "utils.h" +#include "const.h" +#include "qfiles_ref.h" + +#ifndef ACTIVITY_H +#include "activity.h" +#endif + +#include "activitymap.h" + +#ifndef ANIMATION_H +#include "animation.h" +#endif + +#ifndef SCRIPTEVENT_H +#include "scriptevent.h" +#endif + +#ifndef ENGINECALLBACK_H +#include "enginecallback.h" +#endif + +extern globalvars_t *gpGlobals; + +#pragma warning( disable : 4244 ) + + + +int ExtractBbox( void *pmodel, int sequence, float *mins, float *maxs ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return 0; + + dstudioseqdesc_t *pseqdesc; + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex); + + mins[0] = pseqdesc[ sequence ].bbmin[0]; + mins[1] = pseqdesc[ sequence ].bbmin[1]; + mins[2] = pseqdesc[ sequence ].bbmin[2]; + + maxs[0] = pseqdesc[ sequence ].bbmax[0]; + maxs[1] = pseqdesc[ sequence ].bbmax[1]; + maxs[2] = pseqdesc[ sequence ].bbmax[2]; + + return 1; +} + + +int LookupActivity( void *pmodel, entvars_t *pev, int activity ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return 0; + + dstudioseqdesc_t *pseqdesc; + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex); + + int weighttotal = 0; + int seq = ACTIVITY_NOT_AVAILABLE; + for (int i = 0; i < pstudiohdr->numseq; i++) + { + if (pseqdesc[i].activity == activity) + { + weighttotal += pseqdesc[i].actweight; + if (!weighttotal || RANDOM_LONG(0,weighttotal-1) < pseqdesc[i].actweight) + seq = i; + } + } + + return seq; +} + + +int LookupActivityHeaviest( void *pmodel, entvars_t *pev, int activity ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if ( !pstudiohdr ) + return 0; + + dstudioseqdesc_t *pseqdesc; + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex); + + int weight = 0; + int seq = ACTIVITY_NOT_AVAILABLE; + for (int i = 0; i < pstudiohdr->numseq; i++) + { + if (pseqdesc[i].activity == activity) + { + if ( pseqdesc[i].actweight > weight ) + { + weight = pseqdesc[i].actweight; + seq = i; + } + } + } + + return seq; +} + +void GetEyePosition ( void *pmodel, float *vecEyePosition ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + + if ( !pstudiohdr ) + { + ALERT ( at_console, "GetEyePosition() Can't get pstudiohdr ptr!\n" ); + return; + } + + vecEyePosition = pstudiohdr->eyeposition; +} + +int LookupSequence( void *pmodel, const char *label ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return 0; + + dstudioseqdesc_t *pseqdesc; + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex); + + for (int i = 0; i < pstudiohdr->numseq; i++) + { + if (stricmp( pseqdesc[i].label, label ) == 0) + return i; + } + + return -1; +} + + +int IsSoundEvent( int eventNumber ) +{ + if ( eventNumber == SCRIPT_EVENT_SOUND || eventNumber == SCRIPT_EVENT_SOUND_VOICE ) + return 1; + return 0; +} + + +void SequencePrecache( void *pmodel, const char *pSequenceName ) +{ + int index = LookupSequence( pmodel, pSequenceName ); + if ( index >= 0 ) + { + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if ( !pstudiohdr || index >= pstudiohdr->numseq ) + return; + + dstudioseqdesc_t *pseqdesc; + dstudioevent_t *pevent; + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + index; + pevent = (dstudioevent_t *)((byte *)pstudiohdr + pseqdesc->eventindex); + + for (int i = 0; i < pseqdesc->numevents; i++) + { + // Don't send client-side events to the server AI + if ( pevent[i].event >= EVENT_CLIENT ) + continue; + + // UNDONE: Add a callback to check to see if a sound is precached yet and don't allocate a copy + // of it's name if it is. + if ( IsSoundEvent( pevent[i].event ) ) + { + if ( !strlen(pevent[i].options) ) + { + ALERT( at_error, "Bad sound event %d in sequence %s :: %s (sound is \"%s\")\n", pevent[i].event, pstudiohdr->name, pSequenceName, pevent[i].options ); + } + + PRECACHE_SOUND( STRING( ALLOC_STRING(pevent[i].options)) ); + } + } + } +} + + + +void GetSequenceInfo( void *pmodel, entvars_t *pev, float *pflFrameRate, float *pflGroundSpeed ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return; + + dstudioseqdesc_t *pseqdesc; + + if (pev->sequence >= pstudiohdr->numseq) + { + *pflFrameRate = 0.0; + *pflGroundSpeed = 0.0; + return; + } + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + (int)pev->sequence; + + if (pseqdesc->numframes > 1) + { + *pflFrameRate = 256 * pseqdesc->fps / (pseqdesc->numframes - 1); + *pflGroundSpeed = sqrt( pseqdesc->linearmovement[0]*pseqdesc->linearmovement[0]+ pseqdesc->linearmovement[1]*pseqdesc->linearmovement[1]+ pseqdesc->linearmovement[2]*pseqdesc->linearmovement[2] ); + *pflGroundSpeed = *pflGroundSpeed * pseqdesc->fps / (pseqdesc->numframes - 1); + } + else + { + *pflFrameRate = 256.0; + *pflGroundSpeed = 0.0; + } +} + + +int GetSequenceFlags( void *pmodel, entvars_t *pev ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if ( !pstudiohdr || pev->sequence >= pstudiohdr->numseq ) + return 0; + + dstudioseqdesc_t *pseqdesc; + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + (int)pev->sequence; + + return pseqdesc->flags; +} + + +int GetAnimationEvent( void *pmodel, entvars_t *pev, MonsterEvent_t *pMonsterEvent, float flStart, float flEnd, int index ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if ( !pstudiohdr || pev->sequence >= pstudiohdr->numseq || !pMonsterEvent ) + return 0; + + int events = 0; + + dstudioseqdesc_t *pseqdesc; + dstudioevent_t *pevent; + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + (int)pev->sequence; + pevent = (dstudioevent_t *)((byte *)pstudiohdr + pseqdesc->eventindex); + + if (pseqdesc->numevents == 0 || index > pseqdesc->numevents ) + return 0; + + if (pseqdesc->numframes > 1) + { + flStart *= (pseqdesc->numframes - 1) / 256.0; + flEnd *= (pseqdesc->numframes - 1) / 256.0; + } + else + { + flStart = 0; + flEnd = 1.0; + } + + for (; index < pseqdesc->numevents; index++) + { + // Don't send client-side events to the server AI + if ( pevent[index].event >= EVENT_CLIENT ) + continue; + + if ( (pevent[index].frame >= flStart && pevent[index].frame < flEnd) || + ((pseqdesc->flags & STUDIO_LOOPING) && flEnd >= pseqdesc->numframes - 1 && pevent[index].frame < flEnd - pseqdesc->numframes + 1) ) + { + pMonsterEvent->event = pevent[index].event; + pMonsterEvent->options = pevent[index].options; + return index + 1; + } + } + return 0; +} + +float SetController( void *pmodel, entvars_t *pev, int iController, float flValue ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return flValue; + + dstudiobonecontroller_t *pbonecontroller = (dstudiobonecontroller_t *)((byte *)pstudiohdr + pstudiohdr->bonecontrollerindex); + + // find first controller that matches the index + int i = 0; + for (i = 0; i < pstudiohdr->numbonecontrollers; i++, pbonecontroller++) + { + if (pbonecontroller->index == iController) + break; + } + if (i >= pstudiohdr->numbonecontrollers) + return flValue; + + // wrap 0..360 if it's a rotational controller + + if (pbonecontroller->type & (STUDIO_XR | STUDIO_YR | STUDIO_ZR)) + { + // ugly hack, invert value if end < start + if (pbonecontroller->end < pbonecontroller->start) + flValue = -flValue; + + // does the controller not wrap? + if (pbonecontroller->start + 359.0 >= pbonecontroller->end) + { + if (flValue > ((pbonecontroller->start + pbonecontroller->end) / 2.0) + 180) + flValue = flValue - 360; + if (flValue < ((pbonecontroller->start + pbonecontroller->end) / 2.0) - 180) + flValue = flValue + 360; + } + else + { + if (flValue > 360) + flValue = flValue - (int)(flValue / 360.0) * 360.0; + else if (flValue < 0) + flValue = flValue + (int)((flValue / -360.0) + 1) * 360.0; + } + } + + int setting = 255 * (flValue - pbonecontroller->start) / (pbonecontroller->end - pbonecontroller->start); + + if (setting < 0) setting = 0; + if (setting > 255) setting = 255; + pev->controller[iController] = setting; + + return setting * (1.0 / 255.0) * (pbonecontroller->end - pbonecontroller->start) + pbonecontroller->start; +} + + +float SetBlending( void *pmodel, entvars_t *pev, int iBlender, float flValue ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return flValue; + + dstudioseqdesc_t *pseqdesc; + + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + (int)pev->sequence; + + if (pseqdesc->blendtype[iBlender] == 0) + return flValue; + + if (pseqdesc->blendtype[iBlender] & (STUDIO_XR | STUDIO_YR | STUDIO_ZR)) + { + // ugly hack, invert value if end < start + if (pseqdesc->blendend[iBlender] < pseqdesc->blendstart[iBlender]) + flValue = -flValue; + + // does the controller not wrap? + if (pseqdesc->blendstart[iBlender] + 359.0 >= pseqdesc->blendend[iBlender]) + { + if (flValue > ((pseqdesc->blendstart[iBlender] + pseqdesc->blendend[iBlender]) / 2.0) + 180) + flValue = flValue - 360; + if (flValue < ((pseqdesc->blendstart[iBlender] + pseqdesc->blendend[iBlender]) / 2.0) - 180) + flValue = flValue + 360; + } + } + + int setting = 255 * (flValue - pseqdesc->blendstart[iBlender]) / (pseqdesc->blendend[iBlender] - pseqdesc->blendstart[iBlender]); + + if (setting < 0) setting = 0; + if (setting > 255) setting = 255; + + pev->blending[iBlender] = setting; + + return setting * (1.0 / 255.0) * (pseqdesc->blendend[iBlender] - pseqdesc->blendstart[iBlender]) + pseqdesc->blendstart[iBlender]; +} + + + + +int FindTransition( void *pmodel, int iEndingAnim, int iGoalAnim, int *piDir ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return iGoalAnim; + + dstudioseqdesc_t *pseqdesc; + pseqdesc = (dstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex); + + // bail if we're going to or from a node 0 + if (pseqdesc[iEndingAnim].entrynode == 0 || pseqdesc[iGoalAnim].entrynode == 0) + { + return iGoalAnim; + } + + int iEndNode; + + // ALERT( at_console, "from %d to %d: ", pEndNode->iEndNode, pGoalNode->iStartNode ); + + if (*piDir > 0) + { + iEndNode = pseqdesc[iEndingAnim].exitnode; + } + else + { + iEndNode = pseqdesc[iEndingAnim].entrynode; + } + + if (iEndNode == pseqdesc[iGoalAnim].entrynode) + { + *piDir = 1; + return iGoalAnim; + } + + byte *pTransition = ((byte *)pstudiohdr + pstudiohdr->transitionindex); + + int iInternNode = pTransition[(iEndNode-1)*pstudiohdr->numtransitions + (pseqdesc[iGoalAnim].entrynode-1)]; + + if (iInternNode == 0) + return iGoalAnim; + + int i; + + // look for someone going + for (i = 0; i < pstudiohdr->numseq; i++) + { + if (pseqdesc[i].entrynode == iEndNode && pseqdesc[i].exitnode == iInternNode) + { + *piDir = 1; + return i; + } + if (pseqdesc[i].nodeflags) + { + if (pseqdesc[i].exitnode == iEndNode && pseqdesc[i].entrynode == iInternNode) + { + *piDir = -1; + return i; + } + } + } + + ALERT( at_debug, "error in transition graph" ); + return iGoalAnim; +} + +void SetBodygroup( void *pmodel, entvars_t *pev, int iGroup, int iValue ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return; + + if (iGroup > pstudiohdr->numbodyparts) + return; + + dstudiobodyparts_t *pbodypart = (dstudiobodyparts_t *)((byte *)pstudiohdr + pstudiohdr->bodypartindex) + iGroup; + + if (iValue >= pbodypart->nummodels) + return; + + int iCurrent = (pev->body / pbodypart->base) % pbodypart->nummodels; + + pev->body = (pev->body - (iCurrent * pbodypart->base) + (iValue * pbodypart->base)); +} + + +int GetBodygroup( void *pmodel, entvars_t *pev, int iGroup ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (! pstudiohdr) + return 0; + + if (iGroup > pstudiohdr->numbodyparts) + return 0; + + dstudiobodyparts_t *pbodypart = (dstudiobodyparts_t *)((byte *)pstudiohdr + pstudiohdr->bodypartindex) + iGroup; + + if (pbodypart->nummodels <= 1) + return 0; + + int iCurrent = (pev->body / pbodypart->base) % pbodypart->nummodels; + + return iCurrent; +} + +//LRC +int GetBoneCount( void *pmodel ) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (!pstudiohdr) + { + ALERT(at_error, "Bad header in SetBones!\n"); + return 0; + } + + return pstudiohdr->numbones; +} + +#ifndef min +#define min(a,b) (((a) < (b)) ? (a) : (b)) +#endif + +//LRC +void SetBones( void *pmodel, float (*data)[3], int datasize) +{ + dstudiohdr_t *pstudiohdr; + + pstudiohdr = (dstudiohdr_t *)pmodel; + if (!pstudiohdr) + { + ALERT(at_error, "Bad header in SetBones!\n"); + return; + } + + dstudiobone_t *pbone = (dstudiobone_t *)((byte *)pstudiohdr + pstudiohdr->boneindex); + +// ALERT(at_console, "List begins:\n"); + int j; + int limit = min(pstudiohdr->numbones, datasize); + // go through the bones + for (int i = 0; i < limit; i++, pbone++) + { +// ALERT(at_console, " %s\n", pbone->name); + for (j = 0; j < 3; j++) + pbone->value[j] = data[i][j]; + } +// ALERT(at_console, "List ends.\n"); +} diff --git a/server/monsters/animation.h b/server/monsters/animation.h new file mode 100644 index 00000000..cf90952a --- /dev/null +++ b/server/monsters/animation.h @@ -0,0 +1,52 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef ANIMATION_H +#define ANIMATION_H + +#define ACTIVITY_NOT_AVAILABLE -1 + +#ifndef MONSTEREVENT_H +#include "monsterevent.h" +#endif + +extern int IsSoundEvent( int eventNumber ); + +int LookupActivity( void *pmodel, entvars_t *pev, int activity ); +int LookupActivityHeaviest( void *pmodel, entvars_t *pev, int activity ); +int LookupSequence( void *pmodel, const char *label ); +void GetSequenceInfo( void *pmodel, entvars_t *pev, float *pflFrameRate, float *pflGroundSpeed ); +int GetSequenceFlags( void *pmodel, entvars_t *pev ); +int LookupAnimationEvents( void *pmodel, entvars_t *pev, float flStart, float flEnd ); +float SetController( void *pmodel, entvars_t *pev, int iController, float flValue ); +float SetBlending( void *pmodel, entvars_t *pev, int iBlender, float flValue ); +void GetEyePosition( void *pmodel, float *vecEyePosition ); +void SequencePrecache( void *pmodel, const char *pSequenceName ); +int FindTransition( void *pmodel, int iEndingAnim, int iGoalAnim, int *piDir ); +void SetBodygroup( void *pmodel, entvars_t *pev, int iGroup, int iValue ); +int GetBodygroup( void *pmodel, entvars_t *pev, int iGroup ); + +//LRC +void SetBones( void *pmodel, float (*data)[3], int datasize ); +int GetBoneCount( void *pmodel ); +int GetSequenceFrames( void *pmodel, entvars_t *pev ); //LRC + +int GetAnimationEvent( void *pmodel, entvars_t *pev, MonsterEvent_t *pMonsterEvent, float flStart, float flEnd, int index ); +int ExtractBbox( void *pmodel, int sequence, float *mins, float *maxs ); + +// From /engine/studio.h +#define STUDIO_LOOPING 0x0001 + + +#endif //ANIMATION_H diff --git a/server/monsters/apache.cpp b/server/monsters/apache.cpp new file mode 100644 index 00000000..4f8b532d --- /dev/null +++ b/server/monsters/apache.cpp @@ -0,0 +1,941 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#ifndef OEM_BUILD + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "baseweapon.h" +#include "nodes.h" +#include "basebeams.h" +#include "defaults.h" + +#define SF_WAITFORTRIGGER (0x04 | 0x40) // UNDONE: Fix! +#define SF_NOWRECKAGE 0x08 + +class CApache : public CBaseMonster +{ + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + void Spawn( void ); + void Precache( void ); + int Classify( void ) { return CLASS_HUMAN_MILITARY; }; + int BloodColor( void ) { return DONT_BLEED; } + void Killed( entvars_t *pevAttacker, int iGib ); + void GibMonster( void ); + + void SetObjectCollisionBox( void ) + { + pev->absmin = pev->origin + Vector( -300, -300, -172); + pev->absmax = pev->origin + Vector(300, 300, 8); + } + + void EXPORT HuntThink( void ); + void EXPORT FlyTouch( CBaseEntity *pOther ); + void EXPORT CrashTouch( CBaseEntity *pOther ); + void EXPORT DyingThink( void ); + void EXPORT StartupUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT NullThink( void ); + + void ShowDamage( void ); + void Flight( void ); + void FireRocket( void ); + BOOL FireGun( void ); + + int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ); + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + + int m_iRockets; + float m_flForce; + float m_flNextRocket; + + Vector m_vecTarget; + Vector m_posTarget; + + Vector m_vecDesired; + Vector m_posDesired; + + Vector m_vecGoal; + + Vector m_angGun; + float m_flLastSeen; + float m_flPrevSeen; + + int m_iSoundState; // don't save this + + int m_iSpriteTexture; + int m_iExplode; + int m_iBodyGibs; + + float m_flGoalSpeed; + + int m_iDoSmokePuff; + CBeam *m_pBeam; +}; +LINK_ENTITY_TO_CLASS( monster_apache, CApache ); + +TYPEDESCRIPTION CApache::m_SaveData[] = +{ + DEFINE_FIELD( CApache, m_iRockets, FIELD_INTEGER ), + DEFINE_FIELD( CApache, m_flForce, FIELD_FLOAT ), + DEFINE_FIELD( CApache, m_flNextRocket, FIELD_TIME ), + DEFINE_FIELD( CApache, m_vecTarget, FIELD_VECTOR ), + DEFINE_FIELD( CApache, m_posTarget, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CApache, m_vecDesired, FIELD_VECTOR ), + DEFINE_FIELD( CApache, m_posDesired, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CApache, m_vecGoal, FIELD_VECTOR ), + DEFINE_FIELD( CApache, m_angGun, FIELD_VECTOR ), + DEFINE_FIELD( CApache, m_flLastSeen, FIELD_TIME ), + DEFINE_FIELD( CApache, m_flPrevSeen, FIELD_TIME ), +// DEFINE_FIELD( CApache, m_iSoundState, FIELD_INTEGER ), // Don't save, precached +// DEFINE_FIELD( CApache, m_iSpriteTexture, FIELD_INTEGER ), +// DEFINE_FIELD( CApache, m_iExplode, FIELD_INTEGER ), +// DEFINE_FIELD( CApache, m_iBodyGibs, FIELD_INTEGER ), + DEFINE_FIELD( CApache, m_pBeam, FIELD_CLASSPTR ), + DEFINE_FIELD( CApache, m_flGoalSpeed, FIELD_FLOAT ), + DEFINE_FIELD( CApache, m_iDoSmokePuff, FIELD_INTEGER ), +}; +IMPLEMENT_SAVERESTORE( CApache, CBaseMonster ); + + +void CApache :: Spawn( void ) +{ + Precache( ); + // motor + pev->movetype = MOVETYPE_FLY; + pev->solid = SOLID_BBOX; + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/apache.mdl"); + UTIL_SetSize( pev, Vector( -32, -32, -64 ), Vector( 32, 32, 0 ) ); + UTIL_SetOrigin( this, pev->origin ); + + pev->flags |= FL_MONSTER; + pev->takedamage = DAMAGE_AIM; + if (pev->health == 0) + pev->health = APACHE_HEALTH; + + m_flFieldOfView = -0.707; // 270 degrees + + pev->sequence = 0; + ResetSequenceInfo( ); + pev->frame = RANDOM_LONG(0, 0xFF); + + InitBoneControllers(); + + if (pev->spawnflags & SF_WAITFORTRIGGER) + { + SetUse(&CApache :: StartupUse ); + } + else + { + SetThink(&CApache :: HuntThink ); + SetTouch(&CApache :: FlyTouch ); + SetNextThink( 1.0 ); + } + + m_iRockets = 10; +} + + +void CApache::Precache( void ) +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/apache.mdl"); + + PRECACHE_SOUND("apache/ap_rotor1.wav"); + PRECACHE_SOUND("apache/ap_rotor2.wav"); + PRECACHE_SOUND("apache/ap_rotor3.wav"); + PRECACHE_SOUND("apache/ap_whine1.wav"); + + PRECACHE_SOUND("weapons/mortarhit.wav"); + + m_iSpriteTexture = PRECACHE_MODEL( "sprites/white.spr" ); + + PRECACHE_SOUND("turret/tu_fire1.wav"); + + PRECACHE_MODEL("sprites/lgtning.spr"); + + m_iExplode = PRECACHE_MODEL( "sprites/fexplo.spr" ); + m_iBodyGibs = PRECACHE_MODEL( "models/metalplategibs_green.mdl" ); + + UTIL_PrecacheEntity( "hvr_rocket" ); +} + + + +void CApache::NullThink( void ) +{ + StudioFrameAdvance( ); + SetNextThink( 0.5 ); +} + + +void CApache::StartupUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + SetThink(&CApache:: HuntThink ); + SetTouch(&CApache:: FlyTouch ); + SetNextThink( 0.1 ); + SetUse( NULL ); +} + +void CApache :: Killed( entvars_t *pevAttacker, int iGib ) +{ + pev->movetype = MOVETYPE_TOSS; + pev->gravity = 0.3; + + STOP_SOUND( ENT(pev), CHAN_STATIC, "apache/ap_rotor2.wav" ); + + UTIL_SetSize( pev, Vector( -32, -32, -64), Vector( 32, 32, 0) ); + SetThink(&CApache :: DyingThink ); + SetTouch(&CApache :: CrashTouch ); + SetNextThink( 0.1 ); + pev->health = 0; + pev->takedamage = DAMAGE_NO; + + if (pev->spawnflags & SF_NOWRECKAGE) + { + m_flNextRocket = gpGlobals->time + 4.0; + } + else + { + m_flNextRocket = gpGlobals->time + 15.0; + } +} + +void CApache :: DyingThink( void ) +{ + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + pev->avelocity = pev->avelocity * 1.02; + + // still falling? + if (m_flNextRocket > gpGlobals->time ) + { + // random explosions + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, pev->origin ); + WRITE_BYTE( TE_EXPLOSION); // This just makes a dynamic light now + WRITE_COORD( pev->origin.x + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( pev->origin.y + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( pev->origin.z + RANDOM_FLOAT( -150, -50 )); + WRITE_SHORT( g_sModelIndexFireball ); + WRITE_BYTE( RANDOM_LONG(0,29) + 30 ); // scale * 10 + WRITE_BYTE( 12 ); // framerate + WRITE_BYTE( TE_EXPLFLAG_NONE ); + MESSAGE_END(); + + // lots of smoke + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, pev->origin ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( pev->origin.x + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( pev->origin.y + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( pev->origin.z + RANDOM_FLOAT( -150, -50 )); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( 100 ); // scale * 10 + WRITE_BYTE( 10 ); // framerate + MESSAGE_END(); + + Vector vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_BREAKMODEL); + + // position + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z ); + + // size + WRITE_COORD( 400 ); + WRITE_COORD( 400 ); + WRITE_COORD( 132 ); + + // velocity + WRITE_COORD( pev->velocity.x ); + WRITE_COORD( pev->velocity.y ); + WRITE_COORD( pev->velocity.z ); + + // randomization + WRITE_BYTE( 50 ); + + // Model + WRITE_SHORT( m_iBodyGibs ); //model id# + + // # of shards + WRITE_BYTE( 4 ); // let client decide + + // duration + WRITE_BYTE( 30 );// 3.0 seconds + + // flags + + WRITE_BYTE( BREAK_METAL ); + MESSAGE_END(); + + // don't stop it we touch a entity + pev->flags &= ~FL_ONGROUND; + SetNextThink( 0.2 ); + return; + } + else + { + Vector vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + + /* + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_EXPLOSION); // This just makes a dynamic light now + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 300 ); + WRITE_SHORT( g_sModelIndexFireball ); + WRITE_BYTE( 250 ); // scale * 10 + WRITE_BYTE( 8 ); // framerate + MESSAGE_END(); + */ + + // fireball + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_SPRITE ); + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 256 ); + WRITE_SHORT( m_iExplode ); + WRITE_BYTE( 120 ); // scale * 10 + WRITE_BYTE( 255 ); // brightness + MESSAGE_END(); + + // big smoke + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 512 ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( 250 ); // scale * 10 + WRITE_BYTE( 5 ); // framerate + MESSAGE_END(); + + // blast circle + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, pev->origin ); + WRITE_BYTE( TE_BEAMCYLINDER ); + WRITE_COORD( pev->origin.x); + WRITE_COORD( pev->origin.y); + WRITE_COORD( pev->origin.z); + WRITE_COORD( pev->origin.x); + WRITE_COORD( pev->origin.y); + WRITE_COORD( pev->origin.z + 2000 ); // reach damage radius over .2 seconds + WRITE_SHORT( m_iSpriteTexture ); + WRITE_BYTE( 0 ); // startframe + WRITE_BYTE( 0 ); // framerate + WRITE_BYTE( 4 ); // life + WRITE_BYTE( 32 ); // width + WRITE_BYTE( 0 ); // noise + WRITE_BYTE( 255 ); // r, g, b + WRITE_BYTE( 255 ); // r, g, b + WRITE_BYTE( 192 ); // r, g, b + WRITE_BYTE( 128 ); // brightness + WRITE_BYTE( 0 ); // speed + MESSAGE_END(); + + EMIT_SOUND(ENT(pev), CHAN_STATIC, "weapons/mortarhit.wav", 1.0, 0.3); + + RadiusDamage( pev->origin, pev, pev, 300, CLASS_NONE, DMG_BLAST ); + + if (pev->flags & FL_ONGROUND) + { + CBaseEntity *pWreckage = Create( "smokeent", pev->origin, pev->angles ); + UTIL_SetModel( ENT(pWreckage->pev), pev->model ); + UTIL_SetSize( pWreckage->pev, Vector( -200, -200, -128 ), Vector( 200, 200, -32 ) ); + pWreckage->SetNextThink( 0.1 ); + pWreckage->pev->frame = pev->frame; + pWreckage->pev->sequence = pev->sequence; + pWreckage->pev->framerate = 0; + pWreckage->pev->impulse = 50; + pWreckage->pev->dmgtime = gpGlobals->time + 5; + } + + // gibs + vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_BREAKMODEL); + + // position + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 64); + + // size + WRITE_COORD( 400 ); + WRITE_COORD( 400 ); + WRITE_COORD( 128 ); + + // velocity + WRITE_COORD( 0 ); + WRITE_COORD( 0 ); + WRITE_COORD( 200 ); + + // randomization + WRITE_BYTE( 30 ); + + // Model + WRITE_SHORT( m_iBodyGibs ); //model id# + + // # of shards + WRITE_BYTE( 200 ); + + // duration + WRITE_BYTE( 200 );// 10.0 seconds + + // flags + + WRITE_BYTE( BREAK_METAL ); + MESSAGE_END(); + + SetThink( Remove ); + SetNextThink( 0.1 ); + } +} + + +void CApache::FlyTouch( CBaseEntity *pOther ) +{ + // bounce if we hit something solid + if ( pOther->pev->solid == SOLID_BSP) + { + TraceResult tr = UTIL_GetGlobalTrace( ); + + // UNDONE, do a real bounce + pev->velocity = pev->velocity + tr.vecPlaneNormal * (pev->velocity.Length() + 200); + } +} + + +void CApache::CrashTouch( CBaseEntity *pOther ) +{ + // only crash if we hit something solid + if ( pOther->pev->solid == SOLID_BSP) + { + SetTouch( NULL ); + m_flNextRocket = gpGlobals->time; + SetNextThink( 0 ); + } +} + + + +void CApache :: GibMonster( void ) +{ + // EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "common/bodysplat.wav", 0.75, ATTN_NORM, 0, 200); +} + + +void CApache :: HuntThink( void ) +{ + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + ShowDamage( ); + + if ( m_pGoalEnt == NULL && !FStringNull(pev->target) )// this monster has a target + { + m_pGoalEnt = UTIL_FindEntityByTargetname( NULL, STRING( pev->target ) ); + if (m_pGoalEnt) + { + m_posDesired = m_pGoalEnt->pev->origin; + UTIL_MakeAimVectors( m_pGoalEnt->pev->angles ); + m_vecGoal = gpGlobals->v_forward; + } + } + + // if (m_hEnemy == NULL) + { + Look( 4092 ); + m_hEnemy = BestVisibleEnemy( ); + } + + // generic speed up + if (m_flGoalSpeed < 800) + m_flGoalSpeed += 5; + + if (m_hEnemy != NULL) + { + // ALERT( at_console, "%s\n", STRING( m_hEnemy->pev->classname ) ); + if (FVisible( m_hEnemy )) + { + if (m_flLastSeen < gpGlobals->time - 5) + m_flPrevSeen = gpGlobals->time; + m_flLastSeen = gpGlobals->time; + m_posTarget = m_hEnemy->Center( ); + } + else + { + m_hEnemy = NULL; + } + } + + m_vecTarget = (m_posTarget - pev->origin).Normalize(); + + float flLength = (pev->origin - m_posDesired).Length(); + + if (m_pGoalEnt) + { + // ALERT( at_console, "%.0f\n", flLength ); + + if (flLength < 128) + { + m_pGoalEnt = UTIL_FindEntityByTargetname( NULL, STRING( m_pGoalEnt->pev->target ) ); + if (m_pGoalEnt) + { + m_posDesired = m_pGoalEnt->pev->origin; + UTIL_MakeAimVectors( m_pGoalEnt->pev->angles ); + m_vecGoal = gpGlobals->v_forward; + flLength = (pev->origin - m_posDesired).Length(); + } + } + } + else + { + m_posDesired = pev->origin; + } + + if (flLength > 250) // 500 + { + // float flLength2 = (m_posTarget - pev->origin).Length() * (1.5 - DotProduct((m_posTarget - pev->origin).Normalize(), pev->velocity.Normalize() )); + // if (flLength2 < flLength) + if (m_flLastSeen + 90 > gpGlobals->time && DotProduct( (m_posTarget - pev->origin).Normalize(), (m_posDesired - pev->origin).Normalize( )) > 0.25) + { + m_vecDesired = (m_posTarget - pev->origin).Normalize( ); + } + else + { + m_vecDesired = (m_posDesired - pev->origin).Normalize( ); + } + } + else + { + m_vecDesired = m_vecGoal; + } + + Flight( ); + + // ALERT( at_console, "%.0f %.0f %.0f\n", gpGlobals->time, m_flLastSeen, m_flPrevSeen ); + if ((m_flLastSeen + 1 > gpGlobals->time) && (m_flPrevSeen + 2 < gpGlobals->time)) + { + if (FireGun( )) + { + // slow down if we're fireing + if (m_flGoalSpeed > 400) + m_flGoalSpeed = 400; + } + + } + + UTIL_MakeAimVectors( pev->angles ); + Vector vecEst = (gpGlobals->v_forward * 800 + pev->velocity).Normalize( ); + // ALERT( at_console, "%d %d %d %4.2f\n", pev->angles.x < 0, DotProduct( pev->velocity, gpGlobals->v_forward ) > -100, m_flNextRocket < gpGlobals->time, DotProduct( m_vecTarget, vecEst ) ); + + if ((m_iRockets % 2) == 1) + { + FireRocket( ); + m_flNextRocket = gpGlobals->time + 0.5; + if (m_iRockets <= 0) + { + m_flNextRocket = gpGlobals->time + 10; + m_iRockets = 10; + } + } + else if (pev->angles.x < 0 && DotProduct( pev->velocity, gpGlobals->v_forward ) > -100 && m_flNextRocket < gpGlobals->time) + { + if (m_flLastSeen + 60 > gpGlobals->time) + { + if (m_hEnemy != NULL) + { + // make sure it's a good shot + if (DotProduct( m_vecTarget, vecEst) > .965) + { + TraceResult tr; + + UTIL_TraceLine( pev->origin, pev->origin + vecEst * 4096, ignore_monsters, edict(), &tr ); + if ((tr.vecEndPos - m_posTarget).Length() < 512) + FireRocket( ); + } + } + else + { + TraceResult tr; + + UTIL_TraceLine( pev->origin, pev->origin + vecEst * 4096, dont_ignore_monsters, edict(), &tr ); + // just fire when close + if ((tr.vecEndPos - m_posTarget).Length() < 512) + FireRocket( ); + } + } + } +} + + +void CApache :: Flight( void ) +{ + // tilt model 5 degrees + Vector vecAdj = Vector( 5.0, 0, 0 ); + + // estimate where I'll be facing in one seconds + UTIL_MakeAimVectors( pev->angles + pev->avelocity * 2 + vecAdj); + // Vector vecEst1 = pev->origin + pev->velocity + gpGlobals->v_up * m_flForce - Vector( 0, 0, 384 ); + // float flSide = DotProduct( m_posDesired - vecEst1, gpGlobals->v_right ); + + float flSide = DotProduct( m_vecDesired, gpGlobals->v_right ); + + if (flSide < 0) + { + if (pev->avelocity.y < 60) + { + pev->avelocity.y += 8; // 9 * (3.0/2.0); + } + } + else + { + if (pev->avelocity.y > -60) + { + pev->avelocity.y -= 8; // 9 * (3.0/2.0); + } + } + pev->avelocity.y *= 0.98; + + // estimate where I'll be in two seconds + UTIL_MakeAimVectors( pev->angles + pev->avelocity * 1 + vecAdj); + Vector vecEst = pev->origin + pev->velocity * 2.0 + gpGlobals->v_up * m_flForce * 20 - Vector( 0, 0, 384 * 2 ); + + // add immediate force + UTIL_MakeAimVectors( pev->angles + vecAdj); + pev->velocity.x += gpGlobals->v_up.x * m_flForce; + pev->velocity.y += gpGlobals->v_up.y * m_flForce; + pev->velocity.z += gpGlobals->v_up.z * m_flForce; + // add gravity + pev->velocity.z -= 38.4; // 32ft/sec + + + float flSpeed = pev->velocity.Length(); + float flDir = DotProduct( Vector( gpGlobals->v_forward.x, gpGlobals->v_forward.y, 0 ), Vector( pev->velocity.x, pev->velocity.y, 0 ) ); + if (flDir < 0) + flSpeed = -flSpeed; + + float flDist = DotProduct( m_posDesired - vecEst, gpGlobals->v_forward ); + + // float flSlip = DotProduct( pev->velocity, gpGlobals->v_right ); + float flSlip = -DotProduct( m_posDesired - vecEst, gpGlobals->v_right ); + + // fly sideways + if (flSlip > 0) + { + if (pev->angles.z > -30 && pev->avelocity.z > -15) + pev->avelocity.z -= 4; + else + pev->avelocity.z += 2; + } + else + { + + if (pev->angles.z < 30 && pev->avelocity.z < 15) + pev->avelocity.z += 4; + else + pev->avelocity.z -= 2; + } + + // sideways drag + pev->velocity.x = pev->velocity.x * (1.0 - fabs( gpGlobals->v_right.x ) * 0.05); + pev->velocity.y = pev->velocity.y * (1.0 - fabs( gpGlobals->v_right.y ) * 0.05); + pev->velocity.z = pev->velocity.z * (1.0 - fabs( gpGlobals->v_right.z ) * 0.05); + + // general drag + pev->velocity = pev->velocity * 0.995; + + // apply power to stay correct height + if (m_flForce < 80 && vecEst.z < m_posDesired.z) + { + m_flForce += 12; + } + else if (m_flForce > 30) + { + if (vecEst.z > m_posDesired.z) + m_flForce -= 8; + } + + // pitch forward or back to get to target + if (flDist > 0 && flSpeed < m_flGoalSpeed /* && flSpeed < flDist */ && pev->angles.x + pev->avelocity.x > -40) + { + // ALERT( at_console, "F " ); + // lean forward + pev->avelocity.x -= 12.0; + } + else if (flDist < 0 && flSpeed > -50 && pev->angles.x + pev->avelocity.x < 20) + { + // ALERT( at_console, "B " ); + // lean backward + pev->avelocity.x += 12.0; + } + else if (pev->angles.x + pev->avelocity.x > 0) + { + // ALERT( at_console, "f " ); + pev->avelocity.x -= 4.0; + } + else if (pev->angles.x + pev->avelocity.x < 0) + { + // ALERT( at_console, "b " ); + pev->avelocity.x += 4.0; + } + + // ALERT( at_console, "%.0f %.0f : %.0f %.0f : %.0f %.0f : %.0f\n", pev->origin.x, pev->velocity.x, flDist, flSpeed, pev->angles.x, pev->avelocity.x, m_flForce ); + // ALERT( at_console, "%.0f %.0f : %.0f %0.f : %.0f\n", pev->origin.z, pev->velocity.z, vecEst.z, m_posDesired.z, m_flForce ); + + // make rotor, engine sounds + if (m_iSoundState == 0) + { + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_rotor2.wav", 1.0, 0.3, 0, 110 ); + // EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_whine1.wav", 0.5, 0.2, 0, 110 ); + + m_iSoundState = SND_CHANGE_PITCH; // hack for going through level transitions + } + else + { + CBaseEntity *pPlayer = NULL; + + pPlayer = UTIL_FindEntityByClassname( NULL, "player" ); + // UNDONE: this needs to send different sounds to every player for multiplayer. + if (pPlayer) + { + + float pitch = DotProduct( pev->velocity - pPlayer->pev->velocity, (pPlayer->pev->origin - pev->origin).Normalize() ); + + pitch = (int)(100 + pitch / 50.0); + + if (pitch > 250) + pitch = 250; + if (pitch < 50) + pitch = 50; + if (pitch == 100) + pitch = 101; + + float flVol = (m_flForce / 100.0) + .1; + if (flVol > 1.0) + flVol = 1.0; + + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_rotor2.wav", 1.0, 0.3, SND_CHANGE_PITCH | SND_CHANGE_VOL, pitch); + } + // EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_whine1.wav", flVol, 0.2, SND_CHANGE_PITCH | SND_CHANGE_VOL, pitch); + + // ALERT( at_console, "%.0f %.2f\n", pitch, flVol ); + } +} + + +void CApache :: FireRocket( void ) +{ + static float side = 1.0; + static int count; + + if (m_iRockets <= 0) + return; + + UTIL_MakeAimVectors( pev->angles ); + Vector vecSrc = pev->origin + 1.5 * (gpGlobals->v_forward * 21 + gpGlobals->v_right * 70 * side + gpGlobals->v_up * -79); + + switch( m_iRockets % 5) + { + case 0: vecSrc = vecSrc + gpGlobals->v_right * 10; break; + case 1: vecSrc = vecSrc - gpGlobals->v_right * 10; break; + case 2: vecSrc = vecSrc + gpGlobals->v_up * 10; break; + case 3: vecSrc = vecSrc - gpGlobals->v_up * 10; break; + case 4: break; + } + + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSrc ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( vecSrc.x ); + WRITE_COORD( vecSrc.y ); + WRITE_COORD( vecSrc.z ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( 20 ); // scale * 10 + WRITE_BYTE( 12 ); // framerate + MESSAGE_END(); + + CBaseEntity *pRocket = CBaseEntity::Create( "hvr_rocket", vecSrc, pev->angles, edict() ); + if (pRocket) + pRocket->pev->velocity = pev->velocity + gpGlobals->v_forward * 100; + + m_iRockets--; + + side = - side; +} + + + +BOOL CApache :: FireGun( ) +{ + UTIL_MakeAimVectors( pev->angles ); + + Vector posGun, angGun; + GetAttachment( 1, posGun, angGun ); + + Vector vecTarget = (m_posTarget - posGun).Normalize( ); + + Vector vecOut; + + vecOut.x = DotProduct( gpGlobals->v_forward, vecTarget ); + vecOut.y = -DotProduct( gpGlobals->v_right, vecTarget ); + vecOut.z = DotProduct( gpGlobals->v_up, vecTarget ); + + Vector angles = UTIL_VecToAngles (vecOut); + + angles.x = -angles.x; + if (angles.y > 180) + angles.y = angles.y - 360; + if (angles.y < -180) + angles.y = angles.y + 360; + if (angles.x > 180) + angles.x = angles.x - 360; + if (angles.x < -180) + angles.x = angles.x + 360; + + if (angles.x > m_angGun.x) + m_angGun.x = min( angles.x, m_angGun.x + 12 ); + if (angles.x < m_angGun.x) + m_angGun.x = max( angles.x, m_angGun.x - 12 ); + if (angles.y > m_angGun.y) + m_angGun.y = min( angles.y, m_angGun.y + 12 ); + if (angles.y < m_angGun.y) + m_angGun.y = max( angles.y, m_angGun.y - 12 ); + + m_angGun.y = SetBoneController( 0, m_angGun.y ); + m_angGun.x = SetBoneController( 1, m_angGun.x ); + + Vector posBarrel, angBarrel; + GetAttachment( 0, posBarrel, angBarrel ); + Vector vecGun = (posBarrel - posGun).Normalize( ); + + if (DotProduct( vecGun, vecTarget ) > 0.98) + { +#if 1 + FireBullets( 1, posGun, vecGun, VECTOR_CONE_4DEGREES, 8192, BULLET_12MM, 1 ); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "turret/tu_fire1.wav", 1, 0.3); +#else + static float flNext; + TraceResult tr; + UTIL_TraceLine( posGun, posGun + vecGun * 8192, dont_ignore_monsters, ENT( pev ), &tr ); + + if (!m_pBeam) + { + m_pBeam = CBeam::BeamCreate( "sprites/lgtning.spr", 80 ); + m_pBeam->PointEntInit( pev->origin, entindex( ) ); + m_pBeam->SetEndAttachment( 1 ); + m_pBeam->SetColor( 255, 180, 96 ); + m_pBeam->SetBrightness( 192 ); + } + + if (flNext < gpGlobals->time) + { + flNext = gpGlobals->time + 0.5; + m_pBeam->SetStartPos( tr.vecEndPos ); + } +#endif + return TRUE; + } + else + { + if (m_pBeam) + { + UTIL_Remove( m_pBeam ); + m_pBeam = NULL; + } + } + return FALSE; +} + + + +void CApache :: ShowDamage( void ) +{ + if (m_iDoSmokePuff > 0 || RANDOM_LONG(0,99) > pev->health) + { + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, pev->origin ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( pev->origin.x ); + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z - 32 ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( RANDOM_LONG(0,9) + 20 ); // scale * 10 + WRITE_BYTE( 12 ); // framerate + MESSAGE_END(); + } + if (m_iDoSmokePuff > 0) + m_iDoSmokePuff--; +} + + +int CApache :: TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ) +{ + if (pevInflictor->owner == edict()) + return 0; + + if (bitsDamageType & DMG_BLAST) + { + flDamage *= 2; + } + + /* + if ( (bitsDamageType & DMG_BULLET) && flDamage > 50) + { + // clip bullet damage at 50 + flDamage = 50; + } + */ + + // ALERT( at_console, "%.0f\n", flDamage ); + return CBaseEntity::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType ); +} + + + +void CApache::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + // ALERT( at_console, "%d %.0f\n", ptr->iHitgroup, flDamage ); + + // ignore blades + if (ptr->iHitgroup == 6 && (bitsDamageType & (DMG_ENERGYBEAM|DMG_BULLET|DMG_CLUB))) + return; + + // hit hard, hits cockpit, hits engines + if (flDamage > 50 || ptr->iHitgroup == 1 || ptr->iHitgroup == 2) + { + // ALERT( at_console, "%.0f\n", flDamage ); + AddMultiDamage( pevAttacker, this, flDamage, bitsDamageType ); + m_iDoSmokePuff = 3 + (flDamage / 5.0); + } + else + { + // do half damage in the body + // AddMultiDamage( pevAttacker, this, flDamage / 2.0, bitsDamageType ); + UTIL_Ricochet( ptr->vecEndPos, 2.0 ); + } +} + +#endif diff --git a/server/monsters/barnacle.cpp b/server/monsters/barnacle.cpp new file mode 100644 index 00000000..5a8a3e3a --- /dev/null +++ b/server/monsters/barnacle.cpp @@ -0,0 +1,434 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// barnacle - stationary ceiling mounted 'fishing' monster +//========================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" + +#define BARNACLE_BODY_HEIGHT 44 // how 'tall' the barnacle's model is. +#define BARNACLE_PULL_SPEED 8 +#define BARNACLE_KILL_VICTIM_DELAY 5 // how many seconds after pulling prey in to gib them. + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +#define BARNACLE_AE_PUKEGIB 2 + +class CBarnacle : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + CBaseEntity *TongueTouchEnt ( float *pflLength ); + int Classify ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + void EXPORT BarnacleThink ( void ); + void EXPORT WaitTillDead ( void ); + void Killed( entvars_t *pevAttacker, int iGib ); + int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + float m_flAltitude; + float m_flKillVictimTime; + int m_cGibs;// barnacle loads up on gibs each time it kills something. + BOOL m_fTongueExtended; + BOOL m_fLiftingPrey; + float m_flTongueAdj; +}; +LINK_ENTITY_TO_CLASS( monster_barnacle, CBarnacle ); + +TYPEDESCRIPTION CBarnacle::m_SaveData[] = +{ + DEFINE_FIELD( CBarnacle, m_flAltitude, FIELD_FLOAT ), + DEFINE_FIELD( CBarnacle, m_flKillVictimTime, FIELD_TIME ), + DEFINE_FIELD( CBarnacle, m_cGibs, FIELD_INTEGER ),// barnacle loads up on gibs each time it kills something. + DEFINE_FIELD( CBarnacle, m_fTongueExtended, FIELD_BOOLEAN ), + DEFINE_FIELD( CBarnacle, m_fLiftingPrey, FIELD_BOOLEAN ), + DEFINE_FIELD( CBarnacle, m_flTongueAdj, FIELD_FLOAT ), +}; + +IMPLEMENT_SAVERESTORE( CBarnacle, CBaseMonster ); + + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CBarnacle :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_ALIEN_MONSTER; +} + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +// +// Returns number of events handled, 0 if none. +//========================================================= +void CBarnacle :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case BARNACLE_AE_PUKEGIB: + CGib::SpawnRandomGibs( this, 1, 1 ); + break; + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + +//========================================================= +// Spawn +//========================================================= +void CBarnacle :: Spawn() +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/barnacle.mdl"); + UTIL_SetSize( pev, Vector(-16, -16, -32), Vector(16, 16, 0) ); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_NONE; + pev->takedamage = DAMAGE_AIM; + m_bloodColor = BLOOD_COLOR_RED; + pev->effects = EF_INVLIGHT; // take light from the ceiling + pev->health = 25; + m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + m_flKillVictimTime = 0; + m_cGibs = 0; + m_fLiftingPrey = FALSE; + m_flTongueAdj = -100; + + InitBoneControllers(); + + SetActivity ( ACT_IDLE ); + + SetThink(&CBarnacle :: BarnacleThink ); + SetNextThink( 0.5 ); + + UTIL_SetOrigin ( this, pev->origin ); +} + +int CBarnacle::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + if ( bitsDamageType & DMG_CLUB ) + { + flDamage = pev->health; + } + + return CBaseMonster::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType ); +} + +//========================================================= +//========================================================= +void CBarnacle :: BarnacleThink ( void ) +{ + CBaseEntity *pTouchEnt; + CBaseMonster *pVictim; + float flLength; + + SetNextThink( 0.1 ); + + if ( m_hEnemy != NULL ) + { +// barnacle has prey. + + if ( !m_hEnemy->IsAlive() ) + { + // someone (maybe even the barnacle) killed the prey. Reset barnacle. + m_fLiftingPrey = FALSE;// indicate that we're not lifting prey. + m_hEnemy = NULL; + return; + } + + if ( m_fLiftingPrey ) + { + if ( m_hEnemy != NULL && m_hEnemy->pev->deadflag != DEAD_NO ) + { + // crap, someone killed the prey on the way up. + m_hEnemy = NULL; + m_fLiftingPrey = FALSE; + return; + } + + // still pulling prey. + Vector vecNewEnemyOrigin = m_hEnemy->pev->origin; + vecNewEnemyOrigin.x = pev->origin.x; + vecNewEnemyOrigin.y = pev->origin.y; + + // guess as to where their neck is + vecNewEnemyOrigin.x -= 6 * cos(m_hEnemy->pev->angles.y * M_PI/180.0); + vecNewEnemyOrigin.y -= 6 * sin(m_hEnemy->pev->angles.y * M_PI/180.0); + + m_flAltitude -= BARNACLE_PULL_SPEED; + vecNewEnemyOrigin.z += BARNACLE_PULL_SPEED; + + if ( fabs( pev->origin.z - ( vecNewEnemyOrigin.z + m_hEnemy->pev->view_ofs.z - 8 ) ) < BARNACLE_BODY_HEIGHT ) + { + // prey has just been lifted into position ( if the victim origin + eye height + 8 is higher + // than the bottom of the barnacle, it is assumed that the head is within barnacle's body ) + m_fLiftingPrey = FALSE; + + EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_bite3.wav", 1, ATTN_NORM ); + + pVictim = m_hEnemy->MyMonsterPointer(); + + m_flKillVictimTime = gpGlobals->time + 10;// now that the victim is in place, the killing bite will be administered in 10 seconds. + + if ( pVictim ) + { + pVictim->BarnacleVictimBitten( pev ); + SetActivity ( ACT_EAT ); + } + } + + UTIL_SetOrigin ( m_hEnemy, vecNewEnemyOrigin ); + } + else + { + // prey is lifted fully into feeding position and is dangling there. + + pVictim = m_hEnemy->MyMonsterPointer(); + + if ( m_flKillVictimTime != -1 && gpGlobals->time > m_flKillVictimTime ) + { + // kill! + if ( pVictim ) + { + pVictim->TakeDamage ( pev, pev, pVictim->pev->health, DMG_SLASH | DMG_ALWAYSGIB ); + m_cGibs = 3; + } + + return; + } + + // bite prey every once in a while + if ( pVictim && ( RANDOM_LONG(0,49) == 0 ) ) + { + switch ( RANDOM_LONG(0,2) ) + { + case 0: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_chew1.wav", 1, ATTN_NORM ); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_chew2.wav", 1, ATTN_NORM ); break; + case 2: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_chew3.wav", 1, ATTN_NORM ); break; + } + + pVictim->BarnacleVictimBitten( pev ); + } + + } + } + else + { +// barnacle has no prey right now, so just idle and check to see if anything is touching the tongue. + + // If idle and no nearby client, don't think so often + if ( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) && !HaveCamerasInPVS( edict() ) ) + SetNextThink( RANDOM_FLOAT(1,1.5) ); // Stagger a bit to keep barnacles from thinking on the same frame + + if ( m_fSequenceFinished ) + {// this is done so barnacle will fidget. + SetActivity ( ACT_IDLE ); + m_flTongueAdj = -100; + } + + if ( m_cGibs && RANDOM_LONG(0,99) == 1 ) + { + // cough up a gib. + CGib::SpawnRandomGibs( this, 1, 1 ); + m_cGibs--; + + switch ( RANDOM_LONG(0,2) ) + { + case 0: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_chew1.wav", 1, ATTN_NORM ); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_chew2.wav", 1, ATTN_NORM ); break; + case 2: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_chew3.wav", 1, ATTN_NORM ); break; + } + } + + pTouchEnt = TongueTouchEnt( &flLength ); + + if ( pTouchEnt != NULL && m_fTongueExtended ) + { + // tongue is fully extended, and is touching someone. + if ( pTouchEnt->FBecomeProne() ) + { + EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_alert2.wav", 1, ATTN_NORM ); + + SetSequenceByName ( "attack1" ); + m_flTongueAdj = -20; + + m_hEnemy = pTouchEnt; + + pTouchEnt->pev->movetype = MOVETYPE_FLY; + pTouchEnt->pev->velocity = pev->velocity; //LRC- make him come _with_ me + pTouchEnt->pev->origin.x = pev->origin.x; + pTouchEnt->pev->origin.y = pev->origin.y; + + m_fLiftingPrey = TRUE;// indicate that we should be lifting prey. + m_flKillVictimTime = -1;// set this to a bogus time while the victim is lifted. + + m_flAltitude = (pev->origin.z - pTouchEnt->EyePosition().z); + } + } + else + { + // calculate a new length for the tongue to be clear of anything else that moves under it. + if ( m_flAltitude < flLength ) + { + // if tongue is higher than is should be, lower it kind of slowly. + m_flAltitude += BARNACLE_PULL_SPEED; + m_fTongueExtended = FALSE; + } + else + { + m_flAltitude = flLength; + m_fTongueExtended = TRUE; + } + + } + + } + + // ALERT( at_console, "tounge %f\n", m_flAltitude + m_flTongueAdj ); + SetBoneController( 0, -(m_flAltitude + m_flTongueAdj) ); + StudioFrameAdvance( 0.1 ); +} + +//========================================================= +// Killed. +//========================================================= +void CBarnacle :: Killed( entvars_t *pevAttacker, int iGib ) +{ + CBaseMonster *pVictim; + + pev->solid = SOLID_NOT; + pev->takedamage = DAMAGE_NO; + + if ( m_hEnemy != NULL ) + { + pVictim = m_hEnemy->MyMonsterPointer(); + + if ( pVictim ) + { + pVictim->BarnacleVictimReleased(); + } + } + +// CGib::SpawnRandomGibs( pev, 4, 1 ); + + switch ( RANDOM_LONG ( 0, 1 ) ) + { + case 0: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_die1.wav", 1, ATTN_NORM ); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_WEAPON, "barnacle/bcl_die3.wav", 1, ATTN_NORM ); break; + } + + SetActivity ( ACT_DIESIMPLE ); + SetBoneController( 0, 0 ); + + StudioFrameAdvance( 0.1 ); + + SetNextThink( 0.1 ); + SetThink(&CBarnacle :: WaitTillDead ); +} + +//========================================================= +//========================================================= +void CBarnacle :: WaitTillDead ( void ) +{ + SetNextThink( 0.1 ); + + float flInterval = StudioFrameAdvance( 0.1 ); + DispatchAnimEvents ( flInterval ); + + if ( m_fSequenceFinished ) + { + // death anim finished. + StopAnimation(); + SetThink ( NULL ); + } +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CBarnacle :: Precache() +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/barnacle.mdl"); + + PRECACHE_SOUND("barnacle/bcl_alert2.wav");//happy, lifting food up + PRECACHE_SOUND("barnacle/bcl_bite3.wav");//just got food to mouth + PRECACHE_SOUND("barnacle/bcl_chew1.wav"); + PRECACHE_SOUND("barnacle/bcl_chew2.wav"); + PRECACHE_SOUND("barnacle/bcl_chew3.wav"); + PRECACHE_SOUND("barnacle/bcl_die1.wav" ); + PRECACHE_SOUND("barnacle/bcl_die3.wav" ); +} + +//========================================================= +// TongueTouchEnt - does a trace along the barnacle's tongue +// to see if any entity is touching it. Also stores the length +// of the trace in the int pointer provided. +//========================================================= +#define BARNACLE_CHECK_SPACING 8 +CBaseEntity *CBarnacle :: TongueTouchEnt ( float *pflLength ) +{ + TraceResult tr; + float length; + + // trace once to hit architecture and see if the tongue needs to change position. + UTIL_TraceLine ( pev->origin, pev->origin - Vector ( 0 , 0 , 2048 ), ignore_monsters, ENT(pev), &tr ); + length = fabs( pev->origin.z - tr.vecEndPos.z ); + if ( pflLength ) + { + *pflLength = length; + } + + Vector delta = Vector( BARNACLE_CHECK_SPACING, BARNACLE_CHECK_SPACING, 0 ); + Vector mins = pev->origin - delta; + Vector maxs = pev->origin + delta; + maxs.z = pev->origin.z; + mins.z -= length; + + CBaseEntity *pList[10]; + int count = UTIL_EntitiesInBox( pList, 10, mins, maxs, (FL_CLIENT|FL_MONSTER) ); + if ( count ) + { + for ( int i = 0; i < count; i++ ) + { + // only clients and monsters + if ( pList[i] != this && IRelationship( pList[i] ) > R_NO && pList[ i ]->pev->deadflag == DEAD_NO ) // this ent is one of our enemies. Barnacle tries to eat it. + { + return pList[i]; + } + } + } + + return NULL; +} diff --git a/server/monsters/barney.cpp b/server/monsters/barney.cpp new file mode 100644 index 00000000..3dd91469 --- /dev/null +++ b/server/monsters/barney.cpp @@ -0,0 +1,948 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// monster template +//========================================================= +// UNDONE: Holster weapon? + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "talkmonster.h" +#include "schedule.h" +#include "defaultai.h" +#include "scripted.h" +#include "baseweapon.h" +#include "soundent.h" +#include "defaults.h" + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +// first flag is barney dying for scripted sequences? +#define BARNEY_AE_DRAW ( 2 ) +#define BARNEY_AE_SHOOT ( 3 ) +#define BARNEY_AE_HOLSTER ( 4 ) + +#define BARNEY_BODY_GUNHOLSTERED 0 +#define BARNEY_BODY_GUNDRAWN 1 +#define BARNEY_BODY_GUNGONE 2 + +class CBarney : public CTalkMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed( void ); + int ISoundMask( void ); + void BarneyFirePistol( void ); + void AlertSound( void ); + int Classify ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + + void RunTask( Task_t *pTask ); + void StartTask( Task_t *pTask ); + virtual int ObjectCaps( void ) { return CTalkMonster :: ObjectCaps() | FCAP_IMPULSE_USE; } + int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType); + BOOL CheckRangeAttack1 ( float flDot, float flDist ); + + void DeclineFollowing( void ); + + // Override these to set behavior + Schedule_t *GetScheduleOfType ( int Type ); + Schedule_t *GetSchedule ( void ); + MONSTERSTATE GetIdealState ( void ); + + void DeathSound( void ); + void PainSound( void ); + + void TalkInit( void ); + + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + void Killed( entvars_t *pevAttacker, int iGib ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + int m_iBaseBody; //LRC - for barneys with different bodies + BOOL m_fGunDrawn; + float m_painTime; + float m_checkAttackTime; + BOOL m_lastAttackCheck; + + // UNDONE: What is this for? It isn't used? + float m_flPlayerDamage;// how much pain has the player inflicted on me? + + CUSTOM_SCHEDULES; +}; + +LINK_ENTITY_TO_CLASS( monster_barney, CBarney ); + +TYPEDESCRIPTION CBarney::m_SaveData[] = +{ + DEFINE_FIELD( CBarney, m_iBaseBody, FIELD_INTEGER ), //LRC + DEFINE_FIELD( CBarney, m_fGunDrawn, FIELD_BOOLEAN ), + DEFINE_FIELD( CBarney, m_painTime, FIELD_TIME ), + DEFINE_FIELD( CBarney, m_checkAttackTime, FIELD_TIME ), + DEFINE_FIELD( CBarney, m_lastAttackCheck, FIELD_BOOLEAN ), + DEFINE_FIELD( CBarney, m_flPlayerDamage, FIELD_FLOAT ), +}; + +IMPLEMENT_SAVERESTORE( CBarney, CTalkMonster ); + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= +Task_t tlBaFollow[] = +{ + { TASK_MOVE_TO_TARGET_RANGE,(float)128 }, // Move within 128 of target ent (client) + { TASK_SET_SCHEDULE, (float)SCHED_TARGET_FACE }, +}; + +Schedule_t slBaFollow[] = +{ + { + tlBaFollow, + ARRAYSIZE ( tlBaFollow ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_PROVOKED, + bits_SOUND_DANGER, + "Follow" + }, +}; + +//========================================================= +// BarneyDraw- much better looking draw schedule for when +// barney knows who he's gonna attack. +//========================================================= +Task_t tlBarneyEnemyDraw[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, 0 }, + { TASK_PLAY_SEQUENCE_FACE_ENEMY, (float) ACT_ARM }, +}; + +Schedule_t slBarneyEnemyDraw[] = +{ + { + tlBarneyEnemyDraw, + ARRAYSIZE ( tlBarneyEnemyDraw ), + 0, + 0, + "Barney Enemy Draw" + } +}; + +Task_t tlBaFaceTarget[] = +{ + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_FACE_TARGET, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_SET_SCHEDULE, (float)SCHED_TARGET_CHASE }, +}; + +Schedule_t slBaFaceTarget[] = +{ + { + tlBaFaceTarget, + ARRAYSIZE ( tlBaFaceTarget ), + bits_COND_CLIENT_PUSH | + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_PROVOKED, + bits_SOUND_DANGER, + "FaceTarget" + }, +}; + + +Task_t tlIdleBaStand[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT, (float)2 }, // repick IDLESTAND every two seconds. + { TASK_TLK_HEADRESET, (float)0 }, // reset head position +}; + +Schedule_t slIdleBaStand[] = +{ + { + tlIdleBaStand, + ARRAYSIZE ( tlIdleBaStand ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_SMELL | + bits_COND_PROVOKED, + + bits_SOUND_COMBAT |// sound flags - change these, and you'll break the talking code. + //bits_SOUND_PLAYER | + //bits_SOUND_WORLD | + + bits_SOUND_DANGER | + bits_SOUND_MEAT |// scents + bits_SOUND_CARCASS | + bits_SOUND_GARBAGE, + "IdleStand" + }, +}; + +DEFINE_CUSTOM_SCHEDULES( CBarney ) +{ + slBaFollow, + slBarneyEnemyDraw, + slBaFaceTarget, + slIdleBaStand, +}; + + +IMPLEMENT_CUSTOM_SCHEDULES( CBarney, CTalkMonster ); + +void CBarney :: StartTask( Task_t *pTask ) +{ + CTalkMonster::StartTask( pTask ); +} + +void CBarney :: RunTask( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_RANGE_ATTACK1: + if (m_hEnemy != NULL && (m_hEnemy->IsPlayer())) + { + pev->framerate = 1.5; + } + CTalkMonster::RunTask( pTask ); + break; + default: + CTalkMonster::RunTask( pTask ); + break; + } +} + + + + +//========================================================= +// ISoundMask - returns a bit mask indicating which types +// of sounds this monster regards. +//========================================================= +int CBarney :: ISoundMask ( void) +{ + return bits_SOUND_WORLD | + bits_SOUND_COMBAT | + bits_SOUND_CARCASS | + bits_SOUND_MEAT | + bits_SOUND_GARBAGE | + bits_SOUND_DANGER | + bits_SOUND_PLAYER; +} + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CBarney :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_PLAYER_ALLY; +} + +//========================================================= +// ALertSound - barney says "Freeze!" +//========================================================= +void CBarney :: AlertSound( void ) +{ + if ( m_hEnemy != NULL ) + { + if ( FOkToSpeak() ) + { + if (m_iszSpeakAs) + { + char szBuf[32]; + strcpy(szBuf,STRING(m_iszSpeakAs)); + strcat(szBuf,"_ATTACK"); + PlaySentence( szBuf, RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); + } + else + { + PlaySentence( "BA_ATTACK", RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); + } + } + } + +} +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CBarney :: SetYawSpeed ( void ) +{ + int ys; + + ys = 0; + + switch ( m_Activity ) + { + case ACT_IDLE: + ys = 70; + break; + case ACT_WALK: + ys = 70; + break; + case ACT_RUN: + ys = 90; + break; + default: + ys = 70; + break; + } + + pev->yaw_speed = ys; +} + + +//========================================================= +// CheckRangeAttack1 +//========================================================= +BOOL CBarney :: CheckRangeAttack1 ( float flDot, float flDist ) +{ + if ( flDist <= 1024 && flDot >= 0.5 ) + { + if ( gpGlobals->time > m_checkAttackTime ) + { + TraceResult tr; + + Vector shootOrigin = pev->origin + Vector( 0, 0, 55 ); + CBaseEntity *pEnemy = m_hEnemy; + Vector shootTarget = ( (pEnemy->BodyTarget( shootOrigin ) - pEnemy->pev->origin) + m_vecEnemyLKP ); + UTIL_TraceLine( shootOrigin, shootTarget, dont_ignore_monsters, ENT(pev), &tr ); + m_checkAttackTime = gpGlobals->time + 1; + if ( tr.flFraction == 1.0 || (tr.pHit != NULL && CBaseEntity::Instance(tr.pHit) == pEnemy) ) + m_lastAttackCheck = TRUE; + else + m_lastAttackCheck = FALSE; + m_checkAttackTime = gpGlobals->time + 1.5; + } + return m_lastAttackCheck; + } + return FALSE; +} + + +//========================================================= +// BarneyFirePistol - shoots one round from the pistol at +// the enemy barney is facing. +//========================================================= +void CBarney :: BarneyFirePistol ( void ) +{ + Vector vecShootOrigin; + + UTIL_MakeVectors(pev->angles); + vecShootOrigin = pev->origin + Vector( 0, 0, 55 ); + Vector vecShootDir = ShootAtEnemy( vecShootOrigin ); + + Vector angDir = UTIL_VecToAngles( vecShootDir ); + SetBlending( 0, angDir.x ); + pev->effects = EF_MUZZLEFLASH; + + if (pev->frags) + { + FireBullets(1, vecShootOrigin, vecShootDir, VECTOR_CONE_2DEGREES, 1024, BULLET_357); + if (RANDOM_LONG(0, 1)) + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, "weapons/357_shot1.wav", 1, ATTN_NORM, 0, 100 ); + else + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, "weapons/357_shot2.wav", 1, ATTN_NORM, 0, 100 ); + } + else + { + FireBullets(1, vecShootOrigin, vecShootDir, VECTOR_CONE_2DEGREES, 1024, BULLET_9MM ); + + int pitchShift = RANDOM_LONG( 0, 20 ); + + // Only shift about half the time + if ( pitchShift > 10 ) + pitchShift = 0; + else + pitchShift -= 5; + + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, "barney/ba_attack2.wav", 1, ATTN_NORM, 0, 100 + pitchShift ); + } + + CSoundEnt::InsertSound ( bits_SOUND_COMBAT, pev->origin, 384, 0.3 ); + + // UNDONE: Reload? + m_cAmmoLoaded--;// take away a bullet! + + // Teh_Freak: World Lighting! + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_DLIGHT ); + WRITE_COORD( vecShootOrigin.x ); // origin + WRITE_COORD( vecShootOrigin.y ); + WRITE_COORD( vecShootOrigin.z ); + WRITE_BYTE( 16 ); // radius + WRITE_BYTE( 255 ); // R + WRITE_BYTE( 255 ); // G + WRITE_BYTE( 128 ); // B + WRITE_BYTE( 0 ); // life * 10 + WRITE_BYTE( 0 ); // decay + MESSAGE_END(); + // Teh_Freak: World Lighting! + +} + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +// +// Returns number of events handled, 0 if none. +//========================================================= +void CBarney :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case BARNEY_AE_SHOOT: + BarneyFirePistol(); + break; + + case BARNEY_AE_DRAW: + // barney's bodygroup switches here so he can pull gun from holster + pev->body = m_iBaseBody + BARNEY_BODY_GUNDRAWN; + m_fGunDrawn = TRUE; + break; + + case BARNEY_AE_HOLSTER: + // change bodygroup to replace gun in holster + pev->body = m_iBaseBody + BARNEY_BODY_GUNHOLSTERED; + m_fGunDrawn = FALSE; + break; + + default: + CTalkMonster::HandleAnimEvent( pEvent ); + } +} + +//========================================================= +// Spawn +//========================================================= +void CBarney :: Spawn() +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/barney.mdl"); + UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_RED; + if (pev->health == 0) //LRC + pev->health = BARNEY_HEALTH; + pev->view_ofs = Vector ( 0, 0, 50 );// position of the eyes relative to monster's origin. + m_flFieldOfView = VIEW_FIELD_WIDE; // NOTE: we need a wide field of view so npc will notice player and say hello + m_MonsterState = MONSTERSTATE_NONE; + + m_iBaseBody = pev->body; //LRC + pev->body = m_iBaseBody + BARNEY_BODY_GUNHOLSTERED; // gun in holster + m_fGunDrawn = FALSE; + + m_afCapability = bits_CAP_HEAR | bits_CAP_TURN_HEAD | bits_CAP_DOORS_GROUP; + + MonsterInit(); + SetUse(&CBarney :: FollowerUse ); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CBarney :: Precache() +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/barney.mdl"); + + PRECACHE_SOUND("barney/ba_attack1.wav" ); + PRECACHE_SOUND("barney/ba_attack2.wav" ); + + PRECACHE_SOUND("barney/ba_pain1.wav"); + PRECACHE_SOUND("barney/ba_pain2.wav"); + PRECACHE_SOUND("barney/ba_pain3.wav"); + + PRECACHE_SOUND("barney/ba_die1.wav"); + PRECACHE_SOUND("barney/ba_die2.wav"); + PRECACHE_SOUND("barney/ba_die3.wav"); + + // every new barney must call this, otherwise + // when a level is loaded, nobody will talk (time is reset to 0) + TalkInit(); + CTalkMonster::Precache(); +} + +// Init talk data +void CBarney :: TalkInit() +{ + + CTalkMonster::TalkInit(); + + // barney speech group names (group names are in sentences.txt) + + if (!m_iszSpeakAs) + { + m_szGrp[TLK_ANSWER] = "BA_ANSWER"; + m_szGrp[TLK_QUESTION] = "BA_QUESTION"; + m_szGrp[TLK_IDLE] = "BA_IDLE"; + m_szGrp[TLK_STARE] = "BA_STARE"; + if (pev->spawnflags & SF_MONSTER_PREDISASTER) //LRC + m_szGrp[TLK_USE] = "BA_PFOLLOW"; + else + m_szGrp[TLK_USE] = "BA_OK"; + if (pev->spawnflags & SF_MONSTER_PREDISASTER) + m_szGrp[TLK_UNUSE] = "BA_PWAIT"; + else + m_szGrp[TLK_UNUSE] = "BA_WAIT"; + if (pev->spawnflags & SF_MONSTER_PREDISASTER) + m_szGrp[TLK_DECLINE] = "BA_POK"; + else + m_szGrp[TLK_DECLINE] = "BA_NOTOK"; + m_szGrp[TLK_STOP] = "BA_STOP"; + + m_szGrp[TLK_NOSHOOT] = "BA_SCARED"; + m_szGrp[TLK_HELLO] = "BA_HELLO"; + + m_szGrp[TLK_PLHURT1] = "!BA_CUREA"; + m_szGrp[TLK_PLHURT2] = "!BA_CUREB"; + m_szGrp[TLK_PLHURT3] = "!BA_CUREC"; + + m_szGrp[TLK_PHELLO] = NULL; //"BA_PHELLO"; // UNDONE + m_szGrp[TLK_PIDLE] = NULL; //"BA_PIDLE"; // UNDONE + m_szGrp[TLK_PQUESTION] = "BA_PQUEST"; // UNDONE + + m_szGrp[TLK_SMELL] = "BA_SMELL"; + + m_szGrp[TLK_WOUND] = "BA_WOUND"; + m_szGrp[TLK_MORTAL] = "BA_MORTAL"; + } + + // get voice for head - just one barney voice for now + m_voicePitch = 100; +} + + +static BOOL IsFacing( entvars_t *pevTest, const Vector &reference ) +{ + Vector vecDir = (reference - pevTest->origin); + vecDir.z = 0; + vecDir = vecDir.Normalize(); + Vector forward, angle; + angle = pevTest->v_angle; + angle.x = 0; + UTIL_MakeVectorsPrivate( angle, forward, NULL, NULL ); + // He's facing me, he meant it + if ( DotProduct( forward, vecDir ) > 0.96 ) // +/- 15 degrees or so + { + return TRUE; + } + return FALSE; +} + + +int CBarney :: TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType) +{ + // make sure friends talk about it if player hurts talkmonsters... + int ret = CTalkMonster::TakeDamage(pevInflictor, pevAttacker, flDamage, bitsDamageType); + if ( !IsAlive() || pev->deadflag == DEAD_DYING ) + return ret; + + // LRC - if my reaction to the player has been overridden, don't do this stuff + if (m_iPlayerReact) return ret; + + if ( m_MonsterState != MONSTERSTATE_PRONE && (pevAttacker->flags & FL_CLIENT) ) + { + m_flPlayerDamage += flDamage; + + // This is a heurstic to determine if the player intended to harm me + // If I have an enemy, we can't establish intent (may just be crossfire) + if ( m_hEnemy == NULL ) + { + // If the player was facing directly at me, or I'm already suspicious, get mad + if ( (m_afMemory & bits_MEMORY_SUSPICIOUS) || IsFacing( pevAttacker, pev->origin ) ) + { + // Alright, now I'm pissed! + if (m_iszSpeakAs) + { + char szBuf[32]; + strcpy(szBuf,STRING(m_iszSpeakAs)); + strcat(szBuf,"_MAD"); + PlaySentence( szBuf, 4, VOL_NORM, ATTN_NORM ); + } + else + { + PlaySentence( "BA_MAD", 4, VOL_NORM, ATTN_NORM ); + } + + Remember( bits_MEMORY_PROVOKED ); + StopFollowing( TRUE ); + } + else + { + // Hey, be careful with that + if (m_iszSpeakAs) + { + char szBuf[32]; + strcpy(szBuf,STRING(m_iszSpeakAs)); + strcat(szBuf,"_SHOT"); + PlaySentence( szBuf, 4, VOL_NORM, ATTN_NORM ); + } + else + { + PlaySentence( "BA_SHOT", 4, VOL_NORM, ATTN_NORM ); + } + Remember( bits_MEMORY_SUSPICIOUS ); + } + } + else if ( !(m_hEnemy->IsPlayer()) && pev->deadflag == DEAD_NO ) + { + if (m_iszSpeakAs) + { + char szBuf[32]; + strcpy(szBuf,STRING(m_iszSpeakAs)); + strcat(szBuf,"_SHOT"); + PlaySentence( szBuf, 4, VOL_NORM, ATTN_NORM ); + } + else + { + PlaySentence( "BA_SHOT", 4, VOL_NORM, ATTN_NORM ); + } + } + } + + return ret; +} + + +//========================================================= +// PainSound +//========================================================= +void CBarney :: PainSound ( void ) +{ + if (gpGlobals->time < m_painTime) + return; + + m_painTime = gpGlobals->time + RANDOM_FLOAT(0.5, 0.75); + + switch (RANDOM_LONG(0,2)) + { + case 0: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "barney/ba_pain1.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 1: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "barney/ba_pain2.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 2: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "barney/ba_pain3.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + } +} + +//========================================================= +// DeathSound +//========================================================= +void CBarney :: DeathSound ( void ) +{ + switch (RANDOM_LONG(0,2)) + { + case 0: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "barney/ba_die1.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 1: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "barney/ba_die2.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 2: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "barney/ba_die3.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + } +} + + +void CBarney::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + switch( ptr->iHitgroup) + { + case HITGROUP_CHEST: + case HITGROUP_STOMACH: + if (bitsDamageType & (DMG_BULLET | DMG_SLASH | DMG_BLAST)) + { + flDamage = flDamage / 2; + } + break; + case 10: + if (bitsDamageType & (DMG_BULLET | DMG_SLASH | DMG_CLUB)) + { + flDamage -= 20; + if (flDamage <= 0) + { + UTIL_Ricochet( ptr->vecEndPos, 1.0 ); + flDamage = 0.01; + } + } + // always a head shot + ptr->iHitgroup = HITGROUP_HEAD; + break; + } + + CTalkMonster::TraceAttack( pevAttacker, flDamage, vecDir, ptr, bitsDamageType ); +} + + +void CBarney::Killed( entvars_t *pevAttacker, int iGib ) +{ + if ( pev->body < m_iBaseBody + BARNEY_BODY_GUNGONE && !(pev->spawnflags & SF_MONSTER_NO_WPN_DROP)) + { // drop the gun! + Vector vecGunPos; + Vector vecGunAngles; + + pev->body = m_iBaseBody + BARNEY_BODY_GUNGONE; + + GetAttachment( 0, vecGunPos, vecGunAngles ); + + CBaseEntity *pGun; + pGun = DropItem( "weapon_glock", vecGunPos, vecGunAngles ); + } + + SetUse( NULL ); + CTalkMonster::Killed( pevAttacker, iGib ); +} + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= + +Schedule_t* CBarney :: GetScheduleOfType ( int Type ) +{ + Schedule_t *psched; + + switch( Type ) + { + case SCHED_ARM_WEAPON: + if ( m_hEnemy != NULL ) + { + // face enemy, then draw. + return slBarneyEnemyDraw; + } + break; + + // Hook these to make a looping schedule + case SCHED_TARGET_FACE: + // call base class default so that barney will talk + // when 'used' + psched = CTalkMonster::GetScheduleOfType(Type); + + if (psched == slIdleStand) + return slBaFaceTarget; // override this for different target face behavior + else + return psched; + + case SCHED_TARGET_CHASE: + return slBaFollow; + + case SCHED_IDLE_STAND: + // call base class default so that scientist will talk + // when standing during idle + psched = CTalkMonster::GetScheduleOfType(Type); + + if (psched == slIdleStand) + { + // just look straight ahead. + return slIdleBaStand; + } + else + return psched; + } + + return CTalkMonster::GetScheduleOfType( Type ); +} + +//========================================================= +// GetSchedule - Decides which type of schedule best suits +// the monster's current state and conditions. Then calls +// monster's member function to get a pointer to a schedule +// of the proper type. +//========================================================= +Schedule_t *CBarney :: GetSchedule ( void ) +{ + if ( HasConditions( bits_COND_HEAR_SOUND ) ) + { + CSound *pSound; + pSound = PBestSound(); + + ASSERT( pSound != NULL ); + if ( pSound && (pSound->m_iType & bits_SOUND_DANGER) ) + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_BEST_SOUND ); + } + if ( HasConditions( bits_COND_ENEMY_DEAD ) && FOkToSpeak() ) + { + // Hey, be careful with that + if (m_iszSpeakAs) + { + char szBuf[32]; + strcpy(szBuf,STRING(m_iszSpeakAs)); + strcat(szBuf,"_KILL"); + PlaySentence( szBuf, 4, VOL_NORM, ATTN_NORM ); + } + else + { + PlaySentence( "BA_KILL", 4, VOL_NORM, ATTN_NORM ); + } + } + + switch( m_MonsterState ) + { + case MONSTERSTATE_COMBAT: + { +// dead enemy + if ( HasConditions( bits_COND_ENEMY_DEAD ) ) + { + // call base class, all code to handle dead enemies is centralized there. + return CBaseMonster :: GetSchedule(); + } + + // always act surprized with a new enemy + if ( HasConditions( bits_COND_NEW_ENEMY ) && HasConditions( bits_COND_LIGHT_DAMAGE) ) + return GetScheduleOfType( SCHED_SMALL_FLINCH ); + + // wait for one schedule to draw gun + if (!m_fGunDrawn ) + return GetScheduleOfType( SCHED_ARM_WEAPON ); + + if ( HasConditions( bits_COND_HEAVY_DAMAGE ) ) + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_ENEMY ); + } + break; + + case MONSTERSTATE_ALERT: + case MONSTERSTATE_IDLE: + if ( HasConditions(bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE)) + { + // flinch if hurt + return GetScheduleOfType( SCHED_SMALL_FLINCH ); + } + + if ( m_hEnemy == NULL && IsFollowing() ) + { + if ( !m_hTargetEnt->IsAlive() ) + { + // UNDONE: Comment about the recently dead player here? + StopFollowing( FALSE ); + break; + } + else + { + if ( HasConditions( bits_COND_CLIENT_PUSH ) ) + { + return GetScheduleOfType( SCHED_MOVE_AWAY_FOLLOW ); + } + return GetScheduleOfType( SCHED_TARGET_FACE ); + } + } + + if ( HasConditions( bits_COND_CLIENT_PUSH ) ) + { + return GetScheduleOfType( SCHED_MOVE_AWAY ); + } + + // try to say something about smells + TrySmellTalk(); + break; + } + + return CTalkMonster::GetSchedule(); +} + +MONSTERSTATE CBarney :: GetIdealState ( void ) +{ + return CTalkMonster::GetIdealState(); +} + + + +void CBarney::DeclineFollowing( void ) +{ + PlaySentence( m_szGrp[TLK_DECLINE], 2, VOL_NORM, ATTN_NORM ); //LRC +} + + + + + +//========================================================= +// DEAD BARNEY PROP +// +// Designer selects a pose in worldcraft, 0 through num_poses-1 +// this value is added to what is selected as the 'first dead pose' +// among the monster's normal animations. All dead poses must +// appear sequentially in the model file. Be sure and set +// the m_iFirstPose properly! +// +//========================================================= +class CDeadBarney : public CBaseMonster +{ +public: + void Spawn( void ); + int Classify ( void ) { return CLASS_PLAYER_ALLY; } + + void KeyValue( KeyValueData *pkvd ); + + int m_iPose;// which sequence to display -- temporary, don't need to save + static char *m_szPoses[3]; +}; + +char *CDeadBarney::m_szPoses[] = { "lying_on_back", "lying_on_side", "lying_on_stomach" }; + +void CDeadBarney::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "pose")) + { + m_iPose = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseMonster::KeyValue( pkvd ); +} + +LINK_ENTITY_TO_CLASS( monster_barney_dead, CDeadBarney ); + +//========================================================= +// ********** DeadBarney SPAWN ********** +//========================================================= +void CDeadBarney :: Spawn( ) +{ + PRECACHE_MODEL("models/barney.mdl"); + SET_MODEL(ENT(pev), "models/barney.mdl"); + + pev->effects = 0; + pev->yaw_speed = 8; + pev->sequence = 0; + m_bloodColor = BLOOD_COLOR_RED; + + pev->sequence = LookupSequence( m_szPoses[m_iPose] ); + if (pev->sequence == -1) + { + ALERT ( at_debug, "Dead barney with bad pose\n" ); + } + // Corpses have less health + pev->health = DEAD_BARNEY_HEALTH; + + MonsterInitDead(); +} + + diff --git a/server/monsters/baseanimating.h b/server/monsters/baseanimating.h new file mode 100644 index 00000000..d27a710d --- /dev/null +++ b/server/monsters/baseanimating.h @@ -0,0 +1,50 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +#ifndef BASEANIMATING_H +#define BASEANIMATING_H + +class CBaseAnimating : public CBaseLogic +{ +public: + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static TYPEDESCRIPTION m_SaveData[]; + + // Basic Monster Animation functions + float StudioFrameAdvance( float flInterval = 0.0 ); + int GetSequenceFlags( void ); + int LookupActivity ( int activity ); + int LookupActivityHeaviest ( int activity ); + int LookupSequence ( const char *label ); + float SequenceDuration( int iSequence ); + void ResetSequenceInfo ( ); + void DispatchAnimEvents ( float flFutureInterval = 0.1 ); + virtual CBaseAnimating* GetBaseAnimating() { return this; } + virtual void HandleAnimEvent( MonsterEvent_t *pEvent ) { return; }; + float SetBoneController ( int iController, float flValue ); + void InitBoneControllers ( void ); + float SetBlending ( int iBlender, float flValue ); + void GetBonePosition ( int iBone, Vector &origin, Vector &angles ); + void GetAutomovement( Vector &origin, Vector &angles, float flInterval = 0.1 ); + int FindTransition( int iEndingSequence, int iGoalSequence, int *piDir ); + BOOL GetAttachment ( int iAttachment, Vector &origin, Vector &angles ); + void SetBodygroup( int iGroup, int iValue ); + int GetBodygroup( int iGroup ); + int GetBoneCount( void ); + void SetBones( float (*data)[3], int datasize ); + + int ExtractBbox( int sequence, float *mins, float *maxs ); + void SetSequenceBox( void ); + + // animation needs + float m_flFrameRate; // computed FPS for current sequence + float m_flGroundSpeed; // computed linear movement rate for current sequence + float m_flLastEventCheck; // last time the event list was checked + BOOL m_fSequenceFinished;// flag set when StudioAdvanceFrame moves across a frame boundry + BOOL m_fSequenceLoops; // true if the sequence loops +}; + +#endif //BASEANIMATING_H \ No newline at end of file diff --git a/server/monsters/basemonster.cpp b/server/monsters/basemonster.cpp new file mode 100644 index 00000000..2cdc24c3 --- /dev/null +++ b/server/monsters/basemonster.cpp @@ -0,0 +1,6233 @@ +/*** +* +* Copyright (c) 1999, 2000 Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +/* + +===== monsters.cpp ======================================================== + + Monster-related utility code + +*/ + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "nodes.h" +#include "monsters.h" +#include "animation.h" +#include "saverestore.h" +#include "baseweapon.h" +#include "scripted.h" +#include "squadmonster.h" +#include "defaultai.h" +#include "decals.h" +#include "soundent.h" +#include "gamerules.h" +#include "game.h" + +#define MONSTER_CUT_CORNER_DIST 8 // 8 means the monster's bounding box is contained without the box of the node in WC +extern CGraph WorldGraph; + +Vector VecBModelOrigin( entvars_t* pevBModel ); +extern entvars_t *g_pevLastInflictor; +extern DLL_GLOBAL BOOL g_fDrawLines; +extern CGraph WorldGraph;// the world node graph +DLL_GLOBAL BOOL g_fDrawLines = FALSE; + + +// Global Savedata for monster +// UNDONE: Save schedule data? Can this be done? We may +// lose our enemy pointer or other data (goal ent, target, etc) +// that make the current schedule invalid, perhaps it's best +// to just pick a new one when we start up again. +TYPEDESCRIPTION CBaseMonster::m_SaveData[] = +{ + DEFINE_FIELD( CBaseMonster, m_hEnemy, FIELD_EHANDLE ), + DEFINE_FIELD( CBaseMonster, m_hTargetEnt, FIELD_EHANDLE ), + DEFINE_ARRAY( CBaseMonster, m_hOldEnemy, FIELD_EHANDLE, MAX_OLD_ENEMIES ), + DEFINE_ARRAY( CBaseMonster, m_vecOldEnemy, FIELD_POSITION_VECTOR, MAX_OLD_ENEMIES ), + + DEFINE_FIELD( CBaseMonster, m_iClass, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_iPlayerReact, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseMonster, m_flFieldOfView, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMonster, m_flWaitFinished, FIELD_TIME ), + DEFINE_FIELD( CBaseMonster, m_flMoveWaitFinished, FIELD_TIME ), + + DEFINE_FIELD( CBaseMonster, m_Activity, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_IdealActivity, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_LastHitGroup, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_MonsterState, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_IdealMonsterState, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_iTaskStatus, FIELD_INTEGER ), + + //Schedule_t *m_pSchedule; + + DEFINE_FIELD( CBaseMonster, m_iScheduleIndex, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_afConditions, FIELD_INTEGER ), + //WayPoint_t m_Route[ ROUTE_SIZE ]; +// DEFINE_FIELD( CBaseMonster, m_movementGoal, FIELD_INTEGER ), +// DEFINE_FIELD( CBaseMonster, m_iRouteIndex, FIELD_INTEGER ), +// DEFINE_FIELD( CBaseMonster, m_moveWaitTime, FIELD_FLOAT ), + + DEFINE_FIELD( CBaseMonster, m_vecMoveGoal, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMonster, m_movementActivity, FIELD_INTEGER ), + + // int m_iAudibleList; // first index of a linked list of sounds that the monster can hear. +// DEFINE_FIELD( CBaseMonster, m_afSoundTypes, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_vecLastPosition, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMonster, m_iHintNode, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_afMemory, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_iMaxHealth, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseMonster, m_vecEnemyLKP, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseMonster, m_cAmmoLoaded, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_afCapability, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseMonster, m_flNextAttack, FIELD_TIME ), + DEFINE_FIELD( CBaseMonster, m_bitsDamageType, FIELD_INTEGER ), + DEFINE_ARRAY( CBaseMonster, m_rgbTimeBasedDamage, FIELD_CHARACTER, CDMG_TIMEBASED ), + DEFINE_FIELD( CBaseMonster, m_bloodColor, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_failSchedule, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseMonster, m_flHungryTime, FIELD_TIME ), + DEFINE_FIELD( CBaseMonster, m_flDistTooFar, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMonster, m_flDistLook, FIELD_FLOAT ), + DEFINE_FIELD( CBaseMonster, m_iTriggerCondition, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_iszTriggerTarget, FIELD_STRING ), + + DEFINE_FIELD( CBaseMonster, m_HackedGunPos, FIELD_VECTOR ), + + DEFINE_FIELD( CBaseMonster, m_scriptState, FIELD_INTEGER ), + DEFINE_FIELD( CBaseMonster, m_pCine, FIELD_CLASSPTR ), +}; + +//IMPLEMENT_SAVERESTORE( CBaseMonster, CBaseAnimating ); +int CBaseMonster::Save( CSave &save ) +{ + if ( !CBaseAnimating::Save(save) ) + return 0; + if ( pev->targetname ) + return save.WriteFields( STRING(pev->targetname), "CBaseMonster", this, m_SaveData, ARRAYSIZE(m_SaveData) ); + else + return save.WriteFields( STRING(pev->classname), "CBaseMonster", this, m_SaveData, ARRAYSIZE(m_SaveData) ); +} + +int CBaseMonster::Restore( CRestore &restore ) +{ + if ( !CBaseAnimating::Restore(restore) ) + return 0; + int status = restore.ReadFields( "CBaseMonster", this, m_SaveData, ARRAYSIZE(m_SaveData) ); + + // We don't save/restore routes yet + RouteClear(); + + // We don't save/restore schedules yet + m_pSchedule = NULL; + m_iTaskStatus = TASKSTATUS_NEW; + + // Reset animation + m_Activity = ACT_RESET; + + // If we don't have an enemy, clear conditions like see enemy, etc. + if ( m_hEnemy == NULL ) + m_afConditions = 0; + + return status; +} + + +//========================================================= +// Eat - makes a monster full for a little while. +//========================================================= +void CBaseMonster :: Eat ( float flFullDuration ) +{ + m_flHungryTime = gpGlobals->time + flFullDuration; +} + +//========================================================= +// FShouldEat - returns true if a monster is hungry. +//========================================================= +BOOL CBaseMonster :: FShouldEat ( void ) +{ + if ( m_flHungryTime > gpGlobals->time ) + { + return FALSE; + } + + return TRUE; +} + +//========================================================= +// BarnacleVictimBitten - called +// by Barnacle victims when the barnacle pulls their head +// into its mouth +//========================================================= +void CBaseMonster :: BarnacleVictimBitten ( entvars_t *pevBarnacle ) +{ + Schedule_t *pNewSchedule; + + pNewSchedule = GetScheduleOfType( SCHED_BARNACLE_VICTIM_CHOMP ); + + if ( pNewSchedule ) + { + ChangeSchedule( pNewSchedule ); + } +} + +//========================================================= +// BarnacleVictimReleased - called by barnacle victims when +// the host barnacle is killed. +//========================================================= +void CBaseMonster :: BarnacleVictimReleased ( void ) +{ + m_IdealMonsterState = MONSTERSTATE_IDLE; + + pev->velocity = g_vecZero; + pev->movetype = MOVETYPE_STEP; +} + +//========================================================= +// Listen - monsters dig through the active sound list for +// any sounds that may interest them. (smells, too!) +//========================================================= +void CBaseMonster :: Listen ( void ) +{ + int iSound; + int iMySounds; + float hearingSensitivity; + CSound *pCurrentSound; + + m_iAudibleList = SOUNDLIST_EMPTY; + ClearConditions(bits_COND_HEAR_SOUND | bits_COND_SMELL | bits_COND_SMELL_FOOD); + m_afSoundTypes = 0; + + iMySounds = ISoundMask(); + + if ( m_pSchedule ) + { + //!!!WATCH THIS SPOT IF YOU ARE HAVING SOUND RELATED BUGS! + // Make sure your schedule AND personal sound masks agree! + iMySounds &= m_pSchedule->iSoundMask; + } + + iSound = CSoundEnt::ActiveList(); + + // UNDONE: Clear these here? + ClearConditions( bits_COND_HEAR_SOUND | bits_COND_SMELL_FOOD | bits_COND_SMELL ); + hearingSensitivity = HearingSensitivity( ); + + while ( iSound != SOUNDLIST_EMPTY ) + { + pCurrentSound = CSoundEnt::SoundPointerForIndex( iSound ); + + if ( pCurrentSound && + ( pCurrentSound->m_iType & iMySounds ) && + ( pCurrentSound->m_vecOrigin - EarPosition() ).Length() <= pCurrentSound->m_iVolume * hearingSensitivity ) + + //if ( ( g_pSoundEnt->m_SoundPool[ iSound ].m_iType & iMySounds ) && ( g_pSoundEnt->m_SoundPool[ iSound ].m_vecOrigin - EarPosition()).Length () <= g_pSoundEnt->m_SoundPool[ iSound ].m_iVolume * hearingSensitivity ) + { + // the monster cares about this sound, and it's close enough to hear. + //g_pSoundEnt->m_SoundPool[ iSound ].m_iNextAudible = m_iAudibleList; + pCurrentSound->m_iNextAudible = m_iAudibleList; + + if ( pCurrentSound->FIsSound() ) + { + // this is an audible sound. + SetConditions( bits_COND_HEAR_SOUND ); + } + else + { + // if not a sound, must be a smell - determine if it's just a scent, or if it's a food scent +// if ( g_pSoundEnt->m_SoundPool[ iSound ].m_iType & ( bits_SOUND_MEAT | bits_SOUND_CARCASS ) ) + if ( pCurrentSound->m_iType & ( bits_SOUND_MEAT | bits_SOUND_CARCASS ) ) + { + // the detected scent is a food item, so set both conditions. + // !!!BUGBUG - maybe a virtual function to determine whether or not the scent is food? + SetConditions( bits_COND_SMELL_FOOD ); + SetConditions( bits_COND_SMELL ); + } + else + { + // just a normal scent. + SetConditions( bits_COND_SMELL ); + } + } + +// m_afSoundTypes |= g_pSoundEnt->m_SoundPool[ iSound ].m_iType; + m_afSoundTypes |= pCurrentSound->m_iType; + + m_iAudibleList = iSound; + } + +// iSound = g_pSoundEnt->m_SoundPool[ iSound ].m_iNext; + iSound = pCurrentSound->m_iNext; + } +} + +//========================================================= +// FLSoundVolume - subtracts the volume of the given sound +// from the distance the sound source is from the caller, +// and returns that value, which is considered to be the 'local' +// volume of the sound. +//========================================================= +float CBaseMonster :: FLSoundVolume ( CSound *pSound ) +{ + return ( pSound->m_iVolume - ( ( pSound->m_vecOrigin - pev->origin ).Length() ) ); +} + +//========================================================= +// FValidateHintType - tells use whether or not the monster cares +// about the type of Hint Node given +//========================================================= +BOOL CBaseMonster :: FValidateHintType ( short sHint ) +{ + return FALSE; +} + +//========================================================= +// Look - Base class monster function to find enemies or +// food by sight. iDistance is distance ( in units ) that the +// monster can see. +// +// Sets the sight bits of the m_afConditions mask to indicate +// which types of entities were sighted. +// Function also sets the Looker's m_pLink +// to the head of a link list that contains all visible ents. +// (linked via each ent's m_pLink field) +// +//========================================================= +void CBaseMonster :: Look ( int iDistance ) +{ + int iSighted = 0; + + // DON'T let visibility information from last frame sit around! + ClearConditions(bits_COND_SEE_HATE | bits_COND_SEE_DISLIKE | bits_COND_SEE_ENEMY | bits_COND_SEE_FEAR | bits_COND_SEE_NEMESIS | bits_COND_SEE_CLIENT); + + m_pLink = NULL; + + CBaseEntity *pSightEnt = NULL;// the current visible entity that we're dealing with + + // See no evil if prisoner is set + if ( !FBitSet( pev->spawnflags, SF_MONSTER_PRISONER ) ) + { + CBaseEntity *pList[100]; + + Vector delta = Vector( iDistance, iDistance, iDistance ); + + // Find only monsters/clients in box, NOT limited to PVS + int count = UTIL_EntitiesInBox( pList, 100, pev->origin - delta, pev->origin + delta, FL_CLIENT|FL_MONSTER ); + for ( int i = 0; i < count; i++ ) + { + pSightEnt = pList[i]; + // !!!temporarily only considering other monsters and clients, don't see prisoners + + if ( pSightEnt != this && + !FBitSet( pSightEnt->pev->spawnflags, SF_MONSTER_PRISONER ) && + pSightEnt->pev->health > 0 ) + { + // the looker will want to consider this entity + // don't check anything else about an entity that can't be seen, or an entity that you don't care about. + if ( IRelationship( pSightEnt ) != R_NO && FInViewCone( pSightEnt ) && !FBitSet( pSightEnt->pev->flags, FL_NOTARGET ) && FVisible( pSightEnt ) ) + { + if ( pSightEnt->IsPlayer() ) + { + if ( pev->spawnflags & SF_MONSTER_WAIT_TILL_SEEN ) + { + CBaseMonster *pClient; + + pClient = pSightEnt->MyMonsterPointer(); + // don't link this client in the list if the monster is wait till seen and the player isn't facing the monster + if ( pSightEnt && !pClient->FInViewCone( this ) ) + { + // we're not in the player's view cone. + continue; + } + else + { + // player sees us, become normal now. + pev->spawnflags &= ~SF_MONSTER_WAIT_TILL_SEEN; + } + } + + // if we see a client, remember that (mostly for scripted AI) + iSighted |= bits_COND_SEE_CLIENT; + } + + pSightEnt->m_pLink = m_pLink; + m_pLink = pSightEnt; + + if ( pSightEnt == m_hEnemy ) + { + // we know this ent is visible, so if it also happens to be our enemy, store that now. + iSighted |= bits_COND_SEE_ENEMY; + } + + // don't add the Enemy's relationship to the conditions. We only want to worry about conditions when + // we see monsters other than the Enemy. + switch ( IRelationship ( pSightEnt ) ) + { + case R_NM: + iSighted |= bits_COND_SEE_NEMESIS; + break; + case R_HT: + iSighted |= bits_COND_SEE_HATE; + break; + case R_DL: + iSighted |= bits_COND_SEE_DISLIKE; + break; + case R_FR: + iSighted |= bits_COND_SEE_FEAR; + break; + case R_AL: + break; + default: + ALERT ( at_aiconsole, "%s can't assess %s\n", STRING(pev->classname), STRING(pSightEnt->pev->classname ) ); + break; + } + } + } + } + } + + SetConditions( iSighted ); +} + +//========================================================= +// ISoundMask - returns a bit mask indicating which types +// of sounds this monster regards. In the base class implementation, +// monsters care about all sounds, but no scents. +//========================================================= +int CBaseMonster :: ISoundMask ( void ) +{ + return bits_SOUND_WORLD | + bits_SOUND_COMBAT | + bits_SOUND_PLAYER; +} + +//========================================================= +// PBestSound - returns a pointer to the sound the monster +// should react to. Right now responds only to nearest sound. +//========================================================= +CSound* CBaseMonster :: PBestSound ( void ) +{ + int iThisSound; + int iBestSound = -1; + float flBestDist = 8192;// so first nearby sound will become best so far. + float flDist; + CSound *pSound; + + iThisSound = m_iAudibleList; + + if ( iThisSound == SOUNDLIST_EMPTY ) + { + ALERT ( at_aiconsole, "ERROR! monster %s has no audible sounds!\n", STRING(pev->classname) ); +#if _DEBUG + ALERT( at_error, "NULL Return from PBestSound\n" ); +#endif + return NULL; + } + + while ( iThisSound != SOUNDLIST_EMPTY ) + { + pSound = CSoundEnt::SoundPointerForIndex( iThisSound ); + + if ( pSound && pSound->FIsSound() ) + { + flDist = ( pSound->m_vecOrigin - EarPosition()).Length(); + + if ( flDist < flBestDist ) + { + iBestSound = iThisSound; + flBestDist = flDist; + } + } + + iThisSound = pSound->m_iNextAudible; + } + if ( iBestSound >= 0 ) + { + pSound = CSoundEnt::SoundPointerForIndex( iBestSound ); + return pSound; + } +#if _DEBUG + ALERT( at_error, "NULL Return from PBestSound\n" ); +#endif + return NULL; +} + +//========================================================= +// PBestScent - returns a pointer to the scent the monster +// should react to. Right now responds only to nearest scent +//========================================================= +CSound* CBaseMonster :: PBestScent ( void ) +{ + int iThisScent; + int iBestScent = -1; + float flBestDist = 8192;// so first nearby smell will become best so far. + float flDist; + CSound *pSound; + + iThisScent = m_iAudibleList;// smells are in the sound list. + + if ( iThisScent == SOUNDLIST_EMPTY ) + { + ALERT ( at_aiconsole, "ERROR! PBestScent() has empty soundlist!\n" ); +#if _DEBUG + ALERT( at_error, "NULL Return from PBestSound\n" ); +#endif + return NULL; + } + + while ( iThisScent != SOUNDLIST_EMPTY ) + { + pSound = CSoundEnt::SoundPointerForIndex( iThisScent ); + + if ( pSound->FIsScent() ) + { + flDist = ( pSound->m_vecOrigin - pev->origin ).Length(); + + if ( flDist < flBestDist ) + { + iBestScent = iThisScent; + flBestDist = flDist; + } + } + + iThisScent = pSound->m_iNextAudible; + } + if ( iBestScent >= 0 ) + { + pSound = CSoundEnt::SoundPointerForIndex( iBestScent ); + + return pSound; + } + return NULL; +} + + + +//========================================================= +// Monster Think - calls out to core AI functions and handles this +// monster's specific animation events +//========================================================= +void CBaseMonster :: MonsterThink ( void ) +{ + SetNextThink( 0.05 );// keep monster thinking. + + RunAI(); + + float flInterval = StudioFrameAdvance( ); // animate + +// start or end a fidget +// This needs a better home -- switching animations over time should be encapsulated on a per-activity basis +// perhaps MaintainActivity() or a ShiftAnimationOverTime() or something. + + if ( m_MonsterState != MONSTERSTATE_SCRIPT && m_MonsterState != MONSTERSTATE_DEAD && m_Activity == ACT_IDLE && m_fSequenceFinished ) + { + int iSequence; + + if ( m_fSequenceLoops ) + { + // animation does loop, which means we're playing subtle idle. Might need to + // fidget. + iSequence = LookupActivity ( m_Activity ); + } + else + { + // animation that just ended doesn't loop! That means we just finished a fidget + // and should return to our heaviest weighted idle (the subtle one) + iSequence = LookupActivityHeaviest ( m_Activity ); + } + if ( iSequence != ACTIVITY_NOT_AVAILABLE ) + { + pev->sequence = iSequence; // Set to new anim (if it's there) + ResetSequenceInfo( ); + } + } + + DispatchAnimEvents( flInterval ); + + if ( !MovementIsComplete())Move( flInterval ); +} + +//========================================================= +// CBaseMonster - USE - will make a monster angry at whomever +// activated it. +//========================================================= +void CBaseMonster :: MonsterUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + m_IdealMonsterState = MONSTERSTATE_ALERT; +} + +//========================================================= +// Ignore conditions - before a set of conditions is allowed +// to interrupt a monster's schedule, this function removes +// conditions that we have flagged to interrupt the current +// schedule, but may not want to interrupt the schedule every +// time. (Pain, for instance) +//========================================================= +int CBaseMonster :: IgnoreConditions ( void ) +{ + int iIgnoreConditions = 0; + + if ( !FShouldEat() ) + { + // not hungry? Ignore food smell. + iIgnoreConditions |= bits_COND_SMELL_FOOD; + } + + if ( m_MonsterState == MONSTERSTATE_SCRIPT && m_pCine ) + iIgnoreConditions |= m_pCine->IgnoreConditions(); + + return iIgnoreConditions; +} + +//========================================================= +// RouteClear - zeroes out the monster's route array and goal +//========================================================= +void CBaseMonster :: RouteClear ( void ) +{ + RouteNew(); + m_movementGoal = MOVEGOAL_NONE; + m_movementActivity = ACT_IDLE; + Forget( bits_MEMORY_MOVE_FAILED ); +} + +//========================================================= +// Route New - clears out a route to be changed, but keeps +// goal intact. +//========================================================= +void CBaseMonster :: RouteNew ( void ) +{ + m_Route[ 0 ].iType = 0; + m_iRouteIndex = 0; +} + +//========================================================= +// FRouteClear - returns TRUE if the Route is cleared out +// ( invalid ) +//========================================================= +BOOL CBaseMonster :: FRouteClear ( void ) +{ + if ( m_Route[ m_iRouteIndex ].iType == 0 || m_movementGoal == MOVEGOAL_NONE ) + return TRUE; + + return FALSE; +} + +//========================================================= +// FRefreshRoute - after calculating a path to the monster's +// target, this function copies as many waypoints as possible +// from that path to the monster's Route array +//========================================================= +BOOL CBaseMonster :: FRefreshRoute ( void ) +{ + CBaseEntity *pPathCorner; + int i; + BOOL returnCode; + + RouteNew(); + + returnCode = FALSE; + + switch( m_movementGoal ) + { + case MOVEGOAL_PATHCORNER: + { + // monster is on a path_corner loop + pPathCorner = GetNext(); + i = 0; + + while ( pPathCorner && i < ROUTE_SIZE ) + { + m_Route[ i ].iType = bits_MF_TO_PATHCORNER; + m_Route[ i ].vecLocation = pPathCorner->pev->origin; + + pPathCorner = pPathCorner->GetNext(); + + // Last path_corner in list? + if ( !pPathCorner ) + m_Route[i].iType |= bits_MF_IS_GOAL; + + i++; + } + } + returnCode = TRUE; + break; + + case MOVEGOAL_ENEMY: + returnCode = BuildRoute( m_vecEnemyLKP, bits_MF_TO_ENEMY, m_hEnemy ); + break; + + case MOVEGOAL_LOCATION: + returnCode = BuildRoute( m_vecMoveGoal, bits_MF_TO_LOCATION, NULL ); + break; + + case MOVEGOAL_TARGETENT: + if (m_hTargetEnt != NULL) + { + returnCode = BuildRoute( m_hTargetEnt->pev->origin, bits_MF_TO_TARGETENT, m_hTargetEnt ); + } + break; + + case MOVEGOAL_NODE: + returnCode = FGetNodeRoute( m_vecMoveGoal ); +// if ( returnCode ) +// RouteSimplify( NULL ); + break; + } + + return returnCode; +} + + +BOOL CBaseMonster::MoveToEnemy( Activity movementAct, float waitTime ) +{ + m_movementActivity = movementAct; + m_moveWaitTime = waitTime; + + m_movementGoal = MOVEGOAL_ENEMY; + return FRefreshRoute(); +} + + +BOOL CBaseMonster::MoveToLocation( Activity movementAct, float waitTime, const Vector &goal ) +{ + m_movementActivity = movementAct; + m_moveWaitTime = waitTime; + + m_movementGoal = MOVEGOAL_LOCATION; + m_vecMoveGoal = goal; + return FRefreshRoute(); +} + + +BOOL CBaseMonster::MoveToTarget( Activity movementAct, float waitTime ) +{ + m_movementActivity = movementAct; + m_moveWaitTime = waitTime; + + m_movementGoal = MOVEGOAL_TARGETENT; + return FRefreshRoute(); +} + + +BOOL CBaseMonster::MoveToNode( Activity movementAct, float waitTime, const Vector &goal ) +{ + m_movementActivity = movementAct; + m_moveWaitTime = waitTime; + + m_movementGoal = MOVEGOAL_NODE; + m_vecMoveGoal = goal; + return FRefreshRoute(); +} + + +#ifdef _DEBUG +void DrawRoute( entvars_t *pev, WayPoint_t *m_Route, int m_iRouteIndex, int r, int g, int b ) +{ + int i; + + if ( m_Route[m_iRouteIndex].iType == 0 ) + { + ALERT( at_aiconsole, "Can't draw route!\n" ); + return; + } + +// UTIL_ParticleEffect ( m_Route[ m_iRouteIndex ].vecLocation, g_vecZero, 255, 25 ); + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BEAMPOINTS); + WRITE_COORD( pev->origin.x ); + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z ); + WRITE_COORD( m_Route[ m_iRouteIndex ].vecLocation.x ); + WRITE_COORD( m_Route[ m_iRouteIndex ].vecLocation.y ); + WRITE_COORD( m_Route[ m_iRouteIndex ].vecLocation.z ); + + WRITE_SHORT( g_sModelIndexLaser ); + WRITE_BYTE( 0 ); // frame start + WRITE_BYTE( 10 ); // framerate + WRITE_BYTE( 1 ); // life + WRITE_BYTE( 16 ); // width + WRITE_BYTE( 0 ); // noise + WRITE_BYTE( r ); // r, g, b + WRITE_BYTE( g ); // r, g, b + WRITE_BYTE( b ); // r, g, b + WRITE_BYTE( 255 ); // brightness + WRITE_BYTE( 10 ); // speed + MESSAGE_END(); + + for ( i = m_iRouteIndex ; i < ROUTE_SIZE - 1; i++ ) + { + if ( (m_Route[ i ].iType & bits_MF_IS_GOAL) || (m_Route[ i+1 ].iType == 0) ) + break; + + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BEAMPOINTS ); + WRITE_COORD( m_Route[ i ].vecLocation.x ); + WRITE_COORD( m_Route[ i ].vecLocation.y ); + WRITE_COORD( m_Route[ i ].vecLocation.z ); + WRITE_COORD( m_Route[ i + 1 ].vecLocation.x ); + WRITE_COORD( m_Route[ i + 1 ].vecLocation.y ); + WRITE_COORD( m_Route[ i + 1 ].vecLocation.z ); + WRITE_SHORT( g_sModelIndexLaser ); + WRITE_BYTE( 0 ); // frame start + WRITE_BYTE( 10 ); // framerate + WRITE_BYTE( 1 ); // life + WRITE_BYTE( 8 ); // width + WRITE_BYTE( 0 ); // noise + WRITE_BYTE( r ); // r, g, b + WRITE_BYTE( g ); // r, g, b + WRITE_BYTE( b ); // r, g, b + WRITE_BYTE( 255 ); // brightness + WRITE_BYTE( 10 ); // speed + MESSAGE_END(); + +// UTIL_ParticleEffect ( m_Route[ i ].vecLocation, g_vecZero, 255, 25 ); + } +} +#endif + + +int ShouldSimplify( int routeType ) +{ + routeType &= ~bits_MF_IS_GOAL; + + if ( (routeType == bits_MF_TO_PATHCORNER) || (routeType & bits_MF_DONT_SIMPLIFY) ) + return FALSE; + return TRUE; +} + +//========================================================= +// RouteSimplify +// +// Attempts to make the route more direct by cutting out +// unnecessary nodes & cutting corners. +// +//========================================================= +void CBaseMonster :: RouteSimplify( CBaseEntity *pTargetEnt ) +{ + // BUGBUG: this doesn't work 100% yet + int i, count, outCount; + Vector vecStart; + WayPoint_t outRoute[ ROUTE_SIZE * 2 ]; // Any points except the ends can turn into 2 points in the simplified route + + count = 0; + + for ( i = m_iRouteIndex; i < ROUTE_SIZE; i++ ) + { + if ( !m_Route[i].iType ) + break; + else + count++; + if ( m_Route[i].iType & bits_MF_IS_GOAL ) + break; + } + // Can't simplify a direct route! + if ( count < 2 ) + { +// DrawRoute( pev, m_Route, m_iRouteIndex, 0, 0, 255 ); + return; + } + + outCount = 0; + vecStart = pev->origin; + for ( i = 0; i < count-1; i++ ) + { + // Don't eliminate path_corners + if ( !ShouldSimplify( m_Route[m_iRouteIndex+i].iType ) ) + { + outRoute[outCount] = m_Route[ m_iRouteIndex + i ]; + outCount++; + } + else if ( CheckLocalMove ( vecStart, m_Route[m_iRouteIndex+i+1].vecLocation, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + // Skip vert + continue; + } + else + { + Vector vecTest, vecSplit; + + // Halfway between this and next + vecTest = (m_Route[m_iRouteIndex+i+1].vecLocation + m_Route[m_iRouteIndex+i].vecLocation) * 0.5; + + // Halfway between this and previous + vecSplit = (m_Route[m_iRouteIndex+i].vecLocation + vecStart) * 0.5; + + int iType = (m_Route[m_iRouteIndex+i].iType | bits_MF_TO_DETOUR) & ~bits_MF_NOT_TO_MASK; + if ( CheckLocalMove ( vecStart, vecTest, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + outRoute[outCount].iType = iType; + outRoute[outCount].vecLocation = vecTest; + } + else if ( CheckLocalMove ( vecSplit, vecTest, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + outRoute[outCount].iType = iType; + outRoute[outCount].vecLocation = vecSplit; + outRoute[outCount+1].iType = iType; + outRoute[outCount+1].vecLocation = vecTest; + outCount++; // Adding an extra point + } + else + { + outRoute[outCount] = m_Route[ m_iRouteIndex + i ]; + } + } + // Get last point + vecStart = outRoute[ outCount ].vecLocation; + outCount++; + } + ASSERT( i < count ); + outRoute[outCount] = m_Route[ m_iRouteIndex + i ]; + outCount++; + + // Terminate + outRoute[outCount].iType = 0; + ASSERT( outCount < (ROUTE_SIZE*2) ); + +// Copy the simplified route, disable for testing + m_iRouteIndex = 0; + for ( i = 0; i < ROUTE_SIZE && i < outCount; i++ ) + { + m_Route[i] = outRoute[i]; + } + + // Terminate route + if ( i < ROUTE_SIZE ) + m_Route[i].iType = 0; + +// Debug, test movement code +#if 0 +// if ( CVAR_GET_FLOAT( "simplify" ) != 0 ) + DrawRoute( pev, outRoute, 0, 255, 0, 0 ); +// else + DrawRoute( pev, m_Route, m_iRouteIndex, 0, 255, 0 ); +#endif +} + +//========================================================= +// FBecomeProne - tries to send a monster into PRONE state. +// right now only used when a barnacle snatches someone, so +// may have some special case stuff for that. +//========================================================= +BOOL CBaseMonster :: FBecomeProne ( void ) +{ + if ( FBitSet ( pev->flags, FL_ONGROUND ) ) + { + pev->flags -= FL_ONGROUND; + } + + m_IdealMonsterState = MONSTERSTATE_PRONE; + return TRUE; +} + +//========================================================= +// CheckRangeAttack1 +//========================================================= +BOOL CBaseMonster :: CheckRangeAttack1 ( float flDot, float flDist ) +{ + if ( flDist > 64 && flDist <= 784 && flDot >= 0.5 ) + { + return TRUE; + } + return FALSE; +} + +//========================================================= +// CheckRangeAttack2 +//========================================================= +BOOL CBaseMonster :: CheckRangeAttack2 ( float flDot, float flDist ) +{ + if ( flDist > 64 && flDist <= 512 && flDot >= 0.5 ) + { + return TRUE; + } + return FALSE; +} + +//========================================================= +// CheckMeleeAttack1 +//========================================================= +BOOL CBaseMonster :: CheckMeleeAttack1 ( float flDot, float flDist ) +{ + // Decent fix to keep folks from kicking/punching hornets and snarks is to check the onground flag(sjb) + if ( flDist <= 64 && flDot >= 0.7 && m_hEnemy != NULL && FBitSet ( m_hEnemy->pev->flags, FL_ONGROUND ) ) + { + return TRUE; + } + return FALSE; +} + +//========================================================= +// CheckMeleeAttack2 +//========================================================= +BOOL CBaseMonster :: CheckMeleeAttack2 ( float flDot, float flDist ) +{ + if ( flDist <= 64 && flDot >= 0.7 ) + { + return TRUE; + } + return FALSE; +} + +//========================================================= +// CheckAttacks - sets all of the bits for attacks that the +// monster is capable of carrying out on the passed entity. +//========================================================= +void CBaseMonster :: CheckAttacks ( CBaseEntity *pTarget, float flDist ) +{ + Vector2D vec2LOS; + float flDot; + + UTIL_MakeVectors ( pev->angles ); + + vec2LOS = ( pTarget->pev->origin - pev->origin ).Make2D(); + vec2LOS = vec2LOS.Normalize(); + + flDot = DotProduct (vec2LOS , gpGlobals->v_forward.Make2D() ); + + // we know the enemy is in front now. We'll find which attacks the monster is capable of by + // checking for corresponding Activities in the model file, then do the simple checks to validate + // those attack types. + + // Clear all attack conditions + ClearConditions( bits_COND_CAN_RANGE_ATTACK1 | bits_COND_CAN_RANGE_ATTACK2 | bits_COND_CAN_MELEE_ATTACK1 |bits_COND_CAN_MELEE_ATTACK2 ); + + if ( m_afCapability & bits_CAP_RANGE_ATTACK1 ) + { + if ( CheckRangeAttack1 ( flDot, flDist ) ) + SetConditions( bits_COND_CAN_RANGE_ATTACK1 ); + } + if ( m_afCapability & bits_CAP_RANGE_ATTACK2 ) + { + if ( CheckRangeAttack2 ( flDot, flDist ) ) + SetConditions( bits_COND_CAN_RANGE_ATTACK2 ); + } + if ( m_afCapability & bits_CAP_MELEE_ATTACK1 ) + { + if ( CheckMeleeAttack1 ( flDot, flDist ) ) + SetConditions( bits_COND_CAN_MELEE_ATTACK1 ); + } + if ( m_afCapability & bits_CAP_MELEE_ATTACK2 ) + { + if ( CheckMeleeAttack2 ( flDot, flDist ) ) + SetConditions( bits_COND_CAN_MELEE_ATTACK2 ); + } +} + +//========================================================= +// CanCheckAttacks - prequalifies a monster to do more fine +// checking of potential attacks. +//========================================================= +BOOL CBaseMonster :: FCanCheckAttacks ( void ) +{ + if ( HasConditions(bits_COND_SEE_ENEMY) && !HasConditions( bits_COND_ENEMY_TOOFAR ) ) + { + return TRUE; + } + + return FALSE; +} + +//========================================================= +// CheckEnemy - part of the Condition collection process, +// gets and stores data and conditions pertaining to a monster's +// enemy. Returns TRUE if Enemy LKP was updated. +//========================================================= +int CBaseMonster :: CheckEnemy ( CBaseEntity *pEnemy ) +{ + float flDistToEnemy; + int iUpdatedLKP;// set this to TRUE if you update the EnemyLKP in this function. + + iUpdatedLKP = FALSE; + ClearConditions ( bits_COND_ENEMY_FACING_ME ); + + if ( !FVisible( pEnemy ) ) + { + ASSERT(!HasConditions(bits_COND_SEE_ENEMY)); + SetConditions( bits_COND_ENEMY_OCCLUDED ); + } + else + ClearConditions( bits_COND_ENEMY_OCCLUDED ); + + if ( !pEnemy->IsAlive() ) + { + SetConditions ( bits_COND_ENEMY_DEAD ); + ClearConditions( bits_COND_SEE_ENEMY | bits_COND_ENEMY_OCCLUDED ); + return FALSE; + } + + Vector vecEnemyPos = pEnemy->pev->origin; + // distance to enemy's origin + flDistToEnemy = ( vecEnemyPos - pev->origin ).Length(); + vecEnemyPos.z += pEnemy->pev->size.z * 0.5; + // distance to enemy's head + float flDistToEnemy2 = (vecEnemyPos - pev->origin).Length(); + if (flDistToEnemy2 < flDistToEnemy) + flDistToEnemy = flDistToEnemy2; + else + { + // distance to enemy's feet + vecEnemyPos.z -= pEnemy->pev->size.z; + float flDistToEnemy2 = (vecEnemyPos - pev->origin).Length(); + if (flDistToEnemy2 < flDistToEnemy) + flDistToEnemy = flDistToEnemy2; + } + + if ( HasConditions( bits_COND_SEE_ENEMY ) ) + { + CBaseMonster *pEnemyMonster; + + iUpdatedLKP = TRUE; + m_vecEnemyLKP = pEnemy->pev->origin; + + pEnemyMonster = pEnemy->MyMonsterPointer(); + + if ( pEnemyMonster ) + { + if ( pEnemyMonster->FInViewCone ( this ) ) + { + SetConditions ( bits_COND_ENEMY_FACING_ME ); + } + else + ClearConditions( bits_COND_ENEMY_FACING_ME ); + } + + if (pEnemy->pev->velocity != Vector( 0, 0, 0)) + { + // trail the enemy a bit + m_vecEnemyLKP = m_vecEnemyLKP - pEnemy->pev->velocity * RANDOM_FLOAT( -0.05, 0 ); + } + else + { + // UNDONE: use pev->oldorigin? + } + } + else if ( !HasConditions(bits_COND_ENEMY_OCCLUDED|bits_COND_SEE_ENEMY) && ( flDistToEnemy <= 256 ) ) + { + // if the enemy is not occluded, and unseen, that means it is behind or beside the monster. + // if the enemy is near enough the monster, we go ahead and let the monster know where the + // enemy is. + iUpdatedLKP = TRUE; + m_vecEnemyLKP = pEnemy->pev->origin; + } + + if ( flDistToEnemy >= m_flDistTooFar ) + { + // enemy is very far away from monster + SetConditions( bits_COND_ENEMY_TOOFAR ); + } + else + ClearConditions( bits_COND_ENEMY_TOOFAR ); + + if ( FCanCheckAttacks() ) + { + CheckAttacks ( m_hEnemy, flDistToEnemy ); + } + + if ( m_movementGoal == MOVEGOAL_ENEMY ) + { + for ( int i = m_iRouteIndex; i < ROUTE_SIZE; i++ ) + { + if ( m_Route[ i ].iType == (bits_MF_IS_GOAL|bits_MF_TO_ENEMY) ) + { + // UNDONE: Should we allow monsters to override this distance (80?) + if ( (m_Route[ i ].vecLocation - m_vecEnemyLKP).Length() > 80 ) + { + // Refresh + FRefreshRoute(); + return iUpdatedLKP; + } + } + } + } + + return iUpdatedLKP; +} + +//========================================================= +// PushEnemy - remember the last few enemies, always remember the player +//========================================================= +void CBaseMonster :: PushEnemy( CBaseEntity *pEnemy, Vector &vecLastKnownPos ) +{ + int i; + + if (pEnemy == NULL) + return; + + // UNDONE: blah, this is bad, we should use a stack but I'm too lazy to code one. + for (i = 0; i < MAX_OLD_ENEMIES; i++) + { + if (m_hOldEnemy[i] == pEnemy) + return; + if (m_hOldEnemy[i] == NULL) // someone died, reuse their slot + break; + } + if (i >= MAX_OLD_ENEMIES) + return; + + m_hOldEnemy[i] = pEnemy; + m_vecOldEnemy[i] = vecLastKnownPos; +} + +//========================================================= +// PopEnemy - try remembering the last few enemies +//========================================================= +BOOL CBaseMonster :: PopEnemy( ) +{ + // UNDONE: blah, this is bad, we should use a stack but I'm too lazy to code one. + for (int i = MAX_OLD_ENEMIES - 1; i >= 0; i--) + { + if (m_hOldEnemy[i] != NULL) + { + if (m_hOldEnemy[i]->IsAlive( )) // cheat and know when they die + { + m_hEnemy = m_hOldEnemy[i]; + m_vecEnemyLKP = m_vecOldEnemy[i]; + // ALERT( at_console, "remembering\n"); + return TRUE; + } + else + { + m_hOldEnemy[i] = NULL; + } + } + } + return FALSE; +} + +//========================================================= +// SetActivity +//========================================================= +void CBaseMonster :: SetActivity ( Activity NewActivity ) +{ + int iSequence; + + iSequence = LookupActivity ( NewActivity ); + + // Set to the desired anim, or default anim if the desired is not present + if ( iSequence > ACTIVITY_NOT_AVAILABLE ) + { + if ( pev->sequence != iSequence || !m_fSequenceLoops ) + { + // don't reset frame between walk and run + if ( !(m_Activity == ACT_WALK || m_Activity == ACT_RUN) || !(NewActivity == ACT_WALK || NewActivity == ACT_RUN)) + pev->frame = 0; + } + + pev->sequence = iSequence; // Set to the reset anim (if it's there) + ResetSequenceInfo( ); + SetYawSpeed(); + } + else + { + // Not available try to get default anim + ALERT ( at_aiconsole, "%s has no sequence for act:%d\n", STRING(pev->classname), NewActivity ); + pev->sequence = 0; // Set to the reset anim (if it's there) + } + + m_Activity = NewActivity; // Go ahead and set this so it doesn't keep trying when the anim is not present + + // In case someone calls this with something other than the ideal activity + m_IdealActivity = m_Activity; + + +} + +//========================================================= +// SetSequenceByName +//========================================================= +void CBaseMonster :: SetSequenceByName ( char *szSequence ) +{ + int iSequence; + + iSequence = LookupSequence ( szSequence ); + + // Set to the desired anim, or default anim if the desired is not present + if ( iSequence > ACTIVITY_NOT_AVAILABLE ) + { + if ( pev->sequence != iSequence || !m_fSequenceLoops ) + { + pev->frame = 0; + } + + pev->sequence = iSequence; // Set to the reset anim (if it's there) + ResetSequenceInfo( ); + SetYawSpeed(); + } + else + { + // Not available try to get default anim + ALERT ( at_aiconsole, "%s has no sequence named:%f\n", STRING(pev->classname), szSequence ); + pev->sequence = 0; // Set to the reset anim (if it's there) + } +} + +//========================================================= +// CheckLocalMove - returns TRUE if the caller can walk a +// straight line from its current origin to the given +// location. If so, don't use the node graph! +// +// if a valid pointer to a int is passed, the function +// will fill that int with the distance that the check +// reached before hitting something. THIS ONLY HAPPENS +// IF THE LOCAL MOVE CHECK FAILS! +// +// !!!PERFORMANCE - should we try to load balance this? +// DON"T USE SETORIGIN! +//========================================================= +#define LOCAL_STEP_SIZE 16 +int CBaseMonster :: CheckLocalMove ( const Vector &vecStart, const Vector &vecEnd, CBaseEntity *pTarget, float *pflDist ) +{ + Vector vecStartPos;// record monster's position before trying the move + float flYaw; + float flDist; + float flStep, stepSize; + int iReturn; + + vecStartPos = pev->origin; + + + flYaw = UTIL_VecToYaw ( vecEnd - vecStart );// build a yaw that points to the goal. + flDist = ( vecEnd - vecStart ).Length2D();// get the distance. + iReturn = LOCALMOVE_VALID;// assume everything will be ok. + + // move the monster to the start of the local move that's to be checked. + UTIL_SetOrigin( this, vecStart );// !!!BUGBUG - won't this fire triggers? - nope, SetOrigin doesn't fire + + if ( !(pev->flags & (FL_FLY|FL_SWIM)) ) + { + DROP_TO_FLOOR( ENT( pev ) );//make sure monster is on the floor! + } + + //pev->origin.z = vecStartPos.z;//!!!HACKHACK + +// pev->origin = vecStart; + +/* + if ( flDist > 1024 ) + { + // !!!PERFORMANCE - this operation may be too CPU intensive to try checks this large. + // We don't lose much here, because a distance this great is very likely + // to have something in the way. + + // since we've actually moved the monster during the check, undo the move. + pev->origin = vecStartPos; + return FALSE; + } +*/ + // this loop takes single steps to the goal. + for ( flStep = 0 ; flStep < flDist ; flStep += LOCAL_STEP_SIZE ) + { + stepSize = LOCAL_STEP_SIZE; + + if ( (flStep + LOCAL_STEP_SIZE) >= (flDist-1) ) + stepSize = (flDist - flStep) - 1; + +// UTIL_ParticleEffect ( pev->origin, g_vecZero, 255, 25 ); + + if ( !WALK_MOVE( ENT(pev), flYaw, stepSize, WALKMOVE_CHECKONLY ) ) + {// can't take the next step, fail! + + if ( pflDist != NULL ) + { + *pflDist = flStep; + } + if ( pTarget && pTarget->edict() == gpGlobals->trace_ent ) + { + // if this step hits target ent, the move is legal. + iReturn = LOCALMOVE_VALID; + break; + } + else + { + // If we're going toward an entity, and we're almost getting there, it's OK. +// if ( pTarget && fabs( flDist - iStep ) < LOCAL_STEP_SIZE ) +// fReturn = TRUE; +// else + iReturn = LOCALMOVE_INVALID; + break; + } + + } + } + + if ( iReturn == LOCALMOVE_VALID && !(pev->flags & (FL_FLY|FL_SWIM) ) && (!pTarget || (pTarget->pev->flags & FL_ONGROUND)) ) + { + // The monster can move to a spot UNDER the target, but not to it. Don't try to triangulate, go directly to the node graph. + // UNDONE: Magic # 64 -- this used to be pev->size.z but that won't work for small creatures like the headcrab + if ( fabs(vecEnd.z - pev->origin.z) > 64 ) + { + iReturn = LOCALMOVE_INVALID_DONT_TRIANGULATE; + } + } + /* + // uncommenting this block will draw a line representing the nearest legal move. + WRITE_BYTE(MSG_BROADCAST, SVC_TEMPENTITY); + WRITE_BYTE(MSG_BROADCAST, TE_SHOWLINE); + WRITE_COORD(MSG_BROADCAST, pev->origin.x); + WRITE_COORD(MSG_BROADCAST, pev->origin.y); + WRITE_COORD(MSG_BROADCAST, pev->origin.z); + WRITE_COORD(MSG_BROADCAST, vecStart.x); + WRITE_COORD(MSG_BROADCAST, vecStart.y); + WRITE_COORD(MSG_BROADCAST, vecStart.z); + */ + + // since we've actually moved the monster during the check, undo the move. + UTIL_SetOrigin( this, vecStartPos ); + + return iReturn; +} + + +float CBaseMonster :: OpenDoorAndWait( entvars_t *pevDoor ) +{ + float flTravelTime = 0; + + //ALERT(at_aiconsole, "A door. "); + CBaseEntity *pcbeDoor = CBaseEntity::Instance(pevDoor); + if (pcbeDoor && !pcbeDoor->IsLockedByMaster()) + { + //ALERT(at_aiconsole, "unlocked! "); + pcbeDoor->Use(this, this, USE_ON, 0.0); + //ALERT(at_aiconsole, "pevDoor->nextthink = %d ms\n", (int)(1000*pevDoor->nextthink)); + //ALERT(at_aiconsole, "pevDoor->ltime = %d ms\n", (int)(1000*pevDoor->ltime)); + //ALERT(at_aiconsole, "pev-> nextthink = %d ms\n", (int)(1000*pev->nextthink)); + //ALERT(at_aiconsole, "pev->ltime = %d ms\n", (int)(1000*pev->ltime)); + + flTravelTime = pcbeDoor->m_fNextThink - pevDoor->ltime; + + //ALERT(at_aiconsole, "Waiting %d ms\n", (int)(1000*flTravelTime)); + if ( pcbeDoor->pev->targetname ) + { + CBaseEntity *pTarget = NULL; + for (;;) + { + pTarget = UTIL_FindEntityByTargetname( pTarget, STRING(pcbeDoor->pev->targetname)); + if (!pTarget) + break; + + if ( VARS( pTarget->pev ) != pcbeDoor->pev && + FClassnameIs ( pTarget->pev, STRING(pcbeDoor->pev->classname) ) ) + { + pTarget->Use(this, this, USE_ON, 0.0); + } + } + } + } + + return gpGlobals->time + flTravelTime; +} + + +//========================================================= +// AdvanceRoute - poorly named function that advances the +// m_iRouteIndex. If it goes beyond ROUTE_SIZE, the route +// is refreshed. +//========================================================= +void CBaseMonster :: AdvanceRoute ( float distance ) +{ + + if ( m_iRouteIndex == ROUTE_SIZE - 1 ) + { + // time to refresh the route. + if ( !FRefreshRoute() ) + { + ALERT ( at_aiconsole, "Can't Refresh Route!!\n" ); + } + } + else + { + if ( ! (m_Route[ m_iRouteIndex ].iType & bits_MF_IS_GOAL) ) + { + // If we've just passed a path_corner, advance m_pGoalEnt + if ( (m_Route[ m_iRouteIndex ].iType & ~bits_MF_NOT_TO_MASK) == bits_MF_TO_PATHCORNER ) + m_pGoalEnt = m_pGoalEnt->GetNext(); + + // IF both waypoints are nodes, then check for a link for a door and operate it. + // + if ( (m_Route[m_iRouteIndex].iType & bits_MF_TO_NODE) == bits_MF_TO_NODE + && (m_Route[m_iRouteIndex+1].iType & bits_MF_TO_NODE) == bits_MF_TO_NODE) + { + //ALERT(at_aiconsole, "SVD: Two nodes. "); + + int iSrcNode = WorldGraph.FindNearestNode(m_Route[m_iRouteIndex].vecLocation, this ); + int iDestNode = WorldGraph.FindNearestNode(m_Route[m_iRouteIndex+1].vecLocation, this ); + + int iLink; + WorldGraph.HashSearch(iSrcNode, iDestNode, iLink); + + if ( iLink >= 0 && WorldGraph.m_pLinkPool[iLink].m_pLinkEnt != NULL ) + { + //ALERT(at_aiconsole, "A link. "); + if ( WorldGraph.HandleLinkEnt ( iSrcNode, WorldGraph.m_pLinkPool[iLink].m_pLinkEnt, m_afCapability, CGraph::NODEGRAPH_DYNAMIC ) ) + { + //ALERT(at_aiconsole, "usable."); + entvars_t *pevDoor = WorldGraph.m_pLinkPool[iLink].m_pLinkEnt; + if (pevDoor) + { + m_flMoveWaitFinished = OpenDoorAndWait( pevDoor ); +// ALERT( at_aiconsole, "Wating for door %.2f\n", m_flMoveWaitFinished-gpGlobals->time ); + } + } + } + //ALERT(at_aiconsole, "\n"); + } + m_iRouteIndex++; + } + else // At goal!!! + { + if ( distance < m_flGroundSpeed * 0.2 /* FIX */ ) + { + MovementComplete(); + } + } + } +} + + +int CBaseMonster :: RouteClassify( int iMoveFlag ) +{ + int movementGoal; + + movementGoal = MOVEGOAL_NONE; + + if ( iMoveFlag & bits_MF_TO_TARGETENT ) + movementGoal = MOVEGOAL_TARGETENT; + else if ( iMoveFlag & bits_MF_TO_ENEMY ) + movementGoal = MOVEGOAL_ENEMY; + else if ( iMoveFlag & bits_MF_TO_PATHCORNER ) + movementGoal = MOVEGOAL_PATHCORNER; + else if ( iMoveFlag & bits_MF_TO_NODE ) + movementGoal = MOVEGOAL_NODE; + else if ( iMoveFlag & bits_MF_TO_LOCATION ) + movementGoal = MOVEGOAL_LOCATION; + + return movementGoal; +} + +//========================================================= +// BuildRoute +//========================================================= +BOOL CBaseMonster :: BuildRoute ( const Vector &vecGoal, int iMoveFlag, CBaseEntity *pTarget ) +{ + float flDist; + Vector vecApex; + int iLocalMove; + + RouteNew(); + m_movementGoal = RouteClassify( iMoveFlag ); + +// so we don't end up with no moveflags + m_Route[ 0 ].vecLocation = vecGoal; + m_Route[ 0 ].iType = iMoveFlag | bits_MF_IS_GOAL; + +// check simple local move + iLocalMove = CheckLocalMove( pev->origin, vecGoal, pTarget, &flDist ); + + if ( iLocalMove == LOCALMOVE_VALID ) + { + // monster can walk straight there! + return TRUE; + } +// try to triangulate around any obstacles. + else if ( iLocalMove != LOCALMOVE_INVALID_DONT_TRIANGULATE && FTriangulate( pev->origin, vecGoal, flDist, pTarget, &vecApex ) ) + { + // there is a slightly more complicated path that allows the monster to reach vecGoal + m_Route[ 0 ].vecLocation = vecApex; + m_Route[ 0 ].iType = (iMoveFlag | bits_MF_TO_DETOUR); + + m_Route[ 1 ].vecLocation = vecGoal; + m_Route[ 1 ].iType = iMoveFlag | bits_MF_IS_GOAL; + + /* + WRITE_BYTE(MSG_BROADCAST, SVC_TEMPENTITY); + WRITE_BYTE(MSG_BROADCAST, TE_SHOWLINE); + WRITE_COORD(MSG_BROADCAST, vecApex.x ); + WRITE_COORD(MSG_BROADCAST, vecApex.y ); + WRITE_COORD(MSG_BROADCAST, vecApex.z ); + WRITE_COORD(MSG_BROADCAST, vecApex.x ); + WRITE_COORD(MSG_BROADCAST, vecApex.y ); + WRITE_COORD(MSG_BROADCAST, vecApex.z + 128 ); + */ + + RouteSimplify( pTarget ); + return TRUE; + } + +// last ditch, try nodes + if ( FGetNodeRoute( vecGoal ) ) + { +// ALERT ( at_console, "Can get there on nodes\n" ); + m_vecMoveGoal = vecGoal; + RouteSimplify( pTarget ); + return TRUE; + } + + // b0rk + return FALSE; +} + + +//========================================================= +// InsertWaypoint - Rebuilds the existing route so that the +// supplied vector and moveflags are the first waypoint in +// the route, and fills the rest of the route with as much +// of the pre-existing route as possible +//========================================================= +void CBaseMonster :: InsertWaypoint ( Vector vecLocation, int afMoveFlags ) +{ + int i, type; + + + // we have to save some Index and Type information from the real + // path_corner or node waypoint that the monster was trying to reach. This makes sure that data necessary + // to refresh the original path exists even in the new waypoints that don't correspond directy to a path_corner + // or node. + type = afMoveFlags | (m_Route[ m_iRouteIndex ].iType & ~bits_MF_NOT_TO_MASK); + + for ( i = ROUTE_SIZE-1; i > 0; i-- ) + m_Route[i] = m_Route[i-1]; + + m_Route[ m_iRouteIndex ].vecLocation = vecLocation; + m_Route[ m_iRouteIndex ].iType = type; +} + +//========================================================= +// FTriangulate - tries to overcome local obstacles by +// triangulating a path around them. +// +// iApexDist is how far the obstruction that we are trying +// to triangulate around is from the monster. +//========================================================= +BOOL CBaseMonster :: FTriangulate ( const Vector &vecStart , const Vector &vecEnd, float flDist, CBaseEntity *pTargetEnt, Vector *pApex ) +{ + Vector vecDir; + Vector vecForward; + Vector vecLeft;// the spot we'll try to triangulate to on the left + Vector vecRight;// the spot we'll try to triangulate to on the right + Vector vecTop;// the spot we'll try to triangulate to on the top + Vector vecBottom;// the spot we'll try to triangulate to on the bottom + Vector vecFarSide;// the spot that we'll move to after hitting the triangulated point, before moving on to our normal goal. + int i; + float sizeX, sizeZ; + + // If the hull width is less than 24, use 24 because CheckLocalMove uses a min of + // 24. + sizeX = pev->size.x; + if (sizeX < 24.0) + sizeX = 24.0; + else if (sizeX > 48.0) + sizeX = 48.0; + sizeZ = pev->size.z; + //if (sizeZ < 24.0) + // sizeZ = 24.0; + + vecForward = ( vecEnd - vecStart ).Normalize(); + + Vector vecDirUp(0,0,1); + vecDir = CrossProduct ( vecForward, vecDirUp); + + // start checking right about where the object is, picking two equidistant starting points, one on + // the left, one on the right. As we progress through the loop, we'll push these away from the obstacle, + // hoping to find a way around on either side. pev->size.x is added to the ApexDist in order to help select + // an apex point that insures that the monster is sufficiently past the obstacle before trying to turn back + // onto its original course. + + vecLeft = pev->origin + ( vecForward * ( flDist + sizeX ) ) - vecDir * ( sizeX * 3 ); + vecRight = pev->origin + ( vecForward * ( flDist + sizeX ) ) + vecDir * ( sizeX * 3 ); + if (pev->movetype == MOVETYPE_FLY) + { + vecTop = pev->origin + (vecForward * flDist) + (vecDirUp * sizeZ * 3); + vecBottom = pev->origin + (vecForward * flDist) - (vecDirUp * sizeZ * 3); + } + + vecFarSide = m_Route[ m_iRouteIndex ].vecLocation; + + vecDir = vecDir * sizeX * 2; + if (pev->movetype == MOVETYPE_FLY) + vecDirUp = vecDirUp * sizeZ * 2; + + for ( i = 0 ; i < 8; i++ ) + { +// Debug, Draw the triangulation +#if 0 + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE); + WRITE_COORD( pev->origin.x ); + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z ); + WRITE_COORD( vecRight.x ); + WRITE_COORD( vecRight.y ); + WRITE_COORD( vecRight.z ); + MESSAGE_END(); + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE ); + WRITE_COORD( pev->origin.x ); + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z ); + WRITE_COORD( vecLeft.x ); + WRITE_COORD( vecLeft.y ); + WRITE_COORD( vecLeft.z ); + MESSAGE_END(); +#endif + +#if 0 + if (pev->movetype == MOVETYPE_FLY) + { + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE ); + WRITE_COORD( pev->origin.x ); + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z ); + WRITE_COORD( vecTop.x ); + WRITE_COORD( vecTop.y ); + WRITE_COORD( vecTop.z ); + MESSAGE_END(); + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE ); + WRITE_COORD( pev->origin.x ); + WRITE_COORD( pev->origin.y ); + WRITE_COORD( pev->origin.z ); + WRITE_COORD( vecBottom.x ); + WRITE_COORD( vecBottom.y ); + WRITE_COORD( vecBottom.z ); + MESSAGE_END(); + } +#endif + + if ( CheckLocalMove( pev->origin, vecRight, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + if ( CheckLocalMove ( vecRight, vecFarSide, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + if ( pApex ) + { + *pApex = vecRight; + } + + return TRUE; + } + } + if ( CheckLocalMove( pev->origin, vecLeft, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + if ( CheckLocalMove ( vecLeft, vecFarSide, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + if ( pApex ) + { + *pApex = vecLeft; + } + + return TRUE; + } + } + + if (pev->movetype == MOVETYPE_FLY) + { + if ( CheckLocalMove( pev->origin, vecTop, pTargetEnt, NULL ) == LOCALMOVE_VALID) + { + if ( CheckLocalMove ( vecTop, vecFarSide, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + if ( pApex ) + { + *pApex = vecTop; + //ALERT(at_aiconsole, "triangulate over\n"); + } + + return TRUE; + } + } +#if 1 + if ( CheckLocalMove( pev->origin, vecBottom, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + if ( CheckLocalMove ( vecBottom, vecFarSide, pTargetEnt, NULL ) == LOCALMOVE_VALID ) + { + if ( pApex ) + { + *pApex = vecBottom; + //ALERT(at_aiconsole, "triangulate under\n"); + } + + return TRUE; + } + } +#endif + } + + vecRight = vecRight + vecDir; + vecLeft = vecLeft - vecDir; + if (pev->movetype == MOVETYPE_FLY) + { + vecTop = vecTop + vecDirUp; + vecBottom = vecBottom - vecDirUp; + } + } + + return FALSE; +} + +//========================================================= +// Move - take a single step towards the next ROUTE location +//========================================================= +#define DIST_TO_CHECK 200 + +void CBaseMonster :: Move ( float flInterval ) +{ + float flWaypointDist; + float flCheckDist; + float flDist;// how far the lookahead check got before hitting an object. + Vector vecDir; + Vector vecApex; + CBaseEntity *pTargetEnt; + + // Don't move if no valid route + if ( FRouteClear() ) + { + // If we still have a movement goal, then this is probably a route truncated by SimplifyRoute() + // so refresh it. + if ( m_movementGoal == MOVEGOAL_NONE || !FRefreshRoute() ) + { + ALERT( at_aiconsole, "Tried to move with no route!\n" ); + TaskFail(); + return; + } + } + + if ( m_flMoveWaitFinished > gpGlobals->time ) + return; + +// Debug, test movement code +#if 0 +// if ( CVAR_GET_FLOAT("stopmove" ) != 0 ) + { + if ( m_movementGoal == MOVEGOAL_ENEMY ) + RouteSimplify( m_hEnemy ); + else + RouteSimplify( m_hTargetEnt ); + FRefreshRoute(); + return; + } +#else +// Debug, draw the route +// DrawRoute( pev, m_Route, m_iRouteIndex, 0, 200, 0 ); +#endif + + // if the monster is moving directly towards an entity (enemy for instance), we'll set this pointer + // to that entity for the CheckLocalMove and Triangulate functions. + pTargetEnt = NULL; + + // local move to waypoint. + vecDir = ( m_Route[ m_iRouteIndex ].vecLocation - pev->origin ).Normalize(); + flWaypointDist = ( m_Route[ m_iRouteIndex ].vecLocation - pev->origin ).Length2D(); + + MakeIdealYaw ( m_Route[ m_iRouteIndex ].vecLocation ); + ChangeYaw ( pev->yaw_speed ); + + // if the waypoint is closer than CheckDist, CheckDist is the dist to waypoint + if ( flWaypointDist < DIST_TO_CHECK ) + { + flCheckDist = flWaypointDist; + } + else + { + flCheckDist = DIST_TO_CHECK; + } + + if ( (m_Route[ m_iRouteIndex ].iType & (~bits_MF_NOT_TO_MASK)) == bits_MF_TO_ENEMY ) + { + // only on a PURE move to enemy ( i.e., ONLY MF_TO_ENEMY set, not MF_TO_ENEMY and DETOUR ) + pTargetEnt = m_hEnemy; + } + else if ( (m_Route[ m_iRouteIndex ].iType & ~bits_MF_NOT_TO_MASK) == bits_MF_TO_TARGETENT ) + { + pTargetEnt = m_hTargetEnt; + } + + // !!!BUGBUG - CheckDist should be derived from ground speed. + // If this fails, it should be because of some dynamic entity blocking this guy. + // We've already checked this path, so we should wait and time out if the entity doesn't move + flDist = 0; + if ( CheckLocalMove ( pev->origin, pev->origin + vecDir * flCheckDist, pTargetEnt, &flDist ) != LOCALMOVE_VALID ) + { + CBaseEntity *pBlocker; + + // Can't move, stop + Stop(); + // Blocking entity is in global trace_ent + pBlocker = CBaseEntity::Instance( gpGlobals->trace_ent ); + if (pBlocker) + { + DispatchBlocked( edict(), pBlocker->edict() ); + } + + if ( pBlocker && m_moveWaitTime > 0 && pBlocker->IsMoving() && !pBlocker->IsPlayer() && (gpGlobals->time-m_flMoveWaitFinished) > 3.0 ) + { + // Can we still move toward our target? + if ( flDist < m_flGroundSpeed ) + { + // No, Wait for a second + m_flMoveWaitFinished = gpGlobals->time + m_moveWaitTime; + return; + } + // Ok, still enough room to take a step + } + else + { + // try to triangulate around whatever is in the way. + if ( FTriangulate( pev->origin, m_Route[ m_iRouteIndex ].vecLocation, flDist, pTargetEnt, &vecApex ) ) + { + InsertWaypoint( vecApex, bits_MF_TO_DETOUR ); + RouteSimplify( pTargetEnt ); + } + else + { +// ALERT ( at_aiconsole, "Couldn't Triangulate\n" ); + Stop(); + // Only do this once until your route is cleared + if ( m_moveWaitTime > 0 && !(m_afMemory & bits_MEMORY_MOVE_FAILED) ) + { + FRefreshRoute(); + if ( FRouteClear() ) + { + TaskFail(); + } + else + { + // Don't get stuck + if ( (gpGlobals->time - m_flMoveWaitFinished) < 0.2 ) + Remember( bits_MEMORY_MOVE_FAILED ); + + m_flMoveWaitFinished = gpGlobals->time + 0.1; + } + } + else + { + TaskFail(); + ALERT( at_aiconsole, "%s Failed to move (%d)!\n", STRING(pev->classname), HasMemory( bits_MEMORY_MOVE_FAILED ) ); + //ALERT( at_aiconsole, "%f, %f, %f\n", pev->origin.z, (pev->origin + (vecDir * flCheckDist)).z, m_Route[m_iRouteIndex].vecLocation.z ); + } + return; + } + } + } + + // close enough to the target, now advance to the next target. This is done before actually reaching + // the target so that we get a nice natural turn while moving. + if ( ShouldAdvanceRoute( flWaypointDist ) )///!!!BUGBUG- magic number + { + AdvanceRoute( flWaypointDist ); + } + + // Might be waiting for a door + if ( m_flMoveWaitFinished > gpGlobals->time ) + { + Stop(); + return; + } + + // UNDONE: this is a hack to quit moving farther than it has looked ahead. + if (flCheckDist < m_flGroundSpeed * flInterval) + { + flInterval = flCheckDist / m_flGroundSpeed; + // ALERT( at_console, "%.02f\n", flInterval ); + } + MoveExecute( pTargetEnt, vecDir, flInterval ); + + if ( MovementIsComplete() ) + { + Stop(); + RouteClear(); + } +} + + +BOOL CBaseMonster:: ShouldAdvanceRoute( float flWaypointDist ) +{ + if ( flWaypointDist <= MONSTER_CUT_CORNER_DIST ) + { + // ALERT( at_console, "cut %f\n", flWaypointDist ); + return TRUE; + } + + return FALSE; +} + + +void CBaseMonster::MoveExecute( CBaseEntity *pTargetEnt, const Vector &vecDir, float flInterval ) +{ +// float flYaw = UTIL_VecToYaw ( m_Route[ m_iRouteIndex ].vecLocation - pev->origin );// build a yaw that points to the goal. +// WALK_MOVE( ENT(pev), flYaw, m_flGroundSpeed * flInterval, WALKMOVE_NORMAL ); + if ( m_IdealActivity != m_movementActivity ) + m_IdealActivity = m_movementActivity; + + float flTotal = m_flGroundSpeed * pev->framerate * flInterval; + float flStep; + while (flTotal > 0.001) + { + // don't walk more than 16 units or stairs stop working + flStep = min( 16.0, flTotal ); + UTIL_MoveToOrigin ( ENT(pev), m_Route[ m_iRouteIndex ].vecLocation, flStep, MOVE_NORMAL ); + flTotal -= flStep; + } + // ALERT( at_console, "dist %f\n", m_flGroundSpeed * pev->framerate * flInterval ); +} + + +//========================================================= +// MonsterInit - after a monster is spawned, it needs to +// be dropped into the world, checked for mobility problems, +// and put on the proper path, if any. This function does +// all of those things after the monster spawns. Any +// initialization that should take place for all monsters +// goes here. +//========================================================= +void CBaseMonster :: MonsterInit ( void ) +{ + if (!g_pGameRules->FAllowMonsters()) + { + pev->flags |= FL_KILLME; // Post this because some monster code modifies class data after calling this function + return; + } + + // Set fields common to all monsters + pev->effects = 0; + pev->takedamage = DAMAGE_AIM; + pev->ideal_yaw = pev->angles.y; + pev->max_health = pev->health; + pev->deadflag = DEAD_NO; + m_IdealMonsterState = MONSTERSTATE_IDLE;// Assume monster will be idle, until proven otherwise + + m_IdealActivity = ACT_IDLE; + + SetBits (pev->flags, FL_MONSTER); + + ClearSchedule(); + RouteClear(); + InitBoneControllers( ); // FIX: should be done in Spawn + + m_iHintNode = NO_NODE; + + m_afMemory = MEMORY_CLEAR; + + m_hEnemy = NULL; + + m_flDistTooFar = 1024.0; + m_flDistLook = 2048.0; + + // set eye position + SetEyePosition(); + + SetThink(&CBaseMonster :: MonsterInitThink ); + SetNextThink( 0.1 ); + SetUse(&CBaseMonster :: MonsterUse ); +} + +//========================================================= +// MonsterInitThink - Calls StartMonster. Startmonster is +// virtual, but this function cannot be +//========================================================= +void CBaseMonster :: MonsterInitThink ( void ) +{ + StartMonster(); +} + + +void CBaseMonster :: StartPatrol ( CBaseEntity* path ) +{ + m_pGoalEnt = path; + + if ( !m_pGoalEnt ) + { + ALERT(at_error, "ReadyMonster()--%s couldn't find target \"%s\"\n", STRING(pev->classname), STRING(pev->target)); + } + else + { + // Monster will start turning towards his destination +// MakeIdealYaw ( m_pGoalEnt->pev->origin ); + + // set the monster up to walk a path corner path. + // !!!BUGBUG - this is a minor bit of a hack. + // JAYJAY + m_movementGoal = MOVEGOAL_PATHCORNER; + + if ( pev->movetype == MOVETYPE_FLY ) + m_movementActivity = ACT_FLY; + else + m_movementActivity = ACT_WALK; + + if ( !FRefreshRoute() ) + { + ALERT ( at_aiconsole, "Can't Create Route!\n" ); + } + SetState( MONSTERSTATE_IDLE ); + ChangeSchedule( GetScheduleOfType( SCHED_IDLE_WALK ) ); + } +} + +//========================================================= +// StartMonster - final bit of initization before a monster +// is turned over to the AI. +//========================================================= +void CBaseMonster :: StartMonster ( void ) +{ + // update capabilities + if ( LookupActivity ( ACT_RANGE_ATTACK1 ) != ACTIVITY_NOT_AVAILABLE ) + { + m_afCapability |= bits_CAP_RANGE_ATTACK1; + } + if ( LookupActivity ( ACT_RANGE_ATTACK2 ) != ACTIVITY_NOT_AVAILABLE ) + { + m_afCapability |= bits_CAP_RANGE_ATTACK2; + } + if ( LookupActivity ( ACT_MELEE_ATTACK1 ) != ACTIVITY_NOT_AVAILABLE ) + { + m_afCapability |= bits_CAP_MELEE_ATTACK1; + } + if ( LookupActivity ( ACT_MELEE_ATTACK2 ) != ACTIVITY_NOT_AVAILABLE ) + { + m_afCapability |= bits_CAP_MELEE_ATTACK2; + } + + // Raise monster off the floor one unit, then drop to floor + if ( pev->movetype != MOVETYPE_FLY && !FBitSet( pev->spawnflags, SF_MONSTER_FALL_TO_GROUND ) ) + { + pev->origin.z += 1; + DROP_TO_FLOOR ( ENT(pev) ); + // Try to move the monster to make sure it's not stuck in a brush. + //LRC- there are perfectly good reasons for making a monster stuck, so it shouldn't always be an error. + if (!WALK_MOVE ( ENT(pev), 0, 0, WALKMOVE_NORMAL ) && !FBitSet( pev->spawnflags, SF_MONSTER_NO_YELLOW_BLOBS)) + { + Msg("%s \"%s\" stuck in wall--level design error\n", STRING(pev->classname), STRING(pev->targetname)); + pev->effects = EF_BRIGHTFIELD; + } + } + else + { + pev->flags &= ~FL_ONGROUND; + } + + if ( !FStringNull(pev->target) )// this monster has a target + { + StartPatrol(UTIL_FindEntityByTargetname( NULL, STRING( pev->target ))); + } + + //SetState ( m_IdealMonsterState ); + //SetActivity ( m_IdealActivity ); + + // Delay drop to floor to make sure each door in the level has had its chance to spawn + // Spread think times so that they don't all happen at the same time (Carmack) + SetThink(&CBaseMonster :: CallMonsterThink ); + AbsoluteNextThink( m_fNextThink + RANDOM_FLOAT(0.1, 0.4) ); // spread think times. + + if ( !FStringNull(pev->targetname) )// wait until triggered + { + SetState( MONSTERSTATE_IDLE ); + // UNDONE: Some scripted sequence monsters don't have an idle? + SetActivity( ACT_IDLE ); + ChangeSchedule( GetScheduleOfType( SCHED_WAIT_TRIGGER ) ); + } +} + +void CBaseMonster :: MovementComplete( void ) +{ + switch( m_iTaskStatus ) + { + case TASKSTATUS_NEW: + case TASKSTATUS_RUNNING: + m_iTaskStatus = TASKSTATUS_RUNNING_TASK; + break; + + case TASKSTATUS_RUNNING_MOVEMENT: + TaskComplete(); + break; + + case TASKSTATUS_RUNNING_TASK: + ALERT( at_error, "Movement completed twice!\n" ); + break; + + case TASKSTATUS_COMPLETE: + break; + } + m_movementGoal = MOVEGOAL_NONE; +} + + +int CBaseMonster::TaskIsRunning( void ) +{ + if ( m_iTaskStatus != TASKSTATUS_COMPLETE && + m_iTaskStatus != TASKSTATUS_RUNNING_MOVEMENT ) + return 1; + + return 0; +} + +//========================================================= +// IRelationship - returns an integer that describes the +// relationship between two types of monster. +//========================================================= +int CBaseMonster::IRelationship ( CBaseEntity *pTarget ) +{ + static int iEnemy[17][17] = + { //NONE MACH PLYR HPASS HMIL AMIL APASS AMONST APREY APRED INSECT PLRALY PBWPN ABWPN FACT_A FACT_B FACT_C + /*NONE*/ { R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO }, + /*MACHINE*/ { R_NO, R_NO, R_DL, R_DL, R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL }, + /*PLAYER*/ { R_NO, R_DL, R_NO, R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_NO, R_NO, R_DL, R_DL, R_DL, R_DL, R_DL }, + /*HUMANPASSIVE*/ { R_NO, R_NO, R_AL, R_AL, R_HT, R_FR, R_NO, R_HT, R_DL, R_FR, R_NO, R_AL, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*HUMANMILITAR*/ { R_NO, R_NO, R_HT, R_DL, R_NO, R_HT, R_DL, R_DL, R_DL, R_DL, R_NO, R_HT, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*ALIENMILITAR*/ { R_NO, R_DL, R_HT, R_DL, R_HT, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_DL, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*ALIENPASSIVE*/ { R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*ALIENMONSTER*/ { R_NO, R_DL, R_DL, R_DL, R_DL, R_NO, R_NO, R_NO, R_NO, R_NO, R_NO, R_DL, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*ALIENPREY */ { R_NO, R_NO, R_DL, R_DL, R_DL, R_NO, R_NO, R_NO, R_NO, R_FR, R_NO, R_DL, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*ALIENPREDATO*/ { R_NO, R_NO, R_DL, R_DL, R_DL, R_NO, R_NO, R_NO, R_HT, R_DL, R_NO, R_DL, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*INSECT*/ { R_FR, R_FR, R_FR, R_FR, R_FR, R_NO, R_FR, R_FR, R_FR, R_FR, R_NO, R_FR, R_NO, R_NO, R_FR, R_FR, R_FR }, + /*PLAYERALLY*/ { R_NO, R_DL, R_AL, R_AL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_NO, R_NO, R_NO, R_NO, R_DL, R_DL, R_DL }, + /*PBIOWEAPON*/ { R_NO, R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_NO, R_DL, R_NO, R_DL, R_DL, R_DL, R_DL }, + /*ABIOWEAPON*/ { R_NO, R_NO, R_DL, R_DL, R_DL, R_AL, R_NO, R_DL, R_DL, R_NO, R_NO, R_DL, R_DL, R_NO, R_DL, R_DL, R_DL }, + /*FACTION_A*/ { R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_NO, R_DL, R_DL, R_DL, R_AL, R_DL, R_DL }, + /*FACTION_B*/ { R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_NO, R_DL, R_DL, R_DL, R_DL, R_AL, R_DL }, + /*FACTION_C*/ { R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_DL, R_NO, R_DL, R_DL, R_DL, R_DL, R_DL, R_AL } + }; + + int iTargClass = pTarget->Classify(); + + if (iTargClass == CLASS_PLAYER && m_iPlayerReact) //LRC + { + if (m_iPlayerReact == 1) // Ignore player + return R_NO; + else if (m_iPlayerReact == 4) + return R_HT; + else if (m_afMemory & bits_MEMORY_PROVOKED) + return R_HT; + else + return R_NO; + } + + return iEnemy[ Classify() ][ iTargClass ]; +} + +//========================================================= +// FindCover - tries to find a nearby node that will hide +// the caller from its enemy. +// +// If supplied, search will return a node at least as far +// away as MinDist, but no farther than MaxDist. +// if MaxDist isn't supplied, it defaults to a reasonable +// value +//========================================================= +// UNDONE: Should this find the nearest node? + +//float CGraph::PathLength( int iStart, int iDest, int iHull, int afCapMask ) + +BOOL CBaseMonster :: FindCover ( Vector vecThreat, Vector vecViewOffset, float flMinDist, float flMaxDist ) +{ + int i; + int iMyHullIndex; + int iMyNode; + int iThreatNode; + float flDist; + Vector vecLookersOffset; + TraceResult tr; + + if ( !flMaxDist ) + { + // user didn't supply a MaxDist, so work up a crazy one. + flMaxDist = 784; + } + + if ( flMinDist > 0.5 * flMaxDist) + { +#if _DEBUG + ALERT ( at_debug, "FindCover MinDist (%.0f) too close to MaxDist (%.0f)\n", flMinDist, flMaxDist ); +#endif + flMinDist = 0.5 * flMaxDist; + } + + if ( !WorldGraph.m_fGraphPresent || !WorldGraph.m_fGraphPointersSet ) + { + ALERT ( at_aiconsole, "Graph not ready for findcover!\n" ); + return FALSE; + } + + iMyNode = WorldGraph.FindNearestNode( pev->origin, this ); + iThreatNode = WorldGraph.FindNearestNode ( vecThreat, this ); + iMyHullIndex = WorldGraph.HullIndex( this ); + + if ( iMyNode == NO_NODE ) + { + ALERT ( at_aiconsole, "FindCover() - %s has no nearest node!\n", STRING(pev->classname)); + return FALSE; + } + if ( iThreatNode == NO_NODE ) + { + // ALERT ( at_aiconsole, "FindCover() - Threat has no nearest node!\n" ); + iThreatNode = iMyNode; + // return FALSE; + } + + vecLookersOffset = vecThreat + vecViewOffset;// calculate location of enemy's eyes + + // we'll do a rough sample to find nodes that are relatively nearby + for ( i = 0 ; i < WorldGraph.m_cNodes ; i++ ) + { + int nodeNumber = (i + WorldGraph.m_iLastCoverSearch) % WorldGraph.m_cNodes; + + CNode &node = WorldGraph.Node( nodeNumber ); + WorldGraph.m_iLastCoverSearch = nodeNumber + 1; // next monster that searches for cover node will start where we left off here. + + // could use an optimization here!! + flDist = ( pev->origin - node.m_vecOrigin ).Length(); + + // DON'T do the trace check on a node that is farther away than a node that we've already found to + // provide cover! Also make sure the node is within the mins/maxs of the search. + if ( flDist >= flMinDist && flDist < flMaxDist ) + { + UTIL_TraceLine ( node.m_vecOrigin + vecViewOffset, vecLookersOffset, ignore_monsters, ignore_glass, ENT(pev), &tr ); + + // if this node will block the threat's line of sight to me... + if ( tr.flFraction != 1.0 ) + { + // ..and is also closer to me than the threat, or the same distance from myself and the threat the node is good. + if ( ( iMyNode == iThreatNode ) || WorldGraph.PathLength( iMyNode, nodeNumber, iMyHullIndex, m_afCapability ) <= WorldGraph.PathLength( iThreatNode, nodeNumber, iMyHullIndex, m_afCapability ) ) + { + if ( FValidateCover ( node.m_vecOrigin ) && MoveToLocation( ACT_RUN, 0, node.m_vecOrigin ) ) + { + /* + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE); + + WRITE_COORD( node.m_vecOrigin.x ); + WRITE_COORD( node.m_vecOrigin.y ); + WRITE_COORD( node.m_vecOrigin.z ); + + WRITE_COORD( vecLookersOffset.x ); + WRITE_COORD( vecLookersOffset.y ); + WRITE_COORD( vecLookersOffset.z ); + MESSAGE_END(); + */ + + return TRUE; + } + } + } + } + } + return FALSE; +} + + +//========================================================= +// BuildNearestRoute - tries to build a route as close to the target +// as possible, even if there isn't a path to the final point. +// +// If supplied, search will return a node at least as far +// away as MinDist from vecThreat, but no farther than MaxDist. +// if MaxDist isn't supplied, it defaults to a reasonable +// value +//========================================================= +BOOL CBaseMonster :: BuildNearestRoute ( Vector vecThreat, Vector vecViewOffset, float flMinDist, float flMaxDist ) +{ + int i; + int iMyHullIndex; + int iMyNode; + float flDist; + Vector vecLookersOffset; + TraceResult tr; + + if ( !flMaxDist ) + { + // user didn't supply a MaxDist, so work up a crazy one. + flMaxDist = 784; + } + + if ( flMinDist > 0.5 * flMaxDist) + { +#if _DEBUG + ALERT ( at_debug, "FindCover MinDist (%.0f) too close to MaxDist (%.0f)\n", flMinDist, flMaxDist ); +#endif + flMinDist = 0.5 * flMaxDist; + } + + if ( !WorldGraph.m_fGraphPresent || !WorldGraph.m_fGraphPointersSet ) + { + ALERT ( at_aiconsole, "Graph not ready for BuildNearestRoute!\n" ); + return FALSE; + } + + iMyNode = WorldGraph.FindNearestNode( pev->origin, this ); + iMyHullIndex = WorldGraph.HullIndex( this ); + + if ( iMyNode == NO_NODE ) + { + ALERT ( at_aiconsole, "BuildNearestRoute() - %s has no nearest node!\n", STRING(pev->classname)); + return FALSE; + } + + vecLookersOffset = vecThreat + vecViewOffset;// calculate location of enemy's eyes + + // we'll do a rough sample to find nodes that are relatively nearby + for ( i = 0 ; i < WorldGraph.m_cNodes ; i++ ) + { + int nodeNumber = (i + WorldGraph.m_iLastCoverSearch) % WorldGraph.m_cNodes; + + CNode &node = WorldGraph.Node( nodeNumber ); + WorldGraph.m_iLastCoverSearch = nodeNumber + 1; // next monster that searches for cover node will start where we left off here. + + // can I get there? + if (WorldGraph.NextNodeInRoute( iMyNode, nodeNumber, iMyHullIndex, 0 ) != iMyNode) + { + flDist = ( vecThreat - node.m_vecOrigin ).Length(); + + // is it close? + if ( flDist > flMinDist && flDist < flMaxDist) + { + // can I see where I want to be from there? + UTIL_TraceLine( node.m_vecOrigin + pev->view_ofs, vecLookersOffset, ignore_monsters, edict(), &tr ); + + if (tr.flFraction == 1.0) + { + // try to actually get there + if ( BuildRoute ( node.m_vecOrigin, bits_MF_TO_LOCATION, NULL ) ) + { + flMaxDist = flDist; + m_vecMoveGoal = node.m_vecOrigin; + return TRUE; // UNDONE: keep looking for something closer! + } + } + } + } + } + + return FALSE; +} + + + +//========================================================= +// BestVisibleEnemy - this functions searches the link +// list whose head is the caller's m_pLink field, and returns +// a pointer to the enemy entity in that list that is nearest the +// caller. +// +// !!!UNDONE - currently, this only returns the closest enemy. +// we'll want to consider distance, relationship, attack types, back turned, etc. +//========================================================= +CBaseEntity *CBaseMonster :: BestVisibleEnemy ( void ) +{ + CBaseEntity *pReturn; + CBaseEntity *pNextEnt; + int iNearest; + int iDist; + int iBestRelationship; + + iNearest = 8192;// so first visible entity will become the closest. + pNextEnt = m_pLink; + pReturn = NULL; + iBestRelationship = R_NO; + + while ( pNextEnt != NULL ) + { + if ( pNextEnt->IsAlive() ) + { + if ( IRelationship( pNextEnt) > iBestRelationship ) + { + // this entity is disliked MORE than the entity that we + // currently think is the best visible enemy. No need to do + // a distance check, just get mad at this one for now. + iBestRelationship = IRelationship ( pNextEnt ); + iNearest = ( pNextEnt->pev->origin - pev->origin ).Length(); + pReturn = pNextEnt; + } + else if ( IRelationship( pNextEnt) == iBestRelationship ) + { + // this entity is disliked just as much as the entity that + // we currently think is the best visible enemy, so we only + // get mad at it if it is closer. + iDist = ( pNextEnt->pev->origin - pev->origin ).Length(); + + if ( iDist <= iNearest ) + { + iNearest = iDist; + iBestRelationship = IRelationship ( pNextEnt ); + pReturn = pNextEnt; + } + } + } + + pNextEnt = pNextEnt->m_pLink; + } + + return pReturn; +} + + +//========================================================= +// MakeIdealYaw - gets a yaw value for the caller that would +// face the supplied vector. Value is stuffed into the monster's +// ideal_yaw +//========================================================= +void CBaseMonster :: MakeIdealYaw( Vector vecTarget ) +{ + Vector vecProjection; + + // strafing monster needs to face 90 degrees away from its goal + if ( m_movementActivity == ACT_STRAFE_LEFT ) + { + vecProjection.x = -vecTarget.y; + vecProjection.y = vecTarget.x; + + pev->ideal_yaw = UTIL_VecToYaw( vecProjection - pev->origin ); + } + else if ( m_movementActivity == ACT_STRAFE_RIGHT ) + { + vecProjection.x = vecTarget.y; + vecProjection.y = vecTarget.x; + + pev->ideal_yaw = UTIL_VecToYaw( vecProjection - pev->origin ); + } + else + { + pev->ideal_yaw = UTIL_VecToYaw ( vecTarget - pev->origin ); + } +} + +//========================================================= +// FlYawDiff - returns the difference ( in degrees ) between +// monster's current yaw and ideal_yaw +// +// Positive result is left turn, negative is right turn +//========================================================= +float CBaseMonster::FlYawDiff ( void ) +{ + float flCurrentYaw; + + flCurrentYaw = UTIL_AngleMod( pev->angles.y ); + + if ( flCurrentYaw == pev->ideal_yaw ) + { + return 0; + } + + + return UTIL_AngleDiff( pev->ideal_yaw, flCurrentYaw ); +} + + +//========================================================= +// Changeyaw - turns a monster towards its ideal_yaw +//========================================================= +float CBaseMonster::ChangeYaw ( int yawSpeed ) +{ + float ideal, current, move, speed; + + current = UTIL_AngleMod( pev->angles.y ); + ideal = pev->ideal_yaw; + if (current != ideal) + { + speed = (float)yawSpeed * gpGlobals->frametime * 10; + move = ideal - current; + + if (ideal > current) + { + if (move >= 180) + move = move - 360; + } + else + { + if (move <= -180) + move = move + 360; + } + + if (move > 0) + {// turning to the monster's left + if (move > speed) + move = speed; + } + else + {// turning to the monster's right + if (move < -speed) + move = -speed; + } + + pev->angles.y = UTIL_AngleMod (current + move); + + // turn head in desired direction only if they have a turnable head + if (m_afCapability & bits_CAP_TURN_HEAD) + { + float yaw = pev->ideal_yaw - pev->angles.y; + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + // yaw *= 0.8; + SetBoneController( 0, yaw ); + } + } + else + move = 0; + + return move; +} + +//========================================================= +// VecToYaw - turns a directional vector into a yaw value +// that points down that vector. +//========================================================= +float CBaseMonster::VecToYaw ( Vector vecDir ) +{ + if (vecDir.x == 0 && vecDir.y == 0 && vecDir.z == 0) + return pev->angles.y; + + return UTIL_VecToYaw( vecDir ); +} + + +//========================================================= +// SetEyePosition +// +// queries the monster's model for $eyeposition and copies +// that vector to the monster's view_ofs +// +//========================================================= +void CBaseMonster :: SetEyePosition ( void ) +{ + Vector vecEyePosition; + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + GetEyePosition( pmodel, vecEyePosition ); + + pev->view_ofs = vecEyePosition; + + if ( pev->view_ofs == g_vecZero ) + { + ALERT ( at_aiconsole, "%s has no view_ofs!\n", STRING ( pev->classname ) ); + } +} + +void CBaseMonster :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case SCRIPT_EVENT_DEAD: + if ( m_MonsterState == MONSTERSTATE_SCRIPT ) + { + pev->deadflag = DEAD_DYING; + // Kill me now! (and fade out when CineCleanup() is called) +#if _DEBUG + ALERT( at_aiconsole, "Death event: %s\n", STRING(pev->classname) ); +#endif + pev->health = 0; + } +#if _DEBUG + else + ALERT( at_aiconsole, "INVALID death event:%s\n", STRING(pev->classname) ); +#endif + break; + case SCRIPT_EVENT_NOT_DEAD: + if ( m_MonsterState == MONSTERSTATE_SCRIPT ) + { + pev->deadflag = DEAD_NO; + // This is for life/death sequences where the player can determine whether a character is dead or alive after the script + pev->health = pev->max_health; + } + break; + + case SCRIPT_EVENT_SOUND: // Play a named wave file + if ( !(pev->spawnflags & SF_MONSTER_GAG) || m_MonsterState != MONSTERSTATE_IDLE) + EMIT_SOUND( edict(), CHAN_BODY, pEvent->options, 1.0, ATTN_IDLE ); + break; + + case SCRIPT_EVENT_SOUND_VOICE: + if ( !(pev->spawnflags & SF_MONSTER_GAG) || m_MonsterState != MONSTERSTATE_IDLE) + EMIT_SOUND( edict(), CHAN_VOICE, pEvent->options, 1.0, ATTN_IDLE ); + break; + + case SCRIPT_EVENT_SENTENCE_RND1: // Play a named sentence group 33% of the time + if (RANDOM_LONG(0,2) == 0) + break; + // fall through... + case SCRIPT_EVENT_SENTENCE: // Play a named sentence group + SENTENCEG_PlayRndSz( edict(), pEvent->options, 1.0, ATTN_IDLE, 0, 100 ); + break; + + case SCRIPT_EVENT_FIREEVENT: // Fire a trigger + UTIL_FireTargets( pEvent->options, this, this, USE_TOGGLE ); + break; + + case SCRIPT_EVENT_NOINTERRUPT: // Can't be interrupted from now on + if ( m_pCine ) + m_pCine->AllowInterrupt( FALSE ); + break; + + case SCRIPT_EVENT_CANINTERRUPT: // OK to interrupt now + if ( m_pCine ) + m_pCine->AllowInterrupt( TRUE ); + break; + +#if 0 + case SCRIPT_EVENT_INAIR: // Don't DROP_TO_FLOOR() + case SCRIPT_EVENT_ENDANIMATION: // Set ending animation sequence to + break; +#endif + + case MONSTER_EVENT_BODYDROP_HEAVY: + if ( pev->flags & FL_ONGROUND ) + { + if ( RANDOM_LONG( 0, 1 ) == 0 ) + { + EMIT_SOUND_DYN( ENT(pev), CHAN_BODY, "common/bodydrop3.wav", 1, ATTN_NORM, 0, 90 ); + } + else + { + EMIT_SOUND_DYN( ENT(pev), CHAN_BODY, "common/bodydrop4.wav", 1, ATTN_NORM, 0, 90 ); + } + } + break; + + case MONSTER_EVENT_BODYDROP_LIGHT: + if ( pev->flags & FL_ONGROUND ) + { + if ( RANDOM_LONG( 0, 1 ) == 0 ) + { + EMIT_SOUND( ENT(pev), CHAN_BODY, "common/bodydrop3.wav", 1, ATTN_NORM ); + } + else + { + EMIT_SOUND( ENT(pev), CHAN_BODY, "common/bodydrop4.wav", 1, ATTN_NORM ); + } + } + break; + + case MONSTER_EVENT_SWISHSOUND: + { + // NO MONSTER may use this anim event unless that monster's precache precaches this sound!!! + EMIT_SOUND( ENT(pev), CHAN_BODY, "zombie/claw_miss2.wav", 1, ATTN_NORM ); + break; + } + + default: + ALERT( at_aiconsole, "Unhandled animation event %d for %s\n", pEvent->event, STRING(pev->classname) ); + break; + + } +} + + +// Combat + +Vector CBaseMonster :: GetGunPosition( ) +{ + UTIL_MakeVectors(pev->angles); + + // Vector vecSrc = pev->origin + gpGlobals->v_forward * 10; + //vecSrc.z = pevShooter->absmin.z + pevShooter->size.z * 0.7; + //vecSrc.z = pev->origin.z + (pev->view_ofs.z - 4); + Vector vecSrc = pev->origin + + gpGlobals->v_forward * m_HackedGunPos.y + + gpGlobals->v_right * m_HackedGunPos.x + + gpGlobals->v_up * m_HackedGunPos.z; + + return vecSrc; +} + + + + + +//========================================================= +// NODE GRAPH +//========================================================= + + + + + +//========================================================= +// FGetNodeRoute - tries to build an entire node path from +// the callers origin to the passed vector. If this is +// possible, ROUTE_SIZE waypoints will be copied into the +// callers m_Route. TRUE is returned if the operation +// succeeds (path is valid) or FALSE if failed (no path +// exists ) +//========================================================= +BOOL CBaseMonster :: FGetNodeRoute ( Vector vecDest ) +{ + int iPath[ MAX_PATH_SIZE ]; + int iSrcNode, iDestNode; + int iResult; + int i; + int iNumToCopy; + + iSrcNode = WorldGraph.FindNearestNode ( pev->origin, this ); + iDestNode = WorldGraph.FindNearestNode ( vecDest, this ); + + if ( iSrcNode == -1 ) + { + // no node nearest self +// ALERT ( at_aiconsole, "FGetNodeRoute: No valid node near self!\n" ); + return FALSE; + } + else if ( iDestNode == -1 ) + { + // no node nearest target +// ALERT ( at_aiconsole, "FGetNodeRoute: No valid node near target!\n" ); + return FALSE; + } + + // valid src and dest nodes were found, so it's safe to proceed with + // find shortest path + int iNodeHull = WorldGraph.HullIndex( this ); // make this a monster virtual function + iResult = WorldGraph.FindShortestPath ( iPath, iSrcNode, iDestNode, iNodeHull, m_afCapability ); + + if ( !iResult ) + { +#if 1 + ALERT ( at_aiconsole, "No Path from %d to %d!\n", iSrcNode, iDestNode ); + return FALSE; +#else + BOOL bRoutingSave = WorldGraph.m_fRoutingComplete; + WorldGraph.m_fRoutingComplete = FALSE; + iResult = WorldGraph.FindShortestPath(iPath, iSrcNode, iDestNode, iNodeHull, m_afCapability); + WorldGraph.m_fRoutingComplete = bRoutingSave; + if ( !iResult ) + { + ALERT ( at_aiconsole, "No Path from %d to %d!\n", iSrcNode, iDestNode ); + return FALSE; + } + else + { + ALERT ( at_aiconsole, "Routing is inconsistent!" ); + } +#endif + } + + // there's a valid path within iPath now, so now we will fill the route array + // up with as many of the waypoints as it will hold. + + // don't copy ROUTE_SIZE entries if the path returned is shorter + // than ROUTE_SIZE!!! + if ( iResult < ROUTE_SIZE ) + { + iNumToCopy = iResult; + } + else + { + iNumToCopy = ROUTE_SIZE; + } + + for ( i = 0 ; i < iNumToCopy; i++ ) + { + m_Route[ i ].vecLocation = WorldGraph.m_pNodes[ iPath[ i ] ].m_vecOrigin; + m_Route[ i ].iType = bits_MF_TO_NODE; + } + + if ( iNumToCopy < ROUTE_SIZE ) + { + m_Route[ iNumToCopy ].vecLocation = vecDest; + m_Route[ iNumToCopy ].iType |= bits_MF_IS_GOAL; + } + + return TRUE; +} + +//========================================================= +// FindHintNode +//========================================================= +int CBaseMonster :: FindHintNode ( void ) +{ + int i; + TraceResult tr; + + if ( !WorldGraph.m_fGraphPresent ) + { + ALERT ( at_aiconsole, "find_hintnode: graph not ready!\n" ); + return NO_NODE; + } + + if ( WorldGraph.m_iLastActiveIdleSearch >= WorldGraph.m_cNodes ) + { + WorldGraph.m_iLastActiveIdleSearch = 0; + } + + for ( i = 0; i < WorldGraph.m_cNodes ; i++ ) + { + int nodeNumber = (i + WorldGraph.m_iLastActiveIdleSearch) % WorldGraph.m_cNodes; + CNode &node = WorldGraph.Node( nodeNumber ); + + if ( node.m_sHintType ) + { + // this node has a hint. Take it if it is visible, the monster likes it, and the monster has an animation to match the hint's activity. + if ( FValidateHintType ( node.m_sHintType ) ) + { + if ( !node.m_sHintActivity || LookupActivity ( node.m_sHintActivity ) != ACTIVITY_NOT_AVAILABLE ) + { + UTIL_TraceLine ( pev->origin + pev->view_ofs, node.m_vecOrigin + pev->view_ofs, ignore_monsters, ENT(pev), &tr ); + + if ( tr.flFraction == 1.0 ) + { + WorldGraph.m_iLastActiveIdleSearch = nodeNumber + 1; // next monster that searches for hint nodes will start where we left off. + return nodeNumber;// take it! + } + } + } + } + } + + WorldGraph.m_iLastActiveIdleSearch = 0;// start at the top of the list for the next search. + + return NO_NODE; +} + + +void CBaseMonster::ReportAIState( void ) +{ + ALERT_TYPE level = at_console; + + static const char *pStateNames[] = { "None", "Idle", "Combat", "Alert", "Hunt", "Prone", "Scripted", "Dead" }; + + ALERT( level, "%s: ", STRING(pev->classname) ); + if ( (int)m_MonsterState < ARRAYSIZE(pStateNames) ) + ALERT( level, "State: %s, ", pStateNames[m_MonsterState] ); + int i = 0; + while ( activity_map[i].type != 0 ) + { + if ( activity_map[i].type == (int)m_Activity ) + { + ALERT( level, "Activity %s, ", activity_map[i].name ); + break; + } + i++; + } + + if ( m_pSchedule ) + { + const char *pName = NULL; + pName = m_pSchedule->pName; + if ( !pName ) + pName = "Unknown"; + ALERT( level, "Schedule %s, ", pName ); + Task_t *pTask = GetTask(); + if ( pTask ) + ALERT( level, "Task %d (#%d), ", pTask->iTask, m_iScheduleIndex ); + } + else + ALERT( level, "No Schedule, " ); + + if ( m_hEnemy != NULL ) + ALERT( level, "\nEnemy is %s", STRING(m_hEnemy->pev->classname) ); + else + ALERT( level, "No enemy" ); + + if ( IsMoving() ) + { + ALERT( level, " Moving " ); + if ( m_flMoveWaitFinished > gpGlobals->time ) + ALERT( level, ": Stopped for %.2f. ", m_flMoveWaitFinished - gpGlobals->time ); + else if ( m_IdealActivity == GetStoppedActivity() ) + ALERT( level, ": In stopped anim. " ); + } + + CSquadMonster *pSquadMonster = MySquadMonsterPointer(); + + if ( pSquadMonster ) + { + if ( !pSquadMonster->InSquad() ) + { + ALERT ( level, "not " ); + } + + ALERT ( level, "In Squad, " ); + + if ( !pSquadMonster->IsLeader() ) + { + ALERT ( level, "not " ); + } + + ALERT ( level, "Leader." ); + } + + ALERT( level, "\n" ); + ALERT( level, "Yaw speed:%3.1f,Health: %3.1f\n", pev->yaw_speed, pev->health ); + if ( pev->spawnflags & SF_MONSTER_PRISONER ) + ALERT( level, " PRISONER! " ); + if ( pev->spawnflags & SF_MONSTER_PREDISASTER ) + ALERT( level, " Pre-Disaster! " ); + ALERT( level, "\n" ); +} + +//========================================================= +// KeyValue +// +// !!! netname entvar field is used in squadmonster for groupname!!! +//========================================================= +void CBaseMonster :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "TriggerTarget")) + { + m_iszTriggerTarget = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "TriggerCondition") ) + { + m_iTriggerCondition = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iClass") ) //LRC + { + m_iClass = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iPlayerReact") ) + { + m_iPlayerReact = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "type") ) + { + pev->button = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else + { + CBaseAnimating::KeyValue( pkvd ); + } +} + +//========================================================= +// FCheckAITrigger - checks the monster's AI Trigger Conditions, +// if there is a condition, then checks to see if condition is +// met. If yes, the monster's TriggerTarget is fired. +// +// Returns TRUE if the target is fired. +//========================================================= +BOOL CBaseMonster :: FCheckAITrigger ( void ) +{ + BOOL fFireTarget; + + if ( m_iTriggerCondition == AITRIGGER_NONE ) + { + // no conditions, so this trigger is never fired. + return FALSE; + } + + fFireTarget = FALSE; + + switch ( m_iTriggerCondition ) + { + case AITRIGGER_SEEPLAYER_ANGRY_AT_PLAYER: + if ( m_hEnemy != NULL && m_hEnemy->IsPlayer() && HasConditions ( bits_COND_SEE_ENEMY ) ) + { + fFireTarget = TRUE; + } + break; + case AITRIGGER_SEEPLAYER_UNCONDITIONAL: + if ( HasConditions ( bits_COND_SEE_CLIENT ) ) + { + fFireTarget = TRUE; + } + break; + case AITRIGGER_SEEPLAYER_NOT_IN_COMBAT: + if ( HasConditions ( bits_COND_SEE_CLIENT ) && + m_MonsterState != MONSTERSTATE_COMBAT && + m_MonsterState != MONSTERSTATE_PRONE && + m_MonsterState != MONSTERSTATE_SCRIPT) + { + fFireTarget = TRUE; + } + break; + case AITRIGGER_TAKEDAMAGE: + if ( m_afConditions & ( bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE ) ) + { + fFireTarget = TRUE; + } + break; + case AITRIGGER_DEATH: + if ( pev->deadflag != DEAD_NO ) + { + fFireTarget = TRUE; + } + break; + case AITRIGGER_HALFHEALTH: + if ( IsAlive() && pev->health <= ( pev->max_health / 2 ) ) + { + fFireTarget = TRUE; + } + break; +/* + + // !!!UNDONE - no persistant game state that allows us to track these two. + + case AITRIGGER_SQUADMEMBERDIE: + break; + case AITRIGGER_SQUADLEADERDIE: + break; +*/ + case AITRIGGER_HEARWORLD: + if ( m_afConditions & bits_COND_HEAR_SOUND && m_afSoundTypes & bits_SOUND_WORLD ) + { + fFireTarget = TRUE; + } + break; + case AITRIGGER_HEARPLAYER: + if ( m_afConditions & bits_COND_HEAR_SOUND && m_afSoundTypes & bits_SOUND_PLAYER ) + { + fFireTarget = TRUE; + } + break; + case AITRIGGER_HEARCOMBAT: + if ( m_afConditions & bits_COND_HEAR_SOUND && m_afSoundTypes & bits_SOUND_COMBAT ) + { + fFireTarget = TRUE; + } + break; + } + + if ( fFireTarget ) + { + // fire the target, then set the trigger conditions to NONE so we don't fire again + ALERT ( at_aiconsole, "AI Trigger Fire Target\n" ); + UTIL_FireTargets( m_iszTriggerTarget , this, this, USE_TOGGLE ); + m_iTriggerCondition = AITRIGGER_NONE; + return TRUE; + } + + return FALSE; +} + +//========================================================= +// CanPlaySequence - determines whether or not the monster +// can play the scripted sequence or AI sequence that is +// trying to possess it. If DisregardState is set, the monster +// will be sucked into the script no matter what state it is +// in. ONLY Scripted AI ents should allow this. +//========================================================= + +//LRC - to help debug when sequences won't play... +#define DEBUG_CANTPLAY + +int CBaseMonster :: CanPlaySequence( int interruptFlags ) +{ + if ( m_pCine ) + { + if ( interruptFlags & SS_INTERRUPT_SCRIPTS ) + { + return true; + } + else + { +#ifdef DEBUG_CANTPLAY + ALERT(at_debug, "CANTPLAY: Already playing %s \"%s\"!\n", STRING(m_pCine->pev->classname), STRING(m_pCine->pev->targetname)); +#endif + return false; + } + } + else if ( !IsAlive() || m_MonsterState == MONSTERSTATE_PRONE ) + { +#ifdef DEBUG_CANTPLAY + ALERT(at_debug, "CANTPLAY: Dead/Barnacled!\n"); +#endif + // monster is already running a scripted sequence or dead! + return FALSE; + } + + if ( interruptFlags & SS_INTERRUPT_ANYSTATE ) + { + // ok to go, no matter what the monster state. (scripted AI) + return TRUE; + } + + if ( m_MonsterState == MONSTERSTATE_NONE || m_MonsterState == MONSTERSTATE_IDLE || m_IdealMonsterState == MONSTERSTATE_IDLE ) + { + // ok to go, but only in these states + return TRUE; + } + + if ( m_MonsterState == MONSTERSTATE_ALERT && interruptFlags & SS_INTERRUPT_ALERT ) + return TRUE; + + // unknown situation +#ifdef DEBUG_CANTPLAY + ALERT(at_debug, "CANTPLAY: non-interruptable state.\n"); +#endif + return FALSE; +} + + +//========================================================= +// FindLateralCover - attempts to locate a spot in the world +// directly to the left or right of the caller that will +// conceal them from view of pSightEnt +//========================================================= +#define COVER_CHECKS 5// how many checks are made +#define COVER_DELTA 48// distance between checks + +BOOL CBaseMonster :: FindLateralCover ( const Vector &vecThreat, const Vector &vecViewOffset ) +{ + TraceResult tr; + Vector vecBestOnLeft; + Vector vecBestOnRight; + Vector vecLeftTest; + Vector vecRightTest; + Vector vecStepRight; + int i; + + UTIL_MakeVectors ( pev->angles ); + vecStepRight = gpGlobals->v_right * COVER_DELTA; + vecStepRight.z = 0; + + vecLeftTest = vecRightTest = pev->origin; + + for ( i = 0 ; i < COVER_CHECKS ; i++ ) + { + vecLeftTest = vecLeftTest - vecStepRight; + vecRightTest = vecRightTest + vecStepRight; + + // it's faster to check the SightEnt's visibility to the potential spot than to check the local move, so we do that first. + UTIL_TraceLine( vecThreat + vecViewOffset, vecLeftTest + pev->view_ofs, ignore_monsters, ignore_glass, ENT(pev)/*pentIgnore*/, &tr); + + if (tr.flFraction != 1.0) + { + if ( FValidateCover ( vecLeftTest ) && CheckLocalMove( pev->origin, vecLeftTest, NULL, NULL ) == LOCALMOVE_VALID ) + { + if ( MoveToLocation( ACT_RUN, 0, vecLeftTest ) ) + { + return TRUE; + } + } + } + + // it's faster to check the SightEnt's visibility to the potential spot than to check the local move, so we do that first. + UTIL_TraceLine(vecThreat + vecViewOffset, vecRightTest + pev->view_ofs, ignore_monsters, ignore_glass, ENT(pev)/*pentIgnore*/, &tr); + + if ( tr.flFraction != 1.0 ) + { + if ( FValidateCover ( vecRightTest ) && CheckLocalMove( pev->origin, vecRightTest, NULL, NULL ) == LOCALMOVE_VALID ) + { + if ( MoveToLocation( ACT_RUN, 0, vecRightTest ) ) + { + return TRUE; + } + } + } + } + + return FALSE; +} + + +Vector CBaseMonster :: ShootAtEnemy( const Vector &shootOrigin ) +{ + if (m_pCine != NULL && m_hTargetEnt != NULL && (m_pCine->m_fTurnType == 1)) + { + Vector vecDest = ( m_hTargetEnt->pev->absmin + m_hTargetEnt->pev->absmax ) / 2; + return ( vecDest - shootOrigin ).Normalize(); + } + else if ( m_hEnemy ) + { + return ( (m_hEnemy->BodyTarget( shootOrigin ) - m_hEnemy->pev->origin) + m_vecEnemyLKP - shootOrigin ).Normalize(); + } + else return gpGlobals->v_forward; +} + + + +//========================================================= +// FacingIdeal - tells us if a monster is facing its ideal +// yaw. Created this function because many spots in the +// code were checking the yawdiff against this magic +// number. Nicer to have it in one place if we're gonna +// be stuck with it. +//========================================================= +BOOL CBaseMonster :: FacingIdeal( void ) +{ + if ( fabs( FlYawDiff() ) <= 0.006 )//!!!BUGBUG - no magic numbers!!! + { + return TRUE; + } + + return FALSE; +} + +//========================================================= +// FCanActiveIdle +//========================================================= +BOOL CBaseMonster :: FCanActiveIdle ( void ) +{ + /* + if ( m_MonsterState == MONSTERSTATE_IDLE && m_IdealMonsterState == MONSTERSTATE_IDLE && !IsMoving() ) + { + return TRUE; + } + */ + return FALSE; +} + + +void CBaseMonster::PlaySentence( const char *pszSentence, float duration, float volume, float attenuation ) +{ + if ( pszSentence && IsAlive() ) + { + if ( pszSentence[0] == '!' ) + EMIT_SOUND_DYN( edict(), CHAN_VOICE, pszSentence, volume, attenuation, 0, PITCH_NORM ); + else + SENTENCEG_PlayRndSz( edict(), pszSentence, volume, attenuation, 0, PITCH_NORM ); + } +} + + +void CBaseMonster::PlayScriptedSentence( const char *pszSentence, float duration, float volume, float attenuation, BOOL bConcurrent, CBaseEntity *pListener ) +{ + PlaySentence( pszSentence, duration, volume, attenuation ); +} + + +void CBaseMonster::SentenceStop( void ) +{ + EMIT_SOUND( edict(), CHAN_VOICE, "common/null.wav", 1.0, ATTN_IDLE ); +} + + +void CBaseMonster::CorpseFallThink( void ) +{ + if ( pev->flags & FL_ONGROUND ) + { + SetThink ( NULL ); + + //SetSequenceBox( ); + UTIL_SetOrigin( this, pev->origin );// link into world. + } + else + SetNextThink( 0.1 ); +} + +// Call after animation/pose is set up +void CBaseMonster :: MonsterInitDead( void ) +{ + InitBoneControllers(); + + pev->solid = SOLID_BBOX; + pev->movetype = MOVETYPE_TOSS;// so he'll fall to ground + + pev->frame = 0; + ResetSequenceInfo( ); + pev->framerate = 0; + + // Copy health + pev->max_health = pev->health; + pev->deadflag = DEAD_DEAD; + + UTIL_SetSize(pev, g_vecZero, g_vecZero ); + UTIL_SetOrigin( this, pev->origin ); + + // Setup health counters, etc. + BecomeDead(); + SetThink(&CBaseMonster :: CorpseFallThink ); + SetNextThink( 0.5 ); +} + +//========================================================= +// BBoxIsFlat - check to see if the monster's bounding box +// is lying flat on a surface (traces from all four corners +// are same length.) +//========================================================= +BOOL CBaseMonster :: BBoxFlat ( void ) +{ + TraceResult tr; + Vector vecPoint; + float flXSize, flYSize; + float flLength; + float flLength2; + + flXSize = pev->size.x / 2; + flYSize = pev->size.y / 2; + + vecPoint.x = pev->origin.x + flXSize; + vecPoint.y = pev->origin.y + flYSize; + vecPoint.z = pev->origin.z; + + UTIL_TraceLine ( vecPoint, vecPoint - Vector ( 0, 0, 100 ), ignore_monsters, ENT(pev), &tr ); + flLength = (vecPoint - tr.vecEndPos).Length(); + + vecPoint.x = pev->origin.x - flXSize; + vecPoint.y = pev->origin.y - flYSize; + + UTIL_TraceLine ( vecPoint, vecPoint - Vector ( 0, 0, 100 ), ignore_monsters, ENT(pev), &tr ); + flLength2 = (vecPoint - tr.vecEndPos).Length(); + if ( flLength2 > flLength ) + { + return FALSE; + } + flLength = flLength2; + + vecPoint.x = pev->origin.x - flXSize; + vecPoint.y = pev->origin.y + flYSize; + UTIL_TraceLine ( vecPoint, vecPoint - Vector ( 0, 0, 100 ), ignore_monsters, ENT(pev), &tr ); + flLength2 = (vecPoint - tr.vecEndPos).Length(); + if ( flLength2 > flLength ) + { + return FALSE; + } + flLength = flLength2; + + vecPoint.x = pev->origin.x + flXSize; + vecPoint.y = pev->origin.y - flYSize; + UTIL_TraceLine ( vecPoint, vecPoint - Vector ( 0, 0, 100 ), ignore_monsters, ENT(pev), &tr ); + flLength2 = (vecPoint - tr.vecEndPos).Length(); + if ( flLength2 > flLength ) + { + return FALSE; + } + flLength = flLength2; + + return TRUE; +} + +//========================================================= +// Get Enemy - tries to find the best suitable enemy for the monster. +//========================================================= +BOOL CBaseMonster :: GetEnemy ( void ) +{ + CBaseEntity *pNewEnemy; + + if ( HasConditions(bits_COND_SEE_HATE | bits_COND_SEE_DISLIKE | bits_COND_SEE_NEMESIS) ) + { + pNewEnemy = BestVisibleEnemy(); + + if ( pNewEnemy != m_hEnemy && pNewEnemy != NULL) + { + // DO NOT mess with the monster's m_hEnemy pointer unless the schedule the monster is currently running will be interrupted + // by COND_NEW_ENEMY. This will eliminate the problem of monsters getting a new enemy while they are in a schedule that doesn't care, + // and then not realizing it by the time they get to a schedule that does. I don't feel this is a good permanent fix. + + if ( m_pSchedule ) + { + if ( m_pSchedule->iInterruptMask & bits_COND_NEW_ENEMY ) + { + PushEnemy( m_hEnemy, m_vecEnemyLKP ); + SetConditions(bits_COND_NEW_ENEMY); + m_hEnemy = pNewEnemy; + m_vecEnemyLKP = m_hEnemy->pev->origin; + } + // if the new enemy has an owner, take that one as well + if (pNewEnemy->pev->owner != NULL) + { + CBaseEntity *pOwner = GetMonsterPointer( pNewEnemy->pev->owner ); + if ( pOwner && (pOwner->pev->flags & FL_MONSTER) && IRelationship( pOwner ) != R_NO ) + PushEnemy( pOwner, m_vecEnemyLKP ); + } + } + } + } + + // remember old enemies + if (m_hEnemy == NULL && PopEnemy( )) + { + if ( m_pSchedule ) + { + if ( m_pSchedule->iInterruptMask & bits_COND_NEW_ENEMY ) + { + SetConditions(bits_COND_NEW_ENEMY); + } + } + } + + if ( m_hEnemy != NULL ) + { + // monster has an enemy. + return TRUE; + } + + return FALSE;// monster has no enemy +} + +//========================================================= +// SetState +//========================================================= +void CBaseMonster :: SetState ( MONSTERSTATE State ) +{ +/* + if ( State != m_MonsterState ) + { + ALERT ( at_aiconsole, "State Changed to %d\n", State ); + } +*/ + + switch( State ) + { + + // Drop enemy pointers when going to idle + case MONSTERSTATE_IDLE: + + if ( m_hEnemy != NULL ) + { + m_hEnemy = NULL;// not allowed to have an enemy anymore. + ALERT ( at_aiconsole, "Stripped\n" ); + } + break; + } + + m_MonsterState = State; + m_IdealMonsterState = State; +} + +//========================================================= +// RunAI +//========================================================= +void CBaseMonster :: RunAI ( void ) +{ + // to test model's eye height + //UTIL_ParticleEffect ( pev->origin + pev->view_ofs, g_vecZero, 255, 10 ); + + // IDLE sound permitted in ALERT state is because monsters were silent in ALERT state. Only play IDLE sound in IDLE state + // once we have sounds for that state. + if ( ( m_MonsterState == MONSTERSTATE_IDLE || m_MonsterState == MONSTERSTATE_ALERT ) && RANDOM_LONG(0,99) == 0 && !(pev->flags & SF_MONSTER_GAG) ) + { + IdleSound(); + } + + if ( m_MonsterState != MONSTERSTATE_NONE && + m_MonsterState != MONSTERSTATE_PRONE && + m_MonsterState != MONSTERSTATE_DEAD )// don't bother with this crap if monster is prone. + { + // collect some sensory Condition information. + // don't let monsters outside of the player's PVS act up, or most of the interesting + // things will happen before the player gets there! + // UPDATE: We now let COMBAT state monsters think and act fully outside of player PVS. This allows the player to leave + // an area where monsters are fighting, and the fight will continue. + if ( !FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) || ( m_MonsterState == MONSTERSTATE_COMBAT ) || HaveCamerasInPVS( edict() )) + { + Look( m_flDistLook ); + Listen();// check for audible sounds. + + // now filter conditions. + ClearConditions( IgnoreConditions() ); + + GetEnemy(); + } + + // do these calculations if monster has an enemy. + if ( m_hEnemy != NULL ) + { + CheckEnemy( m_hEnemy ); + } + + CheckAmmo(); + } + + FCheckAITrigger(); + + PrescheduleThink(); + + MaintainSchedule(); + + // if the monster didn't use these conditions during the above call to MaintainSchedule() or CheckAITrigger() + // we throw them out cause we don't want them sitting around through the lifespan of a schedule + // that doesn't use them. + m_afConditions &= ~( bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE ); +} + +//========================================================= +// GetIdealState - surveys the Conditions information available +// and finds the best new state for a monster. +//========================================================= +MONSTERSTATE CBaseMonster :: GetIdealState ( void ) +{ + int iConditions; + + iConditions = IScheduleFlags(); + + // If no schedule conditions, the new ideal state is probably the reason we're in here. + switch ( m_MonsterState ) + { + case MONSTERSTATE_IDLE: + + /* + IDLE goes to ALERT upon hearing a sound + -IDLE goes to ALERT upon being injured + IDLE goes to ALERT upon seeing food + -IDLE goes to COMBAT upon sighting an enemy + IDLE goes to HUNT upon smelling food + */ + { + if ( iConditions & bits_COND_NEW_ENEMY ) + { + // new enemy! This means an idle monster has seen someone it dislikes, or + // that a monster in combat has found a more suitable target to attack + m_IdealMonsterState = MONSTERSTATE_COMBAT; + } + else if ( iConditions & bits_COND_LIGHT_DAMAGE ) + { + MakeIdealYaw ( m_vecEnemyLKP ); + m_IdealMonsterState = MONSTERSTATE_ALERT; + } + else if ( iConditions & bits_COND_HEAVY_DAMAGE ) + { + MakeIdealYaw ( m_vecEnemyLKP ); + m_IdealMonsterState = MONSTERSTATE_ALERT; + } + else if ( iConditions & bits_COND_HEAR_SOUND ) + { + CSound *pSound; + + pSound = PBestSound(); + ASSERT( pSound != NULL ); + if ( pSound ) + { + MakeIdealYaw ( pSound->m_vecOrigin ); + if ( pSound->m_iType & (bits_SOUND_COMBAT|bits_SOUND_DANGER) ) + m_IdealMonsterState = MONSTERSTATE_ALERT; + } + } + else if ( iConditions & (bits_COND_SMELL | bits_COND_SMELL_FOOD) ) + { + m_IdealMonsterState = MONSTERSTATE_ALERT; + } + + break; + } + case MONSTERSTATE_ALERT: + /* + ALERT goes to IDLE upon becoming bored + -ALERT goes to COMBAT upon sighting an enemy + ALERT goes to HUNT upon hearing a noise + */ + { + if ( iConditions & (bits_COND_NEW_ENEMY|bits_COND_SEE_ENEMY) ) + { + // see an enemy we MUST attack + m_IdealMonsterState = MONSTERSTATE_COMBAT; + } + else if ( iConditions & bits_COND_HEAR_SOUND ) + { + m_IdealMonsterState = MONSTERSTATE_ALERT; + CSound *pSound = PBestSound(); + ASSERT( pSound != NULL ); + if ( pSound ) + MakeIdealYaw ( pSound->m_vecOrigin ); + } + break; + } + case MONSTERSTATE_COMBAT: + /* + COMBAT goes to HUNT upon losing sight of enemy + COMBAT goes to ALERT upon death of enemy + */ + { + if ( m_hEnemy == NULL ) + { + m_IdealMonsterState = MONSTERSTATE_ALERT; + // pev->effects = EF_BRIGHTFIELD; + ALERT ( at_aiconsole, "***Combat state with no enemy!\n" ); + } + break; + } + case MONSTERSTATE_HUNT: + /* + HUNT goes to ALERT upon seeing food + HUNT goes to ALERT upon being injured + HUNT goes to IDLE if goal touched + HUNT goes to COMBAT upon seeing enemy + */ + { + break; + } + case MONSTERSTATE_SCRIPT: + if ( iConditions & (bits_COND_TASK_FAILED|bits_COND_LIGHT_DAMAGE|bits_COND_HEAVY_DAMAGE) ) + { + ExitScriptedSequence(); // This will set the ideal state + } + break; + + case MONSTERSTATE_DEAD: + m_IdealMonsterState = MONSTERSTATE_DEAD; + break; + } + + return m_IdealMonsterState; +} + +//========================================================= +// FHaveSchedule - Returns TRUE if monster's m_pSchedule +// is anything other than NULL. +//========================================================= +BOOL CBaseMonster :: FHaveSchedule( void ) +{ + if ( m_pSchedule == NULL ) + { + return FALSE; + } + + return TRUE; +} + +//========================================================= +// ClearSchedule - blanks out the caller's schedule pointer +// and index. +//========================================================= +void CBaseMonster :: ClearSchedule( void ) +{ + m_iTaskStatus = TASKSTATUS_NEW; + m_pSchedule = NULL; + m_iScheduleIndex = 0; +} + +//========================================================= +// FScheduleDone - Returns TRUE if the caller is on the +// last task in the schedule +//========================================================= +BOOL CBaseMonster :: FScheduleDone ( void ) +{ + ASSERT( m_pSchedule != NULL ); + + if ( m_iScheduleIndex == m_pSchedule->cTasks ) + { + return TRUE; + } + + return FALSE; +} + +//========================================================= +// ChangeSchedule - replaces the monster's schedule pointer +// with the passed pointer, and sets the ScheduleIndex back +// to 0 +//========================================================= +void CBaseMonster :: ChangeSchedule ( Schedule_t *pNewSchedule ) +{ + ASSERT( pNewSchedule != NULL ); + + m_pSchedule = pNewSchedule; + m_iScheduleIndex = 0; + m_iTaskStatus = TASKSTATUS_NEW; + m_afConditions = 0;// clear all of the conditions + m_failSchedule = SCHED_NONE; + + if ( m_pSchedule->iInterruptMask & bits_COND_HEAR_SOUND && !(m_pSchedule->iSoundMask) ) + { + ALERT ( at_aiconsole, "COND_HEAR_SOUND with no sound mask!\n" ); + } + else if ( m_pSchedule->iSoundMask && !(m_pSchedule->iInterruptMask & bits_COND_HEAR_SOUND) ) + { + ALERT ( at_aiconsole, "Sound mask without COND_HEAR_SOUND!\n" ); + } + +#if _DEBUG + if ( !ScheduleFromName( pNewSchedule->pName ) ) + { + ALERT( at_debug, "Schedule %s not in table!!!\n", pNewSchedule->pName ); + } +#endif + +// this is very useful code if you can isolate a test case in a level with a single monster. It will notify +// you of every schedule selection the monster makes. +#if 0 + if ( FClassnameIs( pev, "monster_human_grunt" ) ) + { + Task_t *pTask = GetTask(); + + if ( pTask ) + { + const char *pName = NULL; + + if ( m_pSchedule ) + { + pName = m_pSchedule->pName; + } + else + { + pName = "No Schedule"; + } + + if ( !pName ) + { + pName = "Unknown"; + } + + ALERT( at_aiconsole, "%s: picked schedule %s\n", STRING( pev->classname ), pName ); + } + } +#endif// 0 + +} + +//========================================================= +// NextScheduledTask - increments the ScheduleIndex +//========================================================= +void CBaseMonster :: NextScheduledTask ( void ) +{ + ASSERT( m_pSchedule != NULL ); + + m_iTaskStatus = TASKSTATUS_NEW; + m_iScheduleIndex++; + + if ( FScheduleDone() ) + { + // just completed last task in schedule, so make it invalid by clearing it. + SetConditions( bits_COND_SCHEDULE_DONE ); + //ClearSchedule(); + } +} + +//========================================================= +// IScheduleFlags - returns an integer with all Conditions +// bits that are currently set and also set in the current +// schedule's Interrupt mask. +//========================================================= +int CBaseMonster :: IScheduleFlags ( void ) +{ + if( !m_pSchedule ) + { + return 0; + } + + // strip off all bits excepts the ones capable of breaking this schedule. + return m_afConditions & m_pSchedule->iInterruptMask; +} + +//========================================================= +// FScheduleValid - returns TRUE as long as the current +// schedule is still the proper schedule to be executing, +// taking into account all conditions +//========================================================= +BOOL CBaseMonster :: FScheduleValid ( void ) +{ + if ( m_pSchedule == NULL ) + { + // schedule is empty, and therefore not valid. + return FALSE; + } + + if ( HasConditions( m_pSchedule->iInterruptMask | bits_COND_SCHEDULE_DONE | bits_COND_TASK_FAILED ) ) + { +#ifdef DEBUG + if ( HasConditions ( bits_COND_TASK_FAILED ) && m_failSchedule == SCHED_NONE ) + { + // fail! Send a visual indicator. + Vector tmp = pev->origin; + tmp.z = pev->absmax.z + 16; + UTIL_Sparks( tmp ); + } +#endif // DEBUG + + // some condition has interrupted the schedule, or the schedule is done + return FALSE; + } + + return TRUE; +} + +//========================================================= +// MaintainSchedule - does all the per-think schedule maintenance. +// ensures that the monster leaves this function with a valid +// schedule! +//========================================================= +void CBaseMonster :: MaintainSchedule ( void ) +{ + Schedule_t *pNewSchedule; + int i; + + // UNDONE: Tune/fix this 10... This is just here so infinite loops are impossible + for ( i = 0; i < 10; i++ ) + { + if ( m_pSchedule != NULL && TaskIsComplete() ) + { + NextScheduledTask(); + } + + // validate existing schedule + if ( !FScheduleValid() || m_MonsterState != m_IdealMonsterState ) + { + // if we come into this block of code, the schedule is going to have to be changed. + // if the previous schedule was interrupted by a condition, GetIdealState will be + // called. Else, a schedule finished normally. + + // Notify the monster that his schedule is changing + ScheduleChange(); + + // Call GetIdealState if we're not dead and one or more of the following... + // - in COMBAT state with no enemy (it died?) + // - conditions bits (excluding SCHEDULE_DONE) indicate interruption, + // - schedule is done but schedule indicates it wants GetIdealState called + // after successful completion (by setting bits_COND_SCHEDULE_DONE in iInterruptMask) + // DEAD & SCRIPT are not suggestions, they are commands! + if ( m_IdealMonsterState != MONSTERSTATE_DEAD && + (m_IdealMonsterState != MONSTERSTATE_SCRIPT || m_IdealMonsterState == m_MonsterState) ) + { + // if we're here, then either we're being told to do something (besides dying or playing a script) + // or our current schedule (besides dying) is invalid. -- LRC + if ( (m_afConditions && !HasConditions(bits_COND_SCHEDULE_DONE)) || + (m_pSchedule && (m_pSchedule->iInterruptMask & bits_COND_SCHEDULE_DONE)) || + ((m_MonsterState == MONSTERSTATE_COMBAT) && (m_hEnemy == NULL)) ) + { + GetIdealState(); + } + } + if ( HasConditions( bits_COND_TASK_FAILED ) && m_MonsterState == m_IdealMonsterState ) + { + if ( m_failSchedule != SCHED_NONE ) + pNewSchedule = GetScheduleOfType( m_failSchedule ); + else + pNewSchedule = GetScheduleOfType( SCHED_FAIL ); + // schedule was invalid because the current task failed to start or complete + ALERT ( at_aiconsole, "Schedule Failed at %d!\n", m_iScheduleIndex ); + ChangeSchedule( pNewSchedule ); + } + else + { + SetState( m_IdealMonsterState ); + if ( m_MonsterState == MONSTERSTATE_SCRIPT || m_MonsterState == MONSTERSTATE_DEAD ) + { + pNewSchedule = CBaseMonster::GetSchedule(); + } + else + pNewSchedule = GetSchedule(); + ChangeSchedule( pNewSchedule ); + } + } + + if ( m_iTaskStatus == TASKSTATUS_NEW ) + { + Task_t *pTask = GetTask(); + ASSERT( pTask != NULL ); + TaskBegin(); + StartTask( pTask ); + } + + // UNDONE: Twice?!!! + if ( m_Activity != m_IdealActivity ) + { + SetActivity ( m_IdealActivity ); + } + + if ( !TaskIsComplete() && m_iTaskStatus != TASKSTATUS_NEW ) + break; + } + + if ( TaskIsRunning() ) + { + Task_t *pTask = GetTask(); + ASSERT( pTask != NULL ); + RunTask( pTask ); + } + + // UNDONE: We have to do this so that we have an animation set to blend to if RunTask changes the animation + // RunTask() will always change animations at the end of a script! + // Don't do this twice + if ( m_Activity != m_IdealActivity ) + { + SetActivity ( m_IdealActivity ); + } +} + +//========================================================= +// RunTask +//========================================================= +void CBaseMonster :: RunTask ( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_TURN_RIGHT: + case TASK_TURN_LEFT: + { + ChangeYaw( pev->yaw_speed ); + + if ( FacingIdeal() ) + { + TaskComplete(); + } + break; + } + + case TASK_PLAY_SEQUENCE_FACE_ENEMY: + case TASK_PLAY_SEQUENCE_FACE_TARGET: + { + CBaseEntity *pTarget; + + if ( pTask->iTask == TASK_PLAY_SEQUENCE_FACE_TARGET ) + pTarget = m_hTargetEnt; + else + pTarget = m_hEnemy; + if ( pTarget ) + { + pev->ideal_yaw = UTIL_VecToYaw( pTarget->pev->origin - pev->origin ); + ChangeYaw( pev->yaw_speed ); + } + if ( m_fSequenceFinished ) + TaskComplete(); + } + break; + + case TASK_PLAY_SEQUENCE: + case TASK_PLAY_ACTIVE_IDLE: + { + if ( m_fSequenceFinished ) + { + TaskComplete(); + } + break; + } + + + case TASK_FACE_ENEMY: + { + MakeIdealYaw( m_vecEnemyLKP ); + + ChangeYaw( pev->yaw_speed ); + + if ( FacingIdeal() ) + { + TaskComplete(); + } + break; + } + case TASK_FACE_HINTNODE: + case TASK_FACE_LASTPOSITION: + case TASK_FACE_TARGET: + case TASK_FACE_IDEAL: + case TASK_FACE_ROUTE: + { + ChangeYaw( pev->yaw_speed ); + + if ( FacingIdeal() ) + { + TaskComplete(); + } + break; + } + case TASK_WAIT_PVS: + { + if ( !FNullEnt(FIND_CLIENT_IN_PVS(edict())) || HaveCamerasInPVS( edict() )) + { + TaskComplete(); + } + break; + } + case TASK_WAIT_INDEFINITE: + { + // don't do anything. + break; + } + case TASK_WAIT: + case TASK_WAIT_RANDOM: + { + if ( gpGlobals->time >= m_flWaitFinished ) + { + TaskComplete(); + } + break; + } + case TASK_WAIT_FACE_ENEMY: + { + MakeIdealYaw ( m_vecEnemyLKP ); + ChangeYaw( pev->yaw_speed ); + + if ( gpGlobals->time >= m_flWaitFinished ) + { + TaskComplete(); + } + break; + } + case TASK_MOVE_TO_TARGET_RANGE: + { + float distance; + + if ( m_hTargetEnt == NULL ) + TaskFail(); + else + { + distance = ( m_vecMoveGoal - pev->origin ).Length2D(); + // Re-evaluate when you think your finished, or the target has moved too far + if ( (distance < pTask->flData) || (m_vecMoveGoal - m_hTargetEnt->pev->origin).Length() > pTask->flData * 0.5 ) + { + m_vecMoveGoal = m_hTargetEnt->pev->origin; + distance = ( m_vecMoveGoal - pev->origin ).Length2D(); + FRefreshRoute(); + } + + // Set the appropriate activity based on an overlapping range + // overlap the range to prevent oscillation + if ( distance < pTask->flData ) + { + TaskComplete(); + RouteClear(); // Stop moving + } + else if ( distance < 190 && m_movementActivity != ACT_WALK ) + m_movementActivity = ACT_WALK; + else if ( distance >= 270 && m_movementActivity != ACT_RUN ) + m_movementActivity = ACT_RUN; + } + + break; + } + case TASK_WAIT_FOR_MOVEMENT: + { + if (MovementIsComplete()) + { + TaskComplete(); + RouteClear(); // Stop moving + } + break; + } + case TASK_DIE: + { + if ( m_fSequenceFinished && pev->frame >= 255 ) + { + pev->deadflag = DEAD_DEAD; + + SetThink ( NULL ); + StopAnimation(); + UTIL_SetSize ( pev, Vector ( -4, -4, 0 ), Vector ( 4, 4, 1 ) ); + CSoundEnt::InsertSound ( bits_SOUND_CARCASS, pev->origin, 384, 30 ); + } + break; + } + case TASK_RANGE_ATTACK1_NOTURN: + case TASK_MELEE_ATTACK1_NOTURN: + case TASK_MELEE_ATTACK2_NOTURN: + case TASK_RANGE_ATTACK2_NOTURN: + case TASK_RELOAD_NOTURN: + { + if ( m_fSequenceFinished ) + { + m_Activity = ACT_RESET; + TaskComplete(); + } + break; + } + case TASK_RANGE_ATTACK1: + case TASK_MELEE_ATTACK1: + case TASK_MELEE_ATTACK2: + case TASK_RANGE_ATTACK2: + case TASK_SPECIAL_ATTACK1: + case TASK_SPECIAL_ATTACK2: + case TASK_RELOAD: + { + MakeIdealYaw ( m_vecEnemyLKP ); + ChangeYaw ( pev->yaw_speed ); + + if ( m_fSequenceFinished ) + { + m_Activity = ACT_RESET; + TaskComplete(); + } + break; + } + case TASK_SMALL_FLINCH: + { + if ( m_fSequenceFinished ) + { + TaskComplete(); + } + } + break; + case TASK_WAIT_FOR_SCRIPT: + { + if ( m_pCine->m_iDelay <= 0 && gpGlobals->time >= m_pCine->m_startTime ) + { + TaskComplete(); + } + break; + } + case TASK_PLAY_SCRIPT: + { +// ALERT(at_console, "Play Script\n"); + if (m_fSequenceFinished) + { +// ALERT(at_console, "Anim Finished\n"); + if (m_pCine->m_iRepeatsLeft > 0) + { +// ALERT(at_console, "Frame %f; Repeat %d from %f\n", pev->frame, m_pCine->m_iRepeatsLeft, m_pCine->m_fRepeatFrame); + m_pCine->m_iRepeatsLeft--; + pev->frame = m_pCine->m_fRepeatFrame; + ResetSequenceInfo( ); + } + else + { + TaskComplete(); + } + } + break; + } + } +} + +//========================================================= +// SetTurnActivity - measures the difference between the way +// the monster is facing and determines whether or not to +// select one of the 180 turn animations. +//========================================================= +void CBaseMonster :: SetTurnActivity ( void ) +{ + float flYD; + flYD = FlYawDiff(); + + if ( flYD <= -45 && LookupActivity ( ACT_TURN_RIGHT ) != ACTIVITY_NOT_AVAILABLE ) + {// big right turn + m_IdealActivity = ACT_TURN_RIGHT; + } + else if ( flYD > 45 && LookupActivity ( ACT_TURN_LEFT ) != ACTIVITY_NOT_AVAILABLE ) + {// big left turn + m_IdealActivity = ACT_TURN_LEFT; + } +} + +//========================================================= +// Start task - selects the correct activity and performs +// any necessary calculations to start the next task on the +// schedule. +//========================================================= +void CBaseMonster :: StartTask ( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_TURN_RIGHT: + { + float flCurrentYaw; + + flCurrentYaw = UTIL_AngleMod( pev->angles.y ); + pev->ideal_yaw = UTIL_AngleMod( flCurrentYaw - pTask->flData ); + SetTurnActivity(); + break; + } + case TASK_TURN_LEFT: + { + float flCurrentYaw; + + flCurrentYaw = UTIL_AngleMod( pev->angles.y ); + pev->ideal_yaw = UTIL_AngleMod( flCurrentYaw + pTask->flData ); + SetTurnActivity(); + break; + } + case TASK_REMEMBER: + { + Remember ( (int)pTask->flData ); + TaskComplete(); + break; + } + case TASK_FORGET: + { + Forget ( (int)pTask->flData ); + TaskComplete(); + break; + } + case TASK_FIND_HINTNODE: + { + m_iHintNode = FindHintNode(); + + if ( m_iHintNode != NO_NODE ) + { + TaskComplete(); + } + else + { + TaskFail(); + } + break; + } + case TASK_STORE_LASTPOSITION: + { + m_vecLastPosition = pev->origin; + TaskComplete(); + break; + } + case TASK_CLEAR_LASTPOSITION: + { + m_vecLastPosition = g_vecZero; + TaskComplete(); + break; + } + case TASK_CLEAR_HINTNODE: + { + m_iHintNode = NO_NODE; + TaskComplete(); + break; + } + case TASK_STOP_MOVING: + { + if ( m_IdealActivity == m_movementActivity ) + { + m_IdealActivity = GetStoppedActivity(); + } + + RouteClear(); + TaskComplete(); + break; + } + case TASK_PLAY_SEQUENCE_FACE_ENEMY: + case TASK_PLAY_SEQUENCE_FACE_TARGET: + case TASK_PLAY_SEQUENCE: + { + m_IdealActivity = ( Activity )( int )pTask->flData; + break; + } + case TASK_PLAY_ACTIVE_IDLE: + { + // monsters verify that they have a sequence for the node's activity BEFORE + // moving towards the node, so it's ok to just set the activity without checking here. + m_IdealActivity = ( Activity )WorldGraph.m_pNodes[ m_iHintNode ].m_sHintActivity; + break; + } + case TASK_SET_SCHEDULE: + { + Schedule_t *pNewSchedule; + + pNewSchedule = GetScheduleOfType( (int)pTask->flData ); + + if ( pNewSchedule ) + { + ChangeSchedule( pNewSchedule ); + } + else + { + TaskFail(); + } + + break; + } + case TASK_FIND_NEAR_NODE_COVER_FROM_ENEMY: + { + if ( m_hEnemy == NULL ) + { + TaskFail(); + return; + } + + if ( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, 0, pTask->flData ) ) + { + // try for cover farther than the FLData from the schedule. + TaskComplete(); + } + else + { + // no coverwhatsoever. + TaskFail(); + } + break; + } + case TASK_FIND_FAR_NODE_COVER_FROM_ENEMY: + { + if ( m_hEnemy == NULL ) + { + TaskFail(); + return; + } + + if ( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, pTask->flData, CoverRadius() ) ) + { + // try for cover farther than the FLData from the schedule. + TaskComplete(); + } + else + { + // no coverwhatsoever. + TaskFail(); + } + break; + } + case TASK_FIND_NODE_COVER_FROM_ENEMY: + { + if ( m_hEnemy == NULL ) + { + TaskFail(); + return; + } + + if ( FindCover( m_hEnemy->pev->origin, m_hEnemy->pev->view_ofs, 0, CoverRadius() ) ) + { + // try for cover farther than the FLData from the schedule. + TaskComplete(); + } + else + { + // no coverwhatsoever. + TaskFail(); + } + break; + } + case TASK_FIND_COVER_FROM_ENEMY: + { + entvars_t *pevCover; + + if ( m_hEnemy == NULL ) + { + // Find cover from self if no enemy available + pevCover = pev; +// TaskFail(); +// return; + } + else + pevCover = m_hEnemy->pev; + + if ( FindLateralCover( pevCover->origin, pevCover->view_ofs ) ) + { + // try lateral first + m_flMoveWaitFinished = gpGlobals->time + pTask->flData; + TaskComplete(); + } + else if ( FindCover( pevCover->origin, pevCover->view_ofs, 0, CoverRadius() ) ) + { + // then try for plain ole cover + m_flMoveWaitFinished = gpGlobals->time + pTask->flData; + TaskComplete(); + } + else + { + // no coverwhatsoever. + TaskFail(); + } + break; + } + case TASK_FIND_COVER_FROM_ORIGIN: + { + if ( FindCover( pev->origin, pev->view_ofs, 0, CoverRadius() ) ) + { + // then try for plain ole cover + m_flMoveWaitFinished = gpGlobals->time + pTask->flData; + TaskComplete(); + } + else + { + // no cover! + TaskFail(); + } + } + break; + case TASK_FIND_COVER_FROM_BEST_SOUND: + { + CSound *pBestSound; + + pBestSound = PBestSound(); + + ASSERT( pBestSound != NULL ); + /* + if ( pBestSound && FindLateralCover( pBestSound->m_vecOrigin, g_vecZero ) ) + { + // try lateral first + m_flMoveWaitFinished = gpGlobals->time + pTask->flData; + TaskComplete(); + } + */ + + if ( pBestSound && FindCover( pBestSound->m_vecOrigin, g_vecZero, pBestSound->m_iVolume, CoverRadius() ) ) + { + // then try for plain ole cover + m_flMoveWaitFinished = gpGlobals->time + pTask->flData; + TaskComplete(); + } + else + { + // no coverwhatsoever. or no sound in list + TaskFail(); + } + break; + } + case TASK_FACE_HINTNODE: + { + pev->ideal_yaw = WorldGraph.m_pNodes[ m_iHintNode ].m_flHintYaw; + SetTurnActivity(); + break; + } + + case TASK_FACE_LASTPOSITION: + MakeIdealYaw ( m_vecLastPosition ); + SetTurnActivity(); + break; + + case TASK_FACE_TARGET: + if ( m_hTargetEnt != NULL ) + { + MakeIdealYaw ( m_hTargetEnt->pev->origin ); + SetTurnActivity(); + } + else + TaskFail(); + break; + case TASK_FACE_ENEMY: + { + MakeIdealYaw ( m_vecEnemyLKP ); + SetTurnActivity(); + break; + } + case TASK_FACE_IDEAL: + { + SetTurnActivity(); + break; + } + case TASK_FACE_ROUTE: + { + if (FRouteClear()) + { + ALERT(at_aiconsole, "No route to face!\n"); + TaskFail(); + } + else + { + MakeIdealYaw(m_Route[m_iRouteIndex].vecLocation); + SetTurnActivity(); + } + break; + } + case TASK_WAIT_PVS: + case TASK_WAIT_INDEFINITE: + { + // don't do anything. + break; + } + case TASK_WAIT: + case TASK_WAIT_FACE_ENEMY: + {// set a future time that tells us when the wait is over. + m_flWaitFinished = gpGlobals->time + pTask->flData; + break; + } + case TASK_WAIT_RANDOM: + {// set a future time that tells us when the wait is over. + m_flWaitFinished = gpGlobals->time + RANDOM_FLOAT( 0.1, pTask->flData ); + break; + } + case TASK_MOVE_TO_TARGET_RANGE: + { + if ( (m_hTargetEnt->pev->origin - pev->origin).Length() < 1 ) + TaskComplete(); + else + { + m_vecMoveGoal = m_hTargetEnt->pev->origin; + if ( !MoveToTarget( ACT_WALK, 2 ) ) + TaskFail(); + } + break; + } + case TASK_RUN_TO_SCRIPT: + case TASK_WALK_TO_SCRIPT: + { + Activity newActivity; + + if ( !m_pGoalEnt || (m_pGoalEnt->pev->origin - pev->origin).Length() < 1 ) + TaskComplete(); + else + { + if ( pTask->iTask == TASK_WALK_TO_SCRIPT ) + newActivity = ACT_WALK; + else + newActivity = ACT_RUN; + // This monster can't do this! + if ( LookupActivity( newActivity ) == ACTIVITY_NOT_AVAILABLE ) + TaskComplete(); + else + { + if ( m_pGoalEnt != NULL ) + { + Vector vecDest; + vecDest = m_pGoalEnt->pev->origin; + + if ( !MoveToLocation( newActivity, 2, vecDest ) ) + { + TaskFail(); + ALERT( at_aiconsole, "%s Failed to reach script!!!\n", STRING(pev->classname) ); + RouteClear(); + } + } + else + { + TaskFail(); + ALERT( at_aiconsole, "%s: MoveTarget is missing!?!\n", STRING(pev->classname) ); + RouteClear(); + } + } + } + TaskComplete(); + break; + } + case TASK_CLEAR_MOVE_WAIT: + { + m_flMoveWaitFinished = gpGlobals->time; + TaskComplete(); + break; + } + case TASK_MELEE_ATTACK1_NOTURN: + case TASK_MELEE_ATTACK1: + { + m_IdealActivity = ACT_MELEE_ATTACK1; + break; + } + case TASK_MELEE_ATTACK2_NOTURN: + case TASK_MELEE_ATTACK2: + { + m_IdealActivity = ACT_MELEE_ATTACK2; + break; + } + case TASK_RANGE_ATTACK1_NOTURN: + case TASK_RANGE_ATTACK1: + { + m_IdealActivity = ACT_RANGE_ATTACK1; + break; + } + case TASK_RANGE_ATTACK2_NOTURN: + case TASK_RANGE_ATTACK2: + { + m_IdealActivity = ACT_RANGE_ATTACK2; + break; + } + case TASK_RELOAD_NOTURN: + case TASK_RELOAD: + { + m_IdealActivity = ACT_RELOAD; + break; + } + case TASK_SPECIAL_ATTACK1: + { + m_IdealActivity = ACT_SPECIAL_ATTACK1; + break; + } + case TASK_SPECIAL_ATTACK2: + { + m_IdealActivity = ACT_SPECIAL_ATTACK2; + break; + } + case TASK_SET_ACTIVITY: + { + m_IdealActivity = (Activity)(int)pTask->flData; + TaskComplete(); + break; + } + case TASK_GET_PATH_TO_ENEMY_LKP: + { + if ( BuildRoute ( m_vecEnemyLKP, bits_MF_TO_LOCATION, NULL ) ) + { + TaskComplete(); + } + else if (BuildNearestRoute( m_vecEnemyLKP, pev->view_ofs, 0, (m_vecEnemyLKP - pev->origin).Length() )) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToEnemyLKP failed!!\n" ); + TaskFail(); + } + break; + } + case TASK_GET_PATH_TO_ENEMY: + { + CBaseEntity *pEnemy = m_hEnemy; + + if ( pEnemy == NULL ) + { + TaskFail(); + return; + } + + if ( BuildRoute ( pEnemy->pev->origin, bits_MF_TO_ENEMY, pEnemy ) ) + { + TaskComplete(); + } + else if (BuildNearestRoute( pEnemy->pev->origin, pEnemy->pev->view_ofs, 0, (pEnemy->pev->origin - pev->origin).Length() )) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToEnemy failed!!\n" ); + TaskFail(); + } + break; + } + case TASK_GET_PATH_TO_ENEMY_CORPSE: + { + UTIL_MakeVectors( pev->angles ); + if ( BuildRoute ( m_vecEnemyLKP - gpGlobals->v_forward * 64, bits_MF_TO_LOCATION, NULL ) ) + { + TaskComplete(); + } + else + { + ALERT ( at_aiconsole, "GetPathToEnemyCorpse failed!!\n" ); + TaskFail(); + } + } + break; + case TASK_GET_PATH_TO_SPOT: + { + CBaseEntity *pPlayer = UTIL_FindEntityByClassname( NULL, "player" ); + if ( BuildRoute ( m_vecMoveGoal, bits_MF_TO_LOCATION, pPlayer ) ) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToSpot failed!!\n" ); + TaskFail(); + } + break; + } + + case TASK_GET_PATH_TO_TARGET: + { + RouteClear(); + if ( m_hTargetEnt != NULL && MoveToTarget( m_movementActivity, 1 ) ) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToSpot failed!!\n" ); + TaskFail(); + } + break; + } + case TASK_GET_PATH_TO_SCRIPT: + { + RouteClear(); + if ( m_pCine != NULL && MoveToLocation( m_movementActivity, 1, m_pCine->pev->origin ) ) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToSpot failed!!\n" ); + TaskFail(); + } + break; + } + case TASK_GET_PATH_TO_HINTNODE:// for active idles! + { + if ( MoveToLocation( m_movementActivity, 2, WorldGraph.m_pNodes[ m_iHintNode ].m_vecOrigin ) ) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToHintNode failed!!\n" ); + TaskFail(); + } + break; + } + case TASK_GET_PATH_TO_LASTPOSITION: + { + m_vecMoveGoal = m_vecLastPosition; + + if ( MoveToLocation( m_movementActivity, 2, m_vecMoveGoal ) ) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToLastPosition failed!!\n" ); + TaskFail(); + } + break; + } + case TASK_GET_PATH_TO_BESTSOUND: + { + CSound *pSound; + + pSound = PBestSound(); + + if ( pSound && MoveToLocation( m_movementActivity, 2, pSound->m_vecOrigin ) ) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToBestSound failed!!\n" ); + TaskFail(); + } + break; + } + case TASK_GET_PATH_TO_BESTSCENT: + { + CSound *pScent; + + pScent = PBestScent(); + + if ( pScent && MoveToLocation( m_movementActivity, 2, pScent->m_vecOrigin ) ) + { + TaskComplete(); + } + else + { + // no way to get there =( + ALERT ( at_aiconsole, "GetPathToBestScent failed!!\n" ); + + TaskFail(); + } + break; + } + case TASK_RUN_PATH: + { + // UNDONE: This is in some default AI and some monsters can't run? -- walk instead? + if ( LookupActivity( ACT_RUN ) != ACTIVITY_NOT_AVAILABLE ) + { + m_movementActivity = ACT_RUN; + } + else + { + m_movementActivity = ACT_WALK; + } + TaskComplete(); + break; + } + case TASK_WALK_PATH: + { + if ( pev->movetype == MOVETYPE_FLY ) + { + m_movementActivity = ACT_FLY; + } + if ( LookupActivity( ACT_WALK ) != ACTIVITY_NOT_AVAILABLE ) + { + m_movementActivity = ACT_WALK; + } + else + { + m_movementActivity = ACT_RUN; + } + TaskComplete(); + break; + } + case TASK_STRAFE_PATH: + { + Vector2D vec2DirToPoint; + Vector2D vec2RightSide; + + // to start strafing, we have to first figure out if the target is on the left side or right side + UTIL_MakeVectors ( pev->angles ); + + vec2DirToPoint = ( m_Route[ 0 ].vecLocation - pev->origin ).Make2D().Normalize(); + vec2RightSide = gpGlobals->v_right.Make2D().Normalize(); + + if ( DotProduct ( vec2DirToPoint, vec2RightSide ) > 0 ) + { + // strafe right + m_movementActivity = ACT_STRAFE_RIGHT; + } + else + { + // strafe left + m_movementActivity = ACT_STRAFE_LEFT; + } + TaskComplete(); + break; + } + + + case TASK_WAIT_FOR_MOVEMENT: + { + if (FRouteClear()) + { + TaskComplete(); + } + break; + } + + case TASK_EAT: + { + Eat( pTask->flData ); + TaskComplete(); + break; + } + case TASK_SMALL_FLINCH: + { + m_IdealActivity = GetSmallFlinchActivity(); + break; + } + case TASK_DIE: + { + RouteClear(); + + m_IdealActivity = GetDeathActivity(); + + pev->deadflag = DEAD_DYING; + break; + } + case TASK_SOUND_WAKE: + { + AlertSound(); + TaskComplete(); + break; + } + case TASK_SOUND_DIE: + { + DeathSound(); + TaskComplete(); + break; + } + case TASK_SOUND_IDLE: + { + IdleSound(); + TaskComplete(); + break; + } + case TASK_SOUND_PAIN: + { + PainSound(); + TaskComplete(); + break; + } + case TASK_SOUND_DEATH: + { + DeathSound(); + TaskComplete(); + break; + } + case TASK_SOUND_ANGRY: + { + // sounds are complete as soon as we get here, cause we've already played them. + ALERT ( at_aiconsole, "SOUND\n" ); + TaskComplete(); + break; + } + case TASK_WAIT_FOR_SCRIPT: + { + if ( m_pCine->m_iDelay <= 0 && gpGlobals->time >= m_pCine->m_startTime ) + { + TaskComplete(); //LRC - start playing immediately + } + else if (!m_pCine->IsAction() && m_pCine->m_iszIdle) + { + m_pCine->StartSequence( (CBaseMonster *)this, m_pCine->m_iszIdle, FALSE ); + if (FStrEq( STRING(m_pCine->m_iszIdle), STRING(m_pCine->m_iszPlay))) + { + pev->framerate = 0; + } + } + else + m_IdealActivity = ACT_IDLE; + + break; + } + case TASK_PLAY_SCRIPT: + { + if (m_pCine->IsAction()) + { + //ALERT(at_console,"PlayScript: setting idealactivity %d\n",m_pCine->m_fAction); + switch(m_pCine->m_fAction) + { + case 0: + m_IdealActivity = ACT_RANGE_ATTACK1; break; + case 1: + m_IdealActivity = ACT_RANGE_ATTACK2; break; + case 2: + m_IdealActivity = ACT_MELEE_ATTACK1; break; + case 3: + m_IdealActivity = ACT_MELEE_ATTACK2; break; + case 4: + m_IdealActivity = ACT_SPECIAL_ATTACK1; break; + case 5: + m_IdealActivity = ACT_SPECIAL_ATTACK2; break; + case 6: + m_IdealActivity = ACT_RELOAD; break; + case 7: + m_IdealActivity = ACT_HOP; break; + } + pev->framerate = 1.0; // shouldn't be needed, but just in case + pev->movetype = MOVETYPE_FLY; + ClearBits(pev->flags, FL_ONGROUND); + } + else + { + m_pCine->StartSequence( (CBaseMonster *)this, m_pCine->m_iszPlay, TRUE ); + if ( m_fSequenceFinished ) + ClearSchedule(); + pev->framerate = 1.0; + //ALERT( at_aiconsole, "Script %s has begun for %s\n", STRING( m_pCine->m_iszPlay ), STRING(pev->classname) ); + } + m_scriptState = SCRIPT_PLAYING; + break; + } + case TASK_ENABLE_SCRIPT: + { + m_pCine->DelayStart( 0 ); + TaskComplete(); + break; + } +//LRC + case TASK_END_SCRIPT: + { + m_pCine->SequenceDone( this ); + TaskComplete(); + break; + } + case TASK_PLANT_ON_SCRIPT: + { + if ( m_pCine != NULL ) + { + // Plant on script + // LRC - if it's a teleport script, do the turn too + if (m_pCine->m_fMoveTo == 4 || m_pCine->m_fMoveTo == 6) + { + if (m_pCine->m_fTurnType == 0) //LRC + pev->angles.y = m_hTargetEnt->pev->angles.y; + else if (m_pCine->m_fTurnType == 1) + pev->angles.y = UTIL_VecToYaw(m_hTargetEnt->pev->origin - pev->origin); + pev->ideal_yaw = pev->angles.y; + pev->avelocity = Vector( 0, 0, 0 ); + pev->velocity = Vector( 0, 0, 0 ); + pev->effects |= EF_NOINTERP; + } + + if (m_pCine->m_fMoveTo != 6) + pev->origin = m_pGoalEnt->pev->origin; + } + + TaskComplete(); + break; + } + case TASK_FACE_SCRIPT: + { + if ( m_pCine != NULL && m_pCine->m_fMoveTo != 0) // movetype "no move" makes us ignore turntype + { + switch (m_pCine->m_fTurnType) + { + case 0: + pev->ideal_yaw = UTIL_AngleMod( m_pCine->pev->angles.y ); + break; + case 1: + // yes, this is inconsistent- turn to face uses the "target" and turn to angle uses the "cine". + if (m_hTargetEnt) + MakeIdealYaw ( m_hTargetEnt->pev->origin ); + else + MakeIdealYaw ( m_pCine->pev->origin ); + break; + // default: don't turn + } + } + + TaskComplete(); + m_IdealActivity = ACT_IDLE; + RouteClear(); + break; + } + case TASK_SUGGEST_STATE: + { + m_IdealMonsterState = (MONSTERSTATE)(int)pTask->flData; + TaskComplete(); + break; + } + + case TASK_SET_FAIL_SCHEDULE: + m_failSchedule = (int)pTask->flData; + TaskComplete(); + break; + + case TASK_CLEAR_FAIL_SCHEDULE: + m_failSchedule = SCHED_NONE; + TaskComplete(); + break; + + default: + { + ALERT ( at_aiconsole, "No StartTask entry for %d\n", (SHARED_TASKS)pTask->iTask ); + break; + } + } +} + +//========================================================= +// GetTask - returns a pointer to the current +// scheduled task. NULL if there's a problem. +//========================================================= +Task_t *CBaseMonster :: GetTask ( void ) +{ + if ( m_iScheduleIndex < 0 || m_iScheduleIndex >= m_pSchedule->cTasks ) + { + // m_iScheduleIndex is not within valid range for the monster's current schedule. + return NULL; + } + else + { + return &m_pSchedule->pTasklist[ m_iScheduleIndex ]; + } +} + +//========================================================= +// GetSchedule - Decides which type of schedule best suits +// the monster's current state and conditions. Then calls +// monster's member function to get a pointer to a schedule +// of the proper type. +//========================================================= +Schedule_t *CBaseMonster :: GetSchedule ( void ) +{ + switch ( m_MonsterState ) + { + case MONSTERSTATE_PRONE: + { + return GetScheduleOfType( SCHED_BARNACLE_VICTIM_GRAB ); + break; + } + case MONSTERSTATE_NONE: + { + ALERT ( at_aiconsole, "MONSTERSTATE IS NONE!\n" ); + break; + } + case MONSTERSTATE_IDLE: + { + if ( HasConditions ( bits_COND_HEAR_SOUND ) ) + { + return GetScheduleOfType( SCHED_ALERT_FACE ); + } + else if ( FRouteClear() ) + { + // no valid route! + return GetScheduleOfType( SCHED_IDLE_STAND ); + } + else + { + // valid route. Get moving + return GetScheduleOfType( SCHED_IDLE_WALK ); + } + break; + } + case MONSTERSTATE_ALERT: + { + if ( HasConditions( bits_COND_ENEMY_DEAD ) && LookupActivity( ACT_VICTORY_DANCE ) != ACTIVITY_NOT_AVAILABLE ) + { + return GetScheduleOfType ( SCHED_VICTORY_DANCE ); + } + + if ( HasConditions(bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE) ) + { + if ( fabs( FlYawDiff() ) < (1.0 - m_flFieldOfView) * 60 ) // roughly in the correct direction + { + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_ORIGIN ); + } + else + { + return GetScheduleOfType( SCHED_ALERT_SMALL_FLINCH ); + } + } + + else if ( HasConditions ( bits_COND_HEAR_SOUND ) ) + { + return GetScheduleOfType( SCHED_ALERT_FACE ); + } + else + { + return GetScheduleOfType( SCHED_ALERT_STAND ); + } + break; + } + case MONSTERSTATE_COMBAT: + { + if ( HasConditions( bits_COND_ENEMY_DEAD ) ) + { + // clear the current (dead) enemy and try to find another. + m_hEnemy = NULL; + + if ( GetEnemy() ) + { + ClearConditions( bits_COND_ENEMY_DEAD ); + return GetSchedule(); + } + else + { + SetState( MONSTERSTATE_ALERT ); + return GetSchedule(); + } + } + + if ( HasConditions(bits_COND_NEW_ENEMY) ) + { + return GetScheduleOfType ( SCHED_WAKE_ANGRY ); + } + else if (HasConditions(bits_COND_LIGHT_DAMAGE) && !HasMemory( bits_MEMORY_FLINCHED) ) + { + return GetScheduleOfType( SCHED_SMALL_FLINCH ); + } + else if ( !HasConditions(bits_COND_SEE_ENEMY) ) + { + // we can't see the enemy + if ( !HasConditions(bits_COND_ENEMY_OCCLUDED) ) + { + // enemy is unseen, but not occluded! + // turn to face enemy + return GetScheduleOfType( SCHED_COMBAT_FACE ); + } + else + { + // chase! + return GetScheduleOfType( SCHED_CHASE_ENEMY ); + } + } + else + { + // we can see the enemy + if ( HasConditions(bits_COND_CAN_RANGE_ATTACK1) ) + { + return GetScheduleOfType( SCHED_RANGE_ATTACK1 ); + } + if ( HasConditions(bits_COND_CAN_RANGE_ATTACK2) ) + { + return GetScheduleOfType( SCHED_RANGE_ATTACK2 ); + } + if ( HasConditions(bits_COND_CAN_MELEE_ATTACK1) ) + { + return GetScheduleOfType( SCHED_MELEE_ATTACK1 ); + } + if ( HasConditions(bits_COND_CAN_MELEE_ATTACK2) ) + { + return GetScheduleOfType( SCHED_MELEE_ATTACK2 ); + } + if ( !HasConditions(bits_COND_CAN_RANGE_ATTACK1 | bits_COND_CAN_MELEE_ATTACK1) ) + { + // if we can see enemy but can't use either attack type, we must need to get closer to enemy + return GetScheduleOfType( SCHED_CHASE_ENEMY ); + } + else if ( !FacingIdeal() ) + { + //turn + return GetScheduleOfType( SCHED_COMBAT_FACE ); + } + else + { + ALERT ( at_aiconsole, "No suitable combat schedule!\n" ); + } + } + break; + } + case MONSTERSTATE_DEAD: + { + return GetScheduleOfType( SCHED_DIE ); + break; + } + case MONSTERSTATE_SCRIPT: + { + ASSERT( m_pCine != NULL ); + if ( !m_pCine ) + { + ALERT( at_aiconsole, "Script failed for %s\n", STRING(pev->classname) ); +// ALERT( at_console, "Script failed for %s\n", STRING(pev->classname) ); + CineCleanup(); + return GetScheduleOfType( SCHED_IDLE_STAND ); + } + + return GetScheduleOfType( SCHED_AISCRIPT ); + } + default: + { + ALERT ( at_aiconsole, "Invalid State for GetSchedule!\n" ); + break; + } + } + + return &slError[ 0 ]; +} + +void CBaseMonster :: MakeDamageBloodDecal ( int cCount, float flNoise, TraceResult *ptr, const Vector &vecDir ) +{ + // make blood decal on the wall! + TraceResult Bloodtr; + Vector vecTraceDir; + int i; + + if ( !IsAlive() ) + { + // dealing with a dead monster. + if ( pev->max_health <= 0 ) + { + // no blood decal for a monster that has already decalled its limit. + return; + } + else + { + pev->max_health--; + } + } + + for ( i = 0 ; i < cCount ; i++ ) + { + vecTraceDir = vecDir; + + vecTraceDir.x += RANDOM_FLOAT( -flNoise, flNoise ); + vecTraceDir.y += RANDOM_FLOAT( -flNoise, flNoise ); + vecTraceDir.z += RANDOM_FLOAT( -flNoise, flNoise ); + + UTIL_TraceLine( ptr->vecEndPos, ptr->vecEndPos + vecTraceDir * 172, ignore_monsters, ENT(pev), &Bloodtr); + +/* + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE); + WRITE_COORD( ptr->vecEndPos.x ); + WRITE_COORD( ptr->vecEndPos.y ); + WRITE_COORD( ptr->vecEndPos.z ); + + WRITE_COORD( Bloodtr.vecEndPos.x ); + WRITE_COORD( Bloodtr.vecEndPos.y ); + WRITE_COORD( Bloodtr.vecEndPos.z ); + MESSAGE_END(); +*/ + + if ( Bloodtr.flFraction != 1.0 ) + { + UTIL_BloodDecalTrace( &Bloodtr, BloodColor() ); + } + } +} + +//========================================================= +// TraceAttack +//========================================================= +void CBaseMonster :: TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + if ( pev->takedamage ) + { + m_LastHitGroup = ptr->iHitgroup; + + switch ( ptr->iHitgroup ) + { + case HITGROUP_GENERIC: + break; + case HITGROUP_HEAD: + flDamage *= DMG_HEAD; + break; + case HITGROUP_CHEST: + flDamage *= DMG_CHEST; + break; + case HITGROUP_STOMACH: + flDamage *= DMG_STOMACH; + break; + case HITGROUP_LEFTARM: + case HITGROUP_RIGHTARM: + flDamage *= DMG_ARM; + break; + case HITGROUP_LEFTLEG: + case HITGROUP_RIGHTLEG: + flDamage *= DMG_LEG; + break; + default: + break; + } + + SpawnBlood(ptr->vecEndPos, BloodColor(), flDamage);// a little surface blood. + TraceBleed( flDamage, vecDir, ptr, bitsDamageType ); + AddMultiDamage( pevAttacker, this, flDamage, bitsDamageType ); + } +} + +//========================================================= +// CheckTraceHullAttack - expects a length to trace, amount +// of damage to do, and damage type. Returns a pointer to +// the damaged entity in case the monster wishes to do +// other stuff to the victim (punchangle, etc) +// +// Used for many contact-range melee attacks. Bites, claws, etc. +//========================================================= +CBaseEntity* CBaseMonster :: CheckTraceHullAttack( float flDist, int iDamage, int iDmgType ) +{ + TraceResult tr; + + if (IsPlayer()) + UTIL_MakeVectors( pev->angles ); + else + UTIL_MakeAimVectors( pev->angles ); + + Vector vecStart = pev->origin; + vecStart.z += pev->size.z * 0.5; + Vector vecEnd = vecStart + (gpGlobals->v_forward * flDist ); + + UTIL_TraceHull( vecStart, vecEnd, dont_ignore_monsters, head_hull, ENT(pev), &tr ); + + if ( tr.pHit ) + { + CBaseEntity *pEntity = CBaseEntity::Instance( tr.pHit ); + + if ( iDamage > 0 ) + { + pEntity->TakeDamage( pev, pev, iDamage, iDmgType ); + } + + return pEntity; + } + + return NULL; +} + + +//========================================================= +// FInViewCone - returns true is the passed ent is in +// the caller's forward view cone. The dot product is performed +// in 2d, making the view cone infinitely tall. +//========================================================= +BOOL CBaseMonster :: FInViewCone ( CBaseEntity *pEntity ) +{ + Vector2D vec2LOS; + float flDot; + + UTIL_MakeVectors ( pev->angles ); + + vec2LOS = ( pEntity->pev->origin - pev->origin ).Make2D(); + vec2LOS = vec2LOS.Normalize(); + + flDot = DotProduct (vec2LOS , gpGlobals->v_forward.Make2D() ); + + if ( flDot > m_flFieldOfView ) + { + return TRUE; + } + else + { + return FALSE; + } +} + +//========================================================= +// FInViewCone - returns true is the passed vector is in +// the caller's forward view cone. The dot product is performed +// in 2d, making the view cone infinitely tall. +//========================================================= +BOOL CBaseMonster :: FInViewCone ( Vector *pOrigin ) +{ + Vector2D vec2LOS; + float flDot; + + UTIL_MakeVectors ( pev->angles ); + + vec2LOS = ( *pOrigin - pev->origin ).Make2D(); + vec2LOS = vec2LOS.Normalize(); + + flDot = DotProduct (vec2LOS , gpGlobals->v_forward.Make2D() ); + + if ( flDot > m_flFieldOfView ) + return TRUE; + return FALSE; +} + +void CBaseMonster :: RadiusDamage(entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int iClassIgnore, int bitsDamageType ) +{ + ::RadiusDamage( pev->origin, pevInflictor, pevAttacker, flDamage, flDamage * 2.5, iClassIgnore, bitsDamageType ); +} + + +void CBaseMonster :: RadiusDamage( Vector vecSrc, entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int iClassIgnore, int bitsDamageType ) +{ + ::RadiusDamage( vecSrc, pevInflictor, pevAttacker, flDamage, flDamage * 2.5, iClassIgnore, bitsDamageType ); +} + +/* +============ +TakeDamage + +The damage is coming from inflictor, but get mad at attacker +This should be the only function that ever reduces health. +bitsDamageType indicates the type of damage sustained, ie: DMG_SHOCK + +Time-based damage: only occurs while the monster is within the trigger_hurt. +When a monster is poisoned via an arrow etc it takes all the poison damage at once. +============ +*/ +int CBaseMonster :: TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + float flTake; + Vector vecDir; + + if (!pev->takedamage) + return 0; + + if ( !IsAlive() ) + { + return DeadTakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType ); + } + + if ( pev->deadflag == DEAD_NO ) + { + // no pain sound during death animation. + PainSound();// "Ouch!" + } + + //!!!LATER - make armor consideration here! + flTake = flDamage; + + // set damage type sustained + m_bitsDamageType |= bitsDamageType; + + // grab the vector of the incoming attack. ( pretend that the inflictor is a little lower than it really is, so the body will tend to fly upward a bit). + vecDir = Vector( 0, 0, 0 ); + if (!FNullEnt( pevInflictor )) + { + CBaseEntity *pInflictor = CBaseEntity :: Instance( pevInflictor ); + if (pInflictor) + { + vecDir = ( pInflictor->Center() - Vector ( 0, 0, 10 ) - Center() ).Normalize(); + vecDir = g_vecAttackDir = vecDir.Normalize(); + } + } + + // add to the damage total for clients, which will be sent as a single + // message at the end of the frame + // todo: remove after combining shotgun blasts? + if ( IsPlayer() ) + { + if ( pevInflictor ) + pev->dmg_inflictor = ENT(pevInflictor); + + pev->dmg_take += flTake; + + // check for godmode or invincibility + if ( pev->flags & FL_GODMODE ) + { + return 0; + } + } + + // if this is a player, move him around! + if ( ( !FNullEnt( pevInflictor ) ) && (pev->movetype == MOVETYPE_WALK) && (!pevAttacker || pevAttacker->solid != SOLID_TRIGGER) ) + { + pev->velocity = pev->velocity + vecDir * -DamageForce( flDamage ); + } + + // do the damage + pev->health -= flTake; + + + // HACKHACK Don't kill monsters in a script. Let them break their scripts first + if ( m_MonsterState == MONSTERSTATE_SCRIPT ) + { + SetConditions( bits_COND_LIGHT_DAMAGE ); + return 0; + } + + if ( pev->health <= 0 ) + { + g_pevLastInflictor = pevInflictor; + + if ( bitsDamageType & DMG_ALWAYSGIB ) + { + Killed( pevAttacker, GIB_ALWAYS ); + } + else if ( bitsDamageType & DMG_NEVERGIB ) + { + Killed( pevAttacker, GIB_NEVER ); + } + else + { + Killed( pevAttacker, GIB_NORMAL ); + } + + g_pevLastInflictor = NULL; + + return 0; + } + + // react to the damage (get mad) + if ( (pev->flags & FL_MONSTER) && !FNullEnt(pevAttacker) ) + { + //LRC - new behaviours, for m_iPlayerReact. + if (pevAttacker->flags & FL_CLIENT) + { + if (m_iPlayerReact == 2) + { + // just get angry. + Remember( bits_MEMORY_PROVOKED ); + } + else if (m_iPlayerReact == 3) + { + // try to decide whether it was deliberate... if I have an enemy, assume it was just crossfire. + if ( m_hEnemy == NULL ) + { + if ( (m_afMemory & bits_MEMORY_SUSPICIOUS) || UTIL_IsFacing( pevAttacker, pev->origin ) ) + Remember( bits_MEMORY_PROVOKED ); + else + Remember( bits_MEMORY_SUSPICIOUS ); + } + } + } + + if ( pevAttacker->flags & (FL_MONSTER | FL_CLIENT) ) + {// only if the attack was a monster or client! + + // enemy's last known position is somewhere down the vector that the attack came from. + if (pevInflictor) + { + if (m_hEnemy == NULL || pevInflictor == m_hEnemy->pev || !HasConditions(bits_COND_SEE_ENEMY)) + { + m_vecEnemyLKP = pevInflictor->origin; + } + } + else + { + m_vecEnemyLKP = pev->origin + ( g_vecAttackDir * 64 ); + } + + MakeIdealYaw( m_vecEnemyLKP ); + + // add pain to the conditions + // !!!HACKHACK - fudged for now. Do we want to have a virtual function to determine what is light and + // heavy damage per monster class? + if ( flDamage > 0 ) + { + SetConditions(bits_COND_LIGHT_DAMAGE); + } + + if ( flDamage >= 20 ) + { + SetConditions(bits_COND_HEAVY_DAMAGE); + } + } + } + + return 1; +} + +//========================================================= +// DeadTakeDamage - takedamage function called when a monster's +// corpse is damaged. +//========================================================= +int CBaseMonster :: DeadTakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + Vector vecDir; + + // grab the vector of the incoming attack. ( pretend that the inflictor is a little lower than it really is, so the body will tend to fly upward a bit). + vecDir = Vector( 0, 0, 0 ); + if (!FNullEnt( pevInflictor )) + { + CBaseEntity *pInflictor = CBaseEntity :: Instance( pevInflictor ); + if (pInflictor) + { + vecDir = ( pInflictor->Center() - Vector ( 0, 0, 10 ) - Center() ).Normalize(); + vecDir = g_vecAttackDir = vecDir.Normalize(); + } + } + +#if 0// turn this back on when the bounding box issues are resolved. + + pev->flags &= ~FL_ONGROUND; + pev->origin.z += 1; + + // let the damage scoot the corpse around a bit. + if ( !FNullEnt(pevInflictor) && (pevAttacker->solid != SOLID_TRIGGER) ) + { + pev->velocity = pev->velocity + vecDir * -DamageForce( flDamage ); + } + +#endif + + // kill the corpse if enough damage was done to destroy the corpse and the damage is of a type that is allowed to destroy the corpse. + if ( bitsDamageType & DMG_GIB_CORPSE ) + { + if ( pev->health <= flDamage ) + { + pev->health = -50; + Killed( pevAttacker, GIB_ALWAYS ); + return 0; + } + // Accumulate corpse gibbing damage, so you can gib with multiple hits + pev->health -= flDamage * 0.1; + } + + return 1; +} + + +float CBaseMonster :: DamageForce( float damage ) +{ + float force = damage * ((32 * 32 * 72.0) / (pev->size.x * pev->size.y * pev->size.z)) * 5; + + if ( force > 1000.0) + { + force = 1000.0; + } + + return force; +} + +// take health +int CBaseMonster :: TakeHealth (float flHealth, int bitsDamageType) +{ + if (!pev->takedamage) + return 0; + + // clear out any damage types we healed. + // UNDONE: generic health should not heal any + // UNDONE: time-based damage + + m_bitsDamageType &= ~(bitsDamageType & ~DMG_TIMEBASED); + return CBaseEntity::TakeHealth(flHealth, bitsDamageType); +} + +int CBaseMonster :: TakeArmor (float flArmor, int suit) +{ + if (!pev->takedamage) return 0; + return CBaseEntity::TakeArmor(flArmor, suit); +} + +BOOL CBaseMonster :: HasHumanGibs( void ) +{ + int myClass = Classify(); + + // these types of monster don't use gibs + if ( myClass == CLASS_NONE || myClass == CLASS_MACHINE || myClass == CLASS_PLAYER_BIOWEAPON && myClass == CLASS_ALIEN_BIOWEAPON) + { + return FALSE; + } + else + { + return (this->m_bloodColor == BLOOD_COLOR_RED); + } +} + + +BOOL CBaseMonster :: HasAlienGibs( void ) +{ + int myClass = Classify(); + + // these types of monster don't use gibs + if ( myClass == CLASS_NONE || myClass == CLASS_MACHINE || myClass == CLASS_PLAYER_BIOWEAPON && myClass == CLASS_ALIEN_BIOWEAPON) + { + return FALSE; + } + else + { + return (this->m_bloodColor == BLOOD_COLOR_GREEN); + } +} + + +//========================================================= +// GibMonster - create some gore and get rid of a monster's +// model. +//========================================================= +void CBaseMonster :: GibMonster( void ) +{ + TraceResult tr; + BOOL gibbed = FALSE; + int iszCustomGibs; + + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "common/bodysplat.wav", 1, ATTN_NORM); + + if ( iszCustomGibs = HasCustomGibs() ) + { + CGib::SpawnHeadGib( this, iszCustomGibs ); + CGib::SpawnRandomGibs( this, 4, 1, iszCustomGibs ); + CGib::SpawnStickyGibs (this, 4, iszCustomGibs ); + gibbed = TRUE; + } + else if ( HasHumanGibs() ) + { + CGib::SpawnHeadGib( this ); + CGib::SpawnRandomGibs( this, 4, 1 ); // throw some human gibs. + CGib::SpawnStickyGibs (this, 4 ); + gibbed = TRUE; + } + else if ( HasAlienGibs() ) + { + CGib::SpawnRandomGibs( this, 4, 0 ); // Throw alien gibs + gibbed = TRUE; + } + if ( !IsPlayer() && gibbed ) UTIL_Remove (this); + else + { + StopAnimation(); + pev->velocity = g_vecZero; + pev->movetype = MOVETYPE_NONE; + pev->avelocity = g_vecZero; + pev->animtime = gpGlobals->time; + pev->effects |= EF_NOINTERP; + } +} + +//========================================================= +// GetDeathActivity - determines the best type of death +// anim to play. +//========================================================= +Activity CBaseMonster :: GetDeathActivity ( void ) +{ + Activity deathActivity; + BOOL fTriedDirection; + float flDot; + TraceResult tr; + Vector vecSrc; + + if ( pev->deadflag != DEAD_NO ) + { + // don't run this while dying. + return m_IdealActivity; + } + + vecSrc = Center(); + + fTriedDirection = FALSE; + deathActivity = ACT_DIESIMPLE;// in case we can't find any special deaths to do. + + UTIL_MakeVectors ( pev->angles ); + flDot = DotProduct ( gpGlobals->v_forward, g_vecAttackDir * -1 ); + + switch ( m_LastHitGroup ) + { + // try to pick a region-specific death. + case HITGROUP_HEAD: + deathActivity = ACT_DIE_HEADSHOT; + break; + + case HITGROUP_STOMACH: + deathActivity = ACT_DIE_GUTSHOT; + break; + + case HITGROUP_GENERIC: + // try to pick a death based on attack direction + fTriedDirection = TRUE; + + if ( flDot > 0.3 ) + { + deathActivity = ACT_DIEFORWARD; + } + else if ( flDot <= -0.3 ) + { + deathActivity = ACT_DIEBACKWARD; + } + break; + + default: + // try to pick a death based on attack direction + fTriedDirection = TRUE; + + if ( flDot > 0.3 ) + { + deathActivity = ACT_DIEFORWARD; + } + else if ( flDot <= -0.3 ) + { + deathActivity = ACT_DIEBACKWARD; + } + break; + } + + + // can we perform the prescribed death? + if ( LookupActivity ( deathActivity ) == ACTIVITY_NOT_AVAILABLE ) + { + // no! did we fail to perform a directional death? + if ( fTriedDirection ) + { + // if yes, we're out of options. Go simple. + deathActivity = ACT_DIESIMPLE; + } + else + { + // cannot perform the ideal region-specific death, so try a direction. + if ( flDot > 0.3 ) + { + deathActivity = ACT_DIEFORWARD; + } + else if ( flDot <= -0.3 ) + { + deathActivity = ACT_DIEBACKWARD; + } + } + } + + if ( LookupActivity ( deathActivity ) == ACTIVITY_NOT_AVAILABLE ) + { + // if we're still invalid, simple is our only option. + deathActivity = ACT_DIESIMPLE; + } + + if ( deathActivity == ACT_DIEFORWARD ) + { + // make sure there's room to fall forward + UTIL_TraceHull ( vecSrc, vecSrc + gpGlobals->v_forward * 64, dont_ignore_monsters, head_hull, edict(), &tr ); + + if ( tr.flFraction != 1.0 ) + { + deathActivity = ACT_DIESIMPLE; + } + } + + if ( deathActivity == ACT_DIEBACKWARD ) + { + // make sure there's room to fall backward + UTIL_TraceHull ( vecSrc, vecSrc - gpGlobals->v_forward * 64, dont_ignore_monsters, head_hull, edict(), &tr ); + + if ( tr.flFraction != 1.0 ) + { + deathActivity = ACT_DIESIMPLE; + } + } + + return deathActivity; +} + +//========================================================= +// GetSmallFlinchActivity - determines the best type of flinch +// anim to play. +//========================================================= +Activity CBaseMonster :: GetSmallFlinchActivity ( void ) +{ + Activity flinchActivity; + BOOL fTriedDirection; + float flDot; + + fTriedDirection = FALSE; + UTIL_MakeVectors ( pev->angles ); + flDot = DotProduct ( gpGlobals->v_forward, g_vecAttackDir * -1 ); + + switch ( m_LastHitGroup ) + { + // pick a region-specific flinch + case HITGROUP_HEAD: + flinchActivity = ACT_FLINCH_HEAD; + break; + case HITGROUP_STOMACH: + flinchActivity = ACT_FLINCH_STOMACH; + break; + case HITGROUP_LEFTARM: + flinchActivity = ACT_FLINCH_LEFTARM; + break; + case HITGROUP_RIGHTARM: + flinchActivity = ACT_FLINCH_RIGHTARM; + break; + case HITGROUP_LEFTLEG: + flinchActivity = ACT_FLINCH_LEFTLEG; + break; + case HITGROUP_RIGHTLEG: + flinchActivity = ACT_FLINCH_RIGHTLEG; + break; + case HITGROUP_GENERIC: + default: + // just get a generic flinch. + flinchActivity = ACT_SMALL_FLINCH; + break; + } + + + // do we have a sequence for the ideal activity? + if ( LookupActivity ( flinchActivity ) == ACTIVITY_NOT_AVAILABLE ) + { + flinchActivity = ACT_SMALL_FLINCH; + } + + return flinchActivity; +} + + +void CBaseMonster::BecomeDead( void ) +{ + pev->takedamage = DAMAGE_YES;// don't let autoaim aim at corpses. + + // give the corpse half of the monster's original maximum health. + pev->health = pev->max_health / 2; + pev->max_health = 5; // max_health now becomes a counter for how many blood decals the corpse can place. + + // make the corpse fly away from the attack vector + pev->movetype = MOVETYPE_TOSS; + //pev->flags &= ~FL_ONGROUND; + //pev->origin.z += 2; + //pev->velocity = g_vecAttackDir * -1; + //pev->velocity = pev->velocity * RANDOM_FLOAT( 300, 400 ); +} + + +BOOL CBaseMonster::ShouldGibMonster( int iGib ) +{ + if ( ( iGib == GIB_NORMAL && pev->health < -30 ) || ( iGib == GIB_ALWAYS ) ) + return TRUE; + + return FALSE; +} + + +void CBaseMonster::CallGibMonster( void ) +{ + pev->takedamage = DAMAGE_NO; + pev->solid = SOLID_NOT;// do something with the body. while monster blows up + + StopAnimation(); + pev->velocity = g_vecZero; + pev->movetype = MOVETYPE_NONE; + pev->avelocity = g_vecZero; + pev->animtime = gpGlobals->time; + pev->effects |= EF_NOINTERP; + + pev->effects = EF_NODRAW; // make the model invisible. + GibMonster(); + + pev->deadflag = DEAD_DEAD; + FCheckAITrigger(); + + // don't let the status bar glitch for players.with <0 health. + if (pev->health < -99) pev->health = 0; +} + + +/* +============ +Killed +============ +*/ +void CBaseMonster :: Killed( entvars_t *pevAttacker, int iGib ) +{ + if ( HasMemory( bits_MEMORY_KILLED ) ) + { + if ( ShouldGibMonster( iGib ) ) + CallGibMonster(); + return; + } + + Remember( bits_MEMORY_KILLED ); + + // clear the deceased's sound channels.(may have been firing or reloading when killed) + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "common/null.wav", 1, ATTN_NORM); + m_IdealMonsterState = MONSTERSTATE_DEAD; + // Make sure this condition is fired too (TakeDamage breaks out before this happens on death) + SetConditions( bits_COND_LIGHT_DAMAGE ); + + // tell owner ( if any ) that we're dead.This is mostly for MonsterMaker functionality. + CBaseEntity *pOwner = CBaseEntity::Instance(pev->owner); + if ( pOwner ) + { + pOwner->DeathNotice( pev ); + } + if ( ShouldGibMonster( iGib ) ) + { + CallGibMonster(); + return; + } + else if ( pev->flags & FL_MONSTER ) + { + SetTouch( NULL ); + BecomeDead(); + } + + // don't let the status bar glitch for players.with <0 health. + if (pev->health < -99) pev->health = 0; + m_IdealMonsterState = MONSTERSTATE_DEAD; +} + +//========================================================= +// FBoxVisible - a more accurate ( and slower ) version +// of FVisible. +// +// !!!UNDONE - make this CBaseMonster? +//========================================================= +BOOL FBoxVisible ( entvars_t *pevLooker, entvars_t *pevTarget, Vector &vecTargetOrigin, float flSize ) +{ + // don't look through water + if ((pevLooker->waterlevel != 3 && pevTarget->waterlevel == 3) + || (pevLooker->waterlevel == 3 && pevTarget->waterlevel == 0)) + return FALSE; + + TraceResult tr; + Vector vecLookerOrigin = pevLooker->origin + pevLooker->view_ofs;//look through the monster's 'eyes' + for (int i = 0; i < 5; i++) + { + Vector vecTarget = pevTarget->origin; + vecTarget.x += RANDOM_FLOAT( pevTarget->mins.x + flSize, pevTarget->maxs.x - flSize); + vecTarget.y += RANDOM_FLOAT( pevTarget->mins.y + flSize, pevTarget->maxs.y - flSize); + vecTarget.z += RANDOM_FLOAT( pevTarget->mins.z + flSize, pevTarget->maxs.z - flSize); + + UTIL_TraceLine(vecLookerOrigin, vecTarget, ignore_monsters, ignore_glass, ENT(pevLooker)/*pentIgnore*/, &tr); + + if (tr.flFraction == 1.0) + { + vecTargetOrigin = vecTarget; + return TRUE;// line of sight is valid. + } + } + return FALSE;// Line of sight is not established +} + +// +// VecCheckToss - returns the velocity at which an object should be lobbed from vecspot1 to land near vecspot2. +// returns g_vecZero if toss is not feasible. +// +Vector VecCheckToss ( entvars_t *pev, const Vector &vecSpot1, Vector vecSpot2, float flGravityAdj ) +{ + TraceResult tr; + Vector vecMidPoint;// halfway point between Spot1 and Spot2 + Vector vecApex;// highest point + Vector vecScale; + Vector vecGrenadeVel; + Vector vecTemp; + float flGravity = CVAR_GET_FLOAT( "sv_gravity" ) * flGravityAdj; + + if (vecSpot2.z - vecSpot1.z > 500) + { + // to high, fail + return g_vecZero; + } + + UTIL_MakeVectors (pev->angles); + + // toss a little bit to the left or right, not right down on the enemy's bean (head). + vecSpot2 = vecSpot2 + gpGlobals->v_right * ( RANDOM_FLOAT(-8,8) + RANDOM_FLOAT(-16,16) ); + vecSpot2 = vecSpot2 + gpGlobals->v_forward * ( RANDOM_FLOAT(-8,8) + RANDOM_FLOAT(-16,16) ); + + // calculate the midpoint and apex of the 'triangle' + // UNDONE: normalize any Z position differences between spot1 and spot2 so that triangle is always RIGHT + + // How much time does it take to get there? + + // get a rough idea of how high it can be thrown + vecMidPoint = vecSpot1 + (vecSpot2 - vecSpot1) * 0.5; + UTIL_TraceLine(vecMidPoint, vecMidPoint + Vector(0,0,500), ignore_monsters, ENT(pev), &tr); + vecMidPoint = tr.vecEndPos; + // (subtract 15 so the grenade doesn't hit the ceiling) + vecMidPoint.z -= 15; + + if (vecMidPoint.z < vecSpot1.z || vecMidPoint.z < vecSpot2.z) + { + // to not enough space, fail + return g_vecZero; + } + + // How high should the grenade travel to reach the apex + float distance1 = (vecMidPoint.z - vecSpot1.z); + float distance2 = (vecMidPoint.z - vecSpot2.z); + + // How long will it take for the grenade to travel this distance + float time1 = sqrt( distance1 / (0.5 * flGravity) ); + float time2 = sqrt( distance2 / (0.5 * flGravity) ); + + if (time1 < 0.1) + { + // too close + return g_vecZero; + } + + // how hard to throw sideways to get there in time. + vecGrenadeVel = (vecSpot2 - vecSpot1) / (time1 + time2); + // how hard upwards to reach the apex at the right time. + vecGrenadeVel.z = flGravity * time1; + + // find the apex + vecApex = vecSpot1 + vecGrenadeVel * time1; + vecApex.z = vecMidPoint.z; + + UTIL_TraceLine(vecSpot1, vecApex, dont_ignore_monsters, ENT(pev), &tr); + if (tr.flFraction != 1.0) + { + // fail! + return g_vecZero; + } + + // UNDONE: either ignore monsters or change it to not care if we hit our enemy + UTIL_TraceLine(vecSpot2, vecApex, ignore_monsters, ENT(pev), &tr); + if (tr.flFraction != 1.0) + { + // fail! + return g_vecZero; + } + + return vecGrenadeVel; +} + + +// +// VecCheckThrow - returns the velocity vector at which an object should be thrown from vecspot1 to hit vecspot2. +// returns g_vecZero if throw is not feasible. +// +Vector VecCheckThrow ( entvars_t *pev, const Vector &vecSpot1, Vector vecSpot2, float flSpeed, float flGravityAdj ) +{ + float flGravity = CVAR_GET_FLOAT( "sv_gravity" ) * flGravityAdj; + + Vector vecGrenadeVel = (vecSpot2 - vecSpot1); + + // throw at a constant time + float time = vecGrenadeVel.Length( ) / flSpeed; + vecGrenadeVel = vecGrenadeVel * (1.0 / time); + + // adjust upward toss to compensate for gravity loss + vecGrenadeVel.z += flGravity * time * 0.5; + + Vector vecApex = vecSpot1 + (vecSpot2 - vecSpot1) * 0.5; + vecApex.z += 0.5 * flGravity * (time * 0.5) * (time * 0.5); + + TraceResult tr; + UTIL_TraceLine(vecSpot1, vecApex, dont_ignore_monsters, ENT(pev), &tr); + if (tr.flFraction != 1.0) + { + // fail! + return g_vecZero; + } + + UTIL_TraceLine(vecSpot2, vecApex, ignore_monsters, ENT(pev), &tr); + if (tr.flFraction != 1.0) + { + // fail! + return g_vecZero; + } + + return vecGrenadeVel; +} + + + +//========================================================= +// DropItem - dead monster drops named item +//========================================================= +CBaseEntity* CBaseMonster :: DropItem ( char *pszItemName, const Vector &vecPos, const Vector &vecAng ) +{ + if ( !pszItemName ) + { + ALERT ( at_debug, "DropItem() - No item name!\n" ); + return NULL; + } + + CBaseEntity *pItem = CBaseEntity::Create( pszItemName, vecPos, vecAng, edict() ); + + if ( pItem ) + { + // do we want this behavior to be default?! (sjb) + pItem->pev->velocity = pev->velocity; + pItem->pev->avelocity = Vector ( 0, RANDOM_FLOAT( 0, 100 ), 0 ); + return pItem; + } + else + { + ALERT ( at_debug, "DropItem() - Didn't create!\n" ); + return FALSE; + } + +} + +//LRC - an entity for monsters to shoot at. +#define SF_MONSTERTARGET_OFF 1 +class CMonsterTarget : public CBaseEntity +{ +public: + void Spawn( void ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + int Classify( void ) { return pev->frags; }; + STATE GetState( void ) + { + return pev->health?STATE_ON:STATE_OFF; + }; +}; +LINK_ENTITY_TO_CLASS( monster_target, CMonsterTarget ); + +void CMonsterTarget :: Spawn ( void ) +{ + if (pev->spawnflags & SF_MONSTERTARGET_OFF) + pev->health = 0; + else pev->health = 1; // Don't ignore me, I'm not dead. I'm quite well really. I think I'll go for a walk... + SetBits (pev->flags, FL_MONSTER); +} + +void CMonsterTarget::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(pev->health) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON)pev->health = 1; + else if(useType == USE_OFF)pev->health = 0; +} \ No newline at end of file diff --git a/server/monsters/basemonster.h b/server/monsters/basemonster.h new file mode 100644 index 00000000..33b18cda --- /dev/null +++ b/server/monsters/basemonster.h @@ -0,0 +1,352 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ + +#ifndef BASEMONSTER_H +#define BASEMONSTER_H + +#define ROUTE_SIZE 8 // how many waypoints a monster can store at one time +#define MAX_OLD_ENEMIES 4 // how many old enemies to remember + +// +// generic Monster +// +class CBaseMonster : public CBaseAnimating +{ +private: + int m_afConditions; + +public: + typedef enum + { + SCRIPT_PLAYING = 0, // Playing the sequence + SCRIPT_WAIT, // Waiting on everyone in the script to be ready + SCRIPT_CLEANUP, // Cancelling the script / cleaning up + SCRIPT_WALK_TO_MARK, + SCRIPT_RUN_TO_MARK, + } SCRIPTSTATE; + + + + // these fields have been added in the process of reworking the state machine. (sjb) + EHANDLE m_hEnemy; // the entity that the monster is fighting. + EHANDLE m_hTargetEnt; // the entity that the monster is trying to reach + EHANDLE m_hOldEnemy[ MAX_OLD_ENEMIES ]; + Vector m_vecOldEnemy[ MAX_OLD_ENEMIES ]; + + float m_flFieldOfView;// width of monster's field of view ( dot product ) + float m_flWaitFinished;// if we're told to wait, this is the time that the wait will be over. + float m_flMoveWaitFinished; + + Activity m_Activity;// what the monster is doing (animation) + Activity m_IdealActivity;// monster should switch to this activity + + int m_LastHitGroup; // the last body region that took damage + + MONSTERSTATE m_MonsterState;// monster's current state + MONSTERSTATE m_IdealMonsterState;// monster should change to this state + + int m_iTaskStatus; + Schedule_t *m_pSchedule; + int m_iScheduleIndex; + + WayPoint_t m_Route[ ROUTE_SIZE ]; // Positions of movement + int m_movementGoal; // Goal that defines route + int m_iRouteIndex; // index into m_Route[] + float m_moveWaitTime; // How long I should wait for something to move + + Vector m_vecMoveGoal; // kept around for node graph moves, so we know our ultimate goal + Activity m_movementActivity; // When moving, set this activity + + int m_iAudibleList; // first index of a linked list of sounds that the monster can hear. + int m_afSoundTypes; + + Vector m_vecLastPosition;// monster sometimes wants to return to where it started after an operation. + + int m_iHintNode; // this is the hint node that the monster is moving towards or performing active idle on. + + int m_afMemory; + + int m_iMaxHealth;// keeps track of monster's maximum health value (for re-healing, etc) + + Vector m_vecEnemyLKP;// last known position of enemy. (enemy's origin) + + int m_cAmmoLoaded; // how much ammo is in the weapon (used to trigger reload anim sequences) + + int m_afCapability;// tells us what a monster can/can't do. + + float m_flNextAttack; // cannot attack again until this time + + int m_bitsDamageType; // what types of damage has monster (player) taken + BYTE m_rgbTimeBasedDamage[CDMG_TIMEBASED]; + + int m_lastDamageAmount;// how much damage did monster (player) last take + // time based damage counters, decr. 1 per 2 seconds + int m_bloodColor; // color of blood particless + + int m_failSchedule; // Schedule type to choose if current schedule fails + + float m_flHungryTime;// set this is a future time to stop the monster from eating for a while. + + float m_flDistTooFar; // if enemy farther away than this, bits_COND_ENEMY_TOOFAR set in CheckEnemy + float m_flDistLook; // distance monster sees (Default 2048) + + int m_iTriggerCondition;// for scripted AI, this is the condition that will cause the activation of the monster's TriggerTarget + string_t m_iszTriggerTarget;// name of target that should be fired. + + Vector m_HackedGunPos; // HACK until we can query end of gun + +// Scripted sequence Info + SCRIPTSTATE m_scriptState; // internal cinematic state + CCineMonster *m_pCine; + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + virtual STATE GetState( void ) { return (pev->deadflag == DEAD_DEAD)?STATE_OFF:STATE_ON; }; + + static TYPEDESCRIPTION m_SaveData[]; + + void KeyValue( KeyValueData *pkvd ); + +// monster use function + void EXPORT MonsterUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT CorpseUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + +// overrideable Monster member functions + + // LRC- to allow level-designers to change monster allegiances + int m_iClass; + int m_iPlayerReact; + virtual int Classify( void ) { return m_iClass?m_iClass:CLASS_NONE; } + + virtual int BloodColor( void ) { return m_bloodColor; } + + virtual CBaseMonster *MyMonsterPointer( void ) { return this; } + virtual void Look ( int iDistance );// basic sight function for monsters + virtual void RunAI ( void );// core ai function! + void Listen ( void ); + + virtual BOOL IsAlive( void ) { return (pev->deadflag != DEAD_DEAD); } + +// Basic Monster AI functions + virtual float ChangeYaw ( int speed ); + float VecToYaw( Vector vecDir ); + float FlYawDiff ( void ); + + float DamageForce( float damage ); + +// stuff written for new state machine + virtual void MonsterThink( void ); + void EXPORT CallMonsterThink( void ) { this->MonsterThink(); } + virtual int IRelationship ( CBaseEntity *pTarget ); + virtual void MonsterInit ( void ); + virtual void MonsterInitDead( void ); // Call after animation/pose is set up + virtual void BecomeDead( void ); + void EXPORT CorpseFallThink( void ); + + void EXPORT MonsterInitThink ( void ); + virtual void StartMonster ( void ); + virtual CBaseEntity* BestVisibleEnemy ( void );// finds best visible enemy for attack + virtual BOOL FInViewCone ( CBaseEntity *pEntity );// see if pEntity is in monster's view cone + virtual BOOL FInViewCone ( Vector *pOrigin );// see if given location is in monster's view cone + virtual void HandleAnimEvent( MonsterEvent_t *pEvent ); + + virtual int CheckLocalMove ( const Vector &vecStart, const Vector &vecEnd, CBaseEntity *pTarget, float *pflDist );// check validity of a straight move through space + virtual void Move( float flInterval = 0.1 ); + virtual void MoveExecute( CBaseEntity *pTargetEnt, const Vector &vecDir, float flInterval ); + virtual BOOL ShouldAdvanceRoute( float flWaypointDist ); + + virtual Activity GetStoppedActivity( void ) { return ACT_IDLE; } + virtual void Stop( void ) { m_IdealActivity = GetStoppedActivity(); } + + // This will stop animation until you call ResetSequenceInfo() at some point in the future + inline void StopAnimation( void ) { pev->framerate = 0; } + + // these functions will survey conditions and set appropriate conditions bits for attack types. + virtual BOOL CheckRangeAttack1( float flDot, float flDist ); + virtual BOOL CheckRangeAttack2( float flDot, float flDist ); + virtual BOOL CheckMeleeAttack1( float flDot, float flDist ); + virtual BOOL CheckMeleeAttack2( float flDot, float flDist ); + + BOOL FHaveSchedule( void ); + BOOL FScheduleValid ( void ); + void ClearSchedule( void ); + BOOL FScheduleDone ( void ); + void ChangeSchedule ( Schedule_t *pNewSchedule ); + void NextScheduledTask ( void ); + Schedule_t *ScheduleInList( const char *pName, Schedule_t **pList, int listCount ); + + virtual Schedule_t *ScheduleFromName( const char *pName ); + static Schedule_t *m_scheduleList[]; + + void MaintainSchedule ( void ); + virtual void StartTask ( Task_t *pTask ); + virtual void RunTask ( Task_t *pTask ); + virtual Schedule_t *GetScheduleOfType( int Type ); + virtual Schedule_t *GetSchedule( void ); + virtual void ScheduleChange( void ) {} +// virtual int CanPlaySequence( void ) { return ((m_pCine == NULL) && (m_MonsterState == MONSTERSTATE_NONE || m_MonsterState == MONSTERSTATE_IDLE || m_IdealMonsterState == MONSTERSTATE_IDLE)); } + virtual int CanPlaySequence( int interruptFlags ); +// virtual int CanPlaySequence( BOOL fDisregardState, int interruptLevel ); + virtual int CanPlaySentence( BOOL fDisregardState ) { return IsAlive(); } + virtual void PlaySentence( const char *pszSentence, float duration, float volume, float attenuation ); + virtual void PlayScriptedSentence( const char *pszSentence, float duration, float volume, float attenuation, BOOL bConcurrent, CBaseEntity *pListener ); + + virtual void SentenceStop( void ); + + Task_t *GetTask ( void ); + virtual MONSTERSTATE GetIdealState ( void ); + virtual void SetActivity ( Activity NewActivity ); + void SetSequenceByName ( char *szSequence ); + void SetState ( MONSTERSTATE State ); + virtual void ReportAIState( void ); + + void CheckAttacks ( CBaseEntity *pTarget, float flDist ); + virtual int CheckEnemy ( CBaseEntity *pEnemy ); + void PushEnemy( CBaseEntity *pEnemy, Vector &vecLastKnownPos ); + BOOL PopEnemy( void ); + + BOOL FGetNodeRoute ( Vector vecDest ); + + inline void TaskComplete( void ) { if ( !HasConditions(bits_COND_TASK_FAILED) ) m_iTaskStatus = TASKSTATUS_COMPLETE; } + void MovementComplete( void ); + inline void TaskFail( void ) { SetConditions(bits_COND_TASK_FAILED); } + inline void TaskBegin( void ) { m_iTaskStatus = TASKSTATUS_RUNNING; } + int TaskIsRunning( void ); + inline int TaskIsComplete( void ) { return (m_iTaskStatus == TASKSTATUS_COMPLETE); } + inline int MovementIsComplete( void ) { return (m_movementGoal == MOVEGOAL_NONE); } + + int IScheduleFlags ( void ); + BOOL FRefreshRoute( void ); + BOOL FRouteClear ( void ); + void RouteSimplify( CBaseEntity *pTargetEnt ); + void AdvanceRoute ( float distance ); + virtual BOOL FTriangulate ( const Vector &vecStart , const Vector &vecEnd, float flDist, CBaseEntity *pTargetEnt, Vector *pApex ); + void MakeIdealYaw( Vector vecTarget ); + virtual void SetYawSpeed ( void ) { return; };// allows different yaw_speeds for each activity + BOOL BuildRoute ( const Vector &vecGoal, int iMoveFlag, CBaseEntity *pTarget ); + virtual BOOL BuildNearestRoute ( Vector vecThreat, Vector vecViewOffset, float flMinDist, float flMaxDist ); + int RouteClassify( int iMoveFlag ); + void InsertWaypoint ( Vector vecLocation, int afMoveFlags ); + + BOOL FindLateralCover ( const Vector &vecThreat, const Vector &vecViewOffset ); + virtual BOOL FindCover ( Vector vecThreat, Vector vecViewOffset, float flMinDist, float flMaxDist ); + virtual BOOL FValidateCover ( const Vector &vecCoverLocation ) { return TRUE; }; + virtual float CoverRadius( void ) { return 784; } // Default cover radius + + virtual BOOL FCanCheckAttacks ( void ); + virtual void CheckAmmo( void ) { return; }; + virtual int IgnoreConditions ( void ); + + inline void SetConditions( int iConditions ) { m_afConditions |= iConditions; } + inline void ClearConditions( int iConditions ) { m_afConditions &= ~iConditions; } + inline BOOL HasConditions( int iConditions ) { if ( m_afConditions & iConditions ) return TRUE; return FALSE; } + inline BOOL HasAllConditions( int iConditions ) { if ( (m_afConditions & iConditions) == iConditions ) return TRUE; return FALSE; } + + virtual BOOL FValidateHintType( short sHint ); + int FindHintNode ( void ); + virtual BOOL FCanActiveIdle ( void ); + void SetTurnActivity ( void ); + float FLSoundVolume ( CSound *pSound ); + + BOOL MoveToNode( Activity movementAct, float waitTime, const Vector &goal ); + BOOL MoveToTarget( Activity movementAct, float waitTime ); + BOOL MoveToLocation( Activity movementAct, float waitTime, const Vector &goal ); + BOOL MoveToEnemy( Activity movementAct, float waitTime ); + + // Returns the time when the door will be open + float OpenDoorAndWait( entvars_t *pevDoor ); + + virtual int ISoundMask( void ); + virtual CSound* PBestSound ( void ); + virtual CSound* PBestScent ( void ); + virtual float HearingSensitivity( void ) { return 1.0; }; + + BOOL FBecomeProne ( void ); + virtual void BarnacleVictimBitten( entvars_t *pevBarnacle ); + virtual void BarnacleVictimReleased( void ); + + void SetEyePosition ( void ); + + BOOL FShouldEat( void );// see if a monster is 'hungry' + void Eat ( float flFullDuration );// make the monster 'full' for a while. + + CBaseEntity *CheckTraceHullAttack( float flDist, int iDamage, int iDmgType ); + BOOL FacingIdeal( void ); + + BOOL FCheckAITrigger( void );// checks and, if necessary, fires the monster's trigger target. + BOOL NoFriendlyFire( void ); + + BOOL BBoxFlat( void ); + + // PrescheduleThink + virtual void PrescheduleThink( void ) { return; }; + + BOOL GetEnemy ( void ); + void MakeDamageBloodDecal ( int cCount, float flNoise, TraceResult *ptr, const Vector &vecDir ); + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + + // combat functions + float UpdateTarget ( entvars_t *pevTarget ); + virtual Activity GetDeathActivity ( void ); + Activity GetSmallFlinchActivity( void ); + virtual void Killed( entvars_t *pevAttacker, int iGib ); + virtual void GibMonster( void ); + BOOL ShouldGibMonster( int iGib ); + void CallGibMonster( void ); + virtual int HasCustomGibs( void ) { return FALSE; } //LRC + virtual BOOL HasHumanGibs( void ); + virtual BOOL HasAlienGibs( void ); + + Vector ShootAtEnemy( const Vector &shootOrigin ); + virtual Vector BodyTarget( const Vector &posSrc ) { return Center( ) * 0.75 + EyePosition() * 0.25; }; // position to shoot at + + virtual Vector GetGunPosition( void ); + + virtual int TakeHealth( float flHealth, int bitsDamageType ); + virtual int TakeArmor( float flArmor, int suit = 0 ); + virtual int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType); + int DeadTakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ); + + void RadiusDamage(entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int iClassIgnore, int bitsDamageType ); + void RadiusDamage(Vector vecSrc, entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int iClassIgnore, int bitsDamageType ); + virtual int IsMoving( void ) { return m_movementGoal != MOVEGOAL_NONE; } + + void RouteClear( void ); + void RouteNew( void ); + + virtual void DeathSound ( void ) { return; }; + virtual void AlertSound ( void ) { return; }; + virtual void IdleSound ( void ) { return; }; + virtual void PainSound ( void ) { return; }; + + virtual void StopFollowing( BOOL clearSchedule ) {} + + inline void Remember( int iMemory ) { m_afMemory |= iMemory; } + inline void Forget( int iMemory ) { m_afMemory &= ~iMemory; } + inline BOOL HasMemory( int iMemory ) { if ( m_afMemory & iMemory ) return TRUE; return FALSE; } + inline BOOL HasAllMemories( int iMemory ) { if ( (m_afMemory & iMemory) == iMemory ) return TRUE; return FALSE; } + + BOOL ExitScriptedSequence( ); + BOOL CineCleanup( ); + + void StartPatrol( CBaseEntity *path ); + + CBaseEntity* DropItem ( char *pszItemName, const Vector &vecPos, const Vector &vecAng );// drop an item. +}; + + + +#endif // BASEMONSTER_H diff --git a/server/monsters/combat.cpp b/server/monsters/combat.cpp new file mode 100644 index 00000000..5ff4b989 --- /dev/null +++ b/server/monsters/combat.cpp @@ -0,0 +1,260 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +/* + +===== combat.cpp ======================================================== + + functions dealing with damage infliction & death + +*/ + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "soundent.h" +#include "decals.h" +#include "animation.h" +#include "baseweapon.h" +#include "basebrush.h" +#include "defaults.h" + +extern DLL_GLOBAL Vector g_vecAttackDir; + +extern Vector VecBModelOrigin( entvars_t* pevBModel ); + +MULTIDAMAGE gMultiDamage; + + +/* +============================================================================== + +MULTI-DAMAGE + +Collects multiple small damages into a single damage + +============================================================================== +*/ + +// +// ClearMultiDamage - resets the global multi damage accumulator +// +void ClearMultiDamage(void) +{ + gMultiDamage.pEntity = NULL; + gMultiDamage.amount = 0; + gMultiDamage.type = 0; +} + + +// +// ApplyMultiDamage - inflicts contents of global multi damage register on gMultiDamage.pEntity +// +// GLOBALS USED: +// gMultiDamage + +void ApplyMultiDamage(entvars_t *pevInflictor, entvars_t *pevAttacker ) +{ + Vector vecSpot1;//where blood comes from + Vector vecDir;//direction blood should go + TraceResult tr; + + if ( !gMultiDamage.pEntity ) + return; + + gMultiDamage.pEntity->TakeDamage(pevInflictor, pevAttacker, gMultiDamage.amount, gMultiDamage.type ); +} + + +// GLOBALS USED: +// gMultiDamage + +void AddMultiDamage( entvars_t *pevInflictor, CBaseEntity *pEntity, float flDamage, int bitsDamageType) +{ + if ( !pEntity ) + return; + + gMultiDamage.type |= bitsDamageType; + + if ( pEntity != gMultiDamage.pEntity ) + { + ApplyMultiDamage(pevInflictor,pevInflictor); // UNDONE: wrong attacker! + gMultiDamage.pEntity = pEntity; + gMultiDamage.amount = 0; + } + + gMultiDamage.amount += flDamage; +} + +void DecalGunshot( TraceResult *pTrace, int iBulletType ) +{ + // Is the entity valid + if ( !UTIL_IsValidEntity( pTrace->pHit ) ) + return; + + if ( VARS(pTrace->pHit)->solid == SOLID_BSP || VARS(pTrace->pHit)->movetype == MOVETYPE_PUSHSTEP ) + { + CBaseEntity *pEntity = NULL; + // Decal the wall with a gunshot + if ( !FNullEnt(pTrace->pHit) ) + pEntity = CBaseEntity::Instance(pTrace->pHit); + + switch( iBulletType ) + { + case BULLET_9MM: + case BULLET_MP5: + case BULLET_BUCKSHOT: + case BULLET_357: + default: + // smoke and decal + UTIL_GunshotDecalTrace( pTrace, DamageDecal( pEntity, DMG_BULLET ) ); + break; + case BULLET_12MM: + // smoke and decal + UTIL_GunshotDecalTrace( pTrace, DamageDecal( pEntity, DMG_BULLET ) ); + break; + case BULLET_CROWBAR: + // wall decal + UTIL_DecalTrace( pTrace, DamageDecal( pEntity, DMG_CLUB ) ); + break; + } + } +} + +// +// EjectBrass - tosses a brass shell from passed origin at passed velocity +// +void EjectBrass ( const Vector &vecOrigin, const Vector &vecVelocity, float rotation, int model, int soundtype ) +{ + // FIX: when the player shoots, their gun isn't in the same position as it is on the model other players see. + + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecOrigin ); + WRITE_BYTE( TE_MODEL); + WRITE_COORD( vecOrigin.x); + WRITE_COORD( vecOrigin.y); + WRITE_COORD( vecOrigin.z); + WRITE_COORD( vecVelocity.x); + WRITE_COORD( vecVelocity.y); + WRITE_COORD( vecVelocity.z); + WRITE_ANGLE( rotation ); + WRITE_SHORT( model ); + WRITE_BYTE ( soundtype); + WRITE_BYTE ( 25 );// 2.5 seconds + MESSAGE_END(); +} + +/* +================ +SpawnBlood +================ +*/ +void SpawnBlood(Vector vecSpot, int bloodColor, float flDamage) +{ + UTIL_BloodDrips( vecSpot, g_vecAttackDir, bloodColor, (int)flDamage ); +} + + +int DamageDecal( CBaseEntity *pEntity, int bitsDamageType ) +{ + if ( !pEntity ) + return (DECAL_GUNSHOT1 + RANDOM_LONG(0,4)); + + return pEntity->DamageDecal( bitsDamageType ); +} + + +// +// RadiusDamage - this entity is exploding, or otherwise needs to inflict damage upon entities within a certain range. +// +// only damage ents that can clearly be seen by the explosion! + + +void RadiusDamage( Vector vecSrc, entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, float flRadius, int iClassIgnore, int bitsDamageType ) +{ + CBaseEntity *pEntity = NULL; + TraceResult tr; + float flAdjustedDamage, falloff; + Vector vecSpot; + + if ( flRadius ) + falloff = flDamage / flRadius; + else + falloff = 1.0; + + int bInWater = (UTIL_PointContents ( vecSrc ) & MASK_WATER ); + + vecSrc.z += 1;// in case grenade is lying on the ground + + if ( !pevAttacker ) + pevAttacker = pevInflictor; + + // iterate on all entities in the vicinity. + while ((pEntity = UTIL_FindEntityInSphere( pEntity, vecSrc, flRadius )) != NULL) + { + if ( pEntity->pev->takedamage != DAMAGE_NO ) + { + // UNDONE: this should check a damage mask, not an ignore + if ( iClassIgnore != CLASS_NONE && pEntity->Classify() == iClassIgnore ) + {// houndeyes don't hurt other houndeyes with their attack + continue; + } + + // blast's don't tavel into or out of water + if (bInWater && pEntity->pev->waterlevel == 0) + continue; + if (!bInWater && pEntity->pev->waterlevel == 3) + continue; + + vecSpot = pEntity->BodyTarget( vecSrc ); + + UTIL_TraceLine ( vecSrc, vecSpot, dont_ignore_monsters, ENT(pevInflictor), &tr ); + + if ( tr.flFraction == 1.0 || tr.pHit == pEntity->edict() ) + {// the explosion can 'see' this entity, so hurt them! + if (tr.fStartSolid) + { + // if we're stuck inside them, fixup the position and distance + tr.vecEndPos = vecSrc; + tr.flFraction = 0.0; + } + + // decrease damage for an ent that's farther from the bomb. + flAdjustedDamage = ( vecSrc - tr.vecEndPos ).Length() * falloff; + flAdjustedDamage = flDamage - flAdjustedDamage; + + if ( flAdjustedDamage < 0 ) + { + flAdjustedDamage = 0; + } + + // ALERT( at_console, "hit %s\n", STRING( pEntity->pev->classname ) ); + if (tr.flFraction != 1.0) + { + ClearMultiDamage( ); + pEntity->TraceAttack( pevInflictor, flAdjustedDamage, (tr.vecEndPos - vecSrc).Normalize( ), &tr, bitsDamageType ); + ApplyMultiDamage( pevInflictor, pevAttacker ); + } + else + { + pEntity->TakeDamage ( pevInflictor, pevAttacker, flAdjustedDamage, bitsDamageType ); + } + } + } + } +} + + + + diff --git a/server/monsters/damage.h b/server/monsters/damage.h new file mode 100644 index 00000000..0761e7a1 --- /dev/null +++ b/server/monsters/damage.h @@ -0,0 +1,21 @@ +//======================================================================= +// Copyright (C) XashXT Group 2006 +//======================================================================= + +extern void ClearMultiDamage(void); +extern void ApplyMultiDamage(entvars_t* pevInflictor, entvars_t* pevAttacker ); +extern void AddMultiDamage( entvars_t *pevInflictor, CBaseEntity *pEntity, float flDamage, int bitsDamageType); + +extern void DecalGunshot( TraceResult *pTrace, int iBulletType ); +extern void SpawnBlood(Vector vecSpot, int bloodColor, float flDamage); +extern int DamageDecal( CBaseEntity *pEntity, int bitsDamageType ); +extern void RadiusDamage( Vector vecSrc, entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, float flRadius, int iClassIgnore, int bitsDamageType ); + +typedef struct +{ + CBaseEntity *pEntity; + float amount; + int type; +} MULTIDAMAGE; + +extern MULTIDAMAGE gMultiDamage; \ No newline at end of file diff --git a/server/monsters/defaultai.cpp b/server/monsters/defaultai.cpp new file mode 100644 index 00000000..02312149 --- /dev/null +++ b/server/monsters/defaultai.cpp @@ -0,0 +1,1260 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// Default behaviors. +//========================================================= +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "defaultai.h" +#include "soundent.h" +#include "nodes.h" +#include "scripted.h" + +//========================================================= +// Fail +//========================================================= +Task_t tlFail[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT, (float)2 }, + { TASK_WAIT_PVS, (float)0 }, +}; + +Schedule_t slFail[] = +{ + { + tlFail, + ARRAYSIZE ( tlFail ), + bits_COND_CAN_ATTACK, + 0, + "Fail" + }, +}; + +//========================================================= +// Idle Schedules +//========================================================= +Task_t tlIdleStand1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT, (float)5 },// repick IDLESTAND every five seconds. gives us a chance to pick an active idle, fidget, etc. +}; + +Schedule_t slIdleStand[] = +{ + { + tlIdleStand1, + ARRAYSIZE ( tlIdleStand1 ), + bits_COND_NEW_ENEMY | + bits_COND_SEE_FEAR | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_SMELL_FOOD | + bits_COND_SMELL | + bits_COND_PROVOKED, + + bits_SOUND_COMBAT |// sound flags + bits_SOUND_WORLD | + bits_SOUND_PLAYER | + bits_SOUND_DANGER | + + bits_SOUND_MEAT |// scents + bits_SOUND_CARCASS | + bits_SOUND_GARBAGE, + "IdleStand" + }, +}; + +Schedule_t slIdleTrigger[] = +{ + { + tlIdleStand1, + ARRAYSIZE ( tlIdleStand1 ), + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE, + 0, + "Idle Trigger" + }, +}; + + +Task_t tlIdleWalk1[] = +{ + { TASK_WALK_PATH, (float)9999 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, +}; + +Schedule_t slIdleWalk[] = +{ + { + tlIdleWalk1, + ARRAYSIZE ( tlIdleWalk1 ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_SMELL_FOOD | + bits_COND_SMELL | + bits_COND_PROVOKED, + + bits_SOUND_COMBAT |// sound flags + + bits_SOUND_MEAT |// scents + bits_SOUND_CARCASS | + bits_SOUND_GARBAGE, + "Idle Walk" + }, +}; + +//========================================================= +// Ambush - monster stands in place and waits for a new +// enemy, or chance to attack an existing enemy. +//========================================================= +Task_t tlAmbush[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT_INDEFINITE, (float)0 }, +}; + +Schedule_t slAmbush[] = +{ + { + tlAmbush, + ARRAYSIZE ( tlAmbush ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_PROVOKED, + + 0, + "Ambush" + }, +}; + +//========================================================= +// ActiveIdle schedule - !!!BUGBUG - if this schedule doesn't +// complete on its own, the monster's HintNode will not be +// cleared, and the rest of the monster's group will avoid +// that node because they think the group member that was +// previously interrupted is still using that node to active +// idle. +///========================================================= +Task_t tlActiveIdle[] = +{ + { TASK_FIND_HINTNODE, (float)0 }, + { TASK_GET_PATH_TO_HINTNODE, (float)0 }, + { TASK_STORE_LASTPOSITION, (float)0 }, + { TASK_WALK_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_FACE_HINTNODE, (float)0 }, + { TASK_PLAY_ACTIVE_IDLE, (float)0 }, + { TASK_GET_PATH_TO_LASTPOSITION,(float)0 }, + { TASK_WALK_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_CLEAR_LASTPOSITION, (float)0 }, + { TASK_CLEAR_HINTNODE, (float)0 }, +}; + +Schedule_t slActiveIdle[] = +{ + { + tlActiveIdle, + ARRAYSIZE( tlActiveIdle ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_PROVOKED | + bits_COND_HEAR_SOUND, + + bits_SOUND_COMBAT | + bits_SOUND_WORLD | + bits_SOUND_PLAYER | + bits_SOUND_DANGER, + "Active Idle" + } +}; + +//========================================================= +// Wake Schedules +//========================================================= +Task_t tlWakeAngry1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_SOUND_WAKE, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, +}; + +Schedule_t slWakeAngry[] = +{ + { + tlWakeAngry1, + ARRAYSIZE ( tlWakeAngry1 ), + 0, + 0, + "Wake Angry" + } +}; + +//========================================================= +// AlertFace Schedules +//========================================================= +Task_t tlAlertFace1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_FACE_IDEAL, (float)0 }, +}; + +Schedule_t slAlertFace[] = +{ + { + tlAlertFace1, + ARRAYSIZE ( tlAlertFace1 ), + bits_COND_NEW_ENEMY | + bits_COND_SEE_FEAR | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_PROVOKED, + 0, + "Alert Face" + }, +}; + +//========================================================= +// AlertSmallFlinch Schedule - shot, but didn't see attacker, +// flinch then face +//========================================================= +Task_t tlAlertSmallFlinch[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_REMEMBER, (float)bits_MEMORY_FLINCHED }, + { TASK_SMALL_FLINCH, (float)0 }, + { TASK_SET_SCHEDULE, (float)SCHED_ALERT_FACE }, +}; + +Schedule_t slAlertSmallFlinch[] = +{ + { + tlAlertSmallFlinch, + ARRAYSIZE ( tlAlertSmallFlinch ), + 0, + 0, + "Alert Small Flinch" + }, +}; + +//========================================================= +// AlertIdle Schedules +//========================================================= +Task_t tlAlertStand1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT, (float)20 }, + { TASK_SUGGEST_STATE, (float)MONSTERSTATE_IDLE }, +}; + +Schedule_t slAlertStand[] = +{ + { + tlAlertStand1, + ARRAYSIZE ( tlAlertStand1 ), + bits_COND_NEW_ENEMY | + bits_COND_SEE_ENEMY | + bits_COND_SEE_FEAR | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_PROVOKED | + bits_COND_SMELL | + bits_COND_SMELL_FOOD | + bits_COND_HEAR_SOUND, + + bits_SOUND_COMBAT |// sound flags + bits_SOUND_WORLD | + bits_SOUND_PLAYER | + bits_SOUND_DANGER | + + bits_SOUND_MEAT |// scent flags + bits_SOUND_CARCASS | + bits_SOUND_GARBAGE, + "Alert Stand" + }, +}; + +//========================================================= +// InvestigateSound - sends a monster to the location of the +// sound that was just heard, to check things out. +//========================================================= +Task_t tlInvestigateSound[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_STORE_LASTPOSITION, (float)0 }, + { TASK_GET_PATH_TO_BESTSOUND, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_WALK_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_IDLE }, + { TASK_WAIT, (float)10 }, + { TASK_GET_PATH_TO_LASTPOSITION,(float)0 }, + { TASK_WALK_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_CLEAR_LASTPOSITION, (float)0 }, +}; + +Schedule_t slInvestigateSound[] = +{ + { + tlInvestigateSound, + ARRAYSIZE ( tlInvestigateSound ), + bits_COND_NEW_ENEMY | + bits_COND_SEE_FEAR | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "InvestigateSound" + }, +}; + +//========================================================= +// CombatIdle Schedule +//========================================================= +Task_t tlCombatStand1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT_INDEFINITE, (float)0 }, +}; + +Schedule_t slCombatStand[] = +{ + { + tlCombatStand1, + ARRAYSIZE ( tlCombatStand1 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_CAN_ATTACK, + 0, + "Combat Stand" + }, +}; + +//========================================================= +// CombatFace Schedule +//========================================================= +Task_t tlCombatFace1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_FACE_ENEMY, (float)0 }, +}; + +Schedule_t slCombatFace[] = +{ + { + tlCombatFace1, + ARRAYSIZE ( tlCombatFace1 ), + bits_COND_CAN_ATTACK | + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD, + 0, + "Combat Face" + }, +}; + +//========================================================= +// Standoff schedule. Used in combat when a monster is +// hiding in cover or the enemy has moved out of sight. +// Should we look around in this schedule? +//========================================================= +Task_t tlStandoff[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT_FACE_ENEMY, (float)2 }, +}; + +Schedule_t slStandoff[] = +{ + { + tlStandoff, + ARRAYSIZE ( tlStandoff ), + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_ENEMY_DEAD | + bits_COND_NEW_ENEMY | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "Standoff" + } +}; + +//========================================================= +// Arm weapon (draw gun) +//========================================================= +Task_t tlArmWeapon[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_PLAY_SEQUENCE, (float) ACT_ARM } +}; + +Schedule_t slArmWeapon[] = +{ + { + tlArmWeapon, + ARRAYSIZE ( tlArmWeapon ), + 0, + 0, + "Arm Weapon" + } +}; + +//========================================================= +// reload schedule +//========================================================= +Task_t tlReload[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_PLAY_SEQUENCE, float(ACT_RELOAD) }, +}; + +Schedule_t slReload[] = +{ + { + tlReload, + ARRAYSIZE ( tlReload ), + bits_COND_HEAVY_DAMAGE, + 0, + "Reload" + } +}; + +//========================================================= +// Attack Schedules +//========================================================= + +// primary range attack +Task_t tlRangeAttack1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, +}; + +Schedule_t slRangeAttack1[] = +{ + { + tlRangeAttack1, + ARRAYSIZE ( tlRangeAttack1 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED | + bits_COND_NO_AMMO_LOADED | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "Range Attack1" + }, +}; + +// secondary range attack +Task_t tlRangeAttack2[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_RANGE_ATTACK2, (float)0 }, +}; + +Schedule_t slRangeAttack2[] = +{ + { + tlRangeAttack2, + ARRAYSIZE ( tlRangeAttack2 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "Range Attack2" + }, +}; + +// primary melee attack +Task_t tlPrimaryMeleeAttack1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_MELEE_ATTACK1, (float)0 }, +}; + +Schedule_t slPrimaryMeleeAttack[] = +{ + { + tlPrimaryMeleeAttack1, + ARRAYSIZE ( tlPrimaryMeleeAttack1 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED, + 0, + "Primary Melee Attack" + }, +}; + +// secondary melee attack +Task_t tlSecondaryMeleeAttack1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_MELEE_ATTACK2, (float)0 }, +}; + +Schedule_t slSecondaryMeleeAttack[] = +{ + { + tlSecondaryMeleeAttack1, + ARRAYSIZE ( tlSecondaryMeleeAttack1 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED, + 0, + "Secondary Melee Attack" + }, +}; + +// special attack1 +Task_t tlSpecialAttack1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_SPECIAL_ATTACK1, (float)0 }, +}; + +Schedule_t slSpecialAttack1[] = +{ + { + tlSpecialAttack1, + ARRAYSIZE ( tlSpecialAttack1 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED | + bits_COND_NO_AMMO_LOADED | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "Special Attack1" + }, +}; + +// special attack2 +Task_t tlSpecialAttack2[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_SPECIAL_ATTACK2, (float)0 }, +}; + +Schedule_t slSpecialAttack2[] = +{ + { + tlSpecialAttack2, + ARRAYSIZE ( tlSpecialAttack2 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED | + bits_COND_NO_AMMO_LOADED | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "Special Attack2" + }, +}; + +// Chase enemy schedule +Task_t tlChaseEnemy1[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_CHASE_ENEMY_FAILED }, + { TASK_GET_PATH_TO_ENEMY, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, +}; + +Schedule_t slChaseEnemy[] = +{ + { + tlChaseEnemy1, + ARRAYSIZE ( tlChaseEnemy1 ), + bits_COND_NEW_ENEMY | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_MELEE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_CAN_MELEE_ATTACK2 | + bits_COND_TASK_FAILED | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "Chase Enemy" + }, +}; + + +// Chase enemy failure schedule +Task_t tlChaseEnemyFailed[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_WAIT, (float)0.2 }, + { TASK_FIND_COVER_FROM_ENEMY, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, +// { TASK_TURN_LEFT, (float)179 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_WAIT, (float)1 }, +}; + +Schedule_t slChaseEnemyFailed[] = +{ + { + tlChaseEnemyFailed, + ARRAYSIZE ( tlChaseEnemyFailed ), + bits_COND_NEW_ENEMY | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_MELEE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_CAN_MELEE_ATTACK2 | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "tlChaseEnemyFailed" + }, +}; + + +//========================================================= +// small flinch, played when minor damage is taken. +//========================================================= +Task_t tlSmallFlinch[] = +{ + { TASK_REMEMBER, (float)bits_MEMORY_FLINCHED }, + { TASK_STOP_MOVING, 0 }, + { TASK_SMALL_FLINCH, 0 }, +}; + +Schedule_t slSmallFlinch[] = +{ + { + tlSmallFlinch, + ARRAYSIZE ( tlSmallFlinch ), + 0, + 0, + "Small Flinch" + }, +}; + +//========================================================= +// Die! +//========================================================= +Task_t tlDie1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SOUND_DIE, (float)0 }, + { TASK_DIE, (float)0 }, +}; + +Schedule_t slDie[] = +{ + { + tlDie1, + ARRAYSIZE( tlDie1 ), + 0, + 0, + "Die" + }, +}; + +//========================================================= +// Victory Dance +//========================================================= +Task_t tlVictoryDance[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_VICTORY_DANCE }, + { TASK_WAIT, (float)0 }, +}; + +Schedule_t slVictoryDance[] = +{ + { + tlVictoryDance, + ARRAYSIZE( tlVictoryDance ), + 0, + 0, + "Victory Dance" + }, +}; + +//========================================================= +// BarnacleVictimGrab - barnacle tongue just hit the monster, +// so play a hit animation, then play a cycling pull animation +// as the creature is hoisting the monster. +//========================================================= +Task_t tlBarnacleVictimGrab[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_BARNACLE_HIT }, + { TASK_SET_ACTIVITY, (float)ACT_BARNACLE_PULL }, + { TASK_WAIT_INDEFINITE, (float)0 },// just cycle barnacle pull anim while barnacle hoists. +}; + +Schedule_t slBarnacleVictimGrab[] = +{ + { + tlBarnacleVictimGrab, + ARRAYSIZE ( tlBarnacleVictimGrab ), + 0, + 0, + "Barnacle Victim" + } +}; + +//========================================================= +// BarnacleVictimChomp - barnacle has pulled the prey to its +// mouth. Victim should play the BARNCLE_CHOMP animation +// once, then loop the BARNACLE_CHEW animation indefinitely +//========================================================= +Task_t tlBarnacleVictimChomp[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_BARNACLE_CHOMP }, + { TASK_SET_ACTIVITY, (float)ACT_BARNACLE_CHEW }, + { TASK_WAIT_INDEFINITE, (float)0 },// just cycle barnacle pull anim while barnacle hoists. +}; + +Schedule_t slBarnacleVictimChomp[] = +{ + { + tlBarnacleVictimChomp, + ARRAYSIZE ( tlBarnacleVictimChomp ), + 0, + 0, + "Barnacle Chomp" + } +}; + + +// Universal Error Schedule +Task_t tlError[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_WAIT_INDEFINITE, (float)0 }, +}; + +Schedule_t slError[] = +{ + { + tlError, + ARRAYSIZE ( tlError ), + 0, + 0, + "Error" + }, +}; + +//LRC +Task_t tlScriptedTeleport[] = +{ + { TASK_PLANT_ON_SCRIPT, (float)0 }, + { TASK_WAIT_FOR_SCRIPT, (float)0 }, + { TASK_PLAY_SCRIPT, (float)0 }, + { TASK_END_SCRIPT, (float)0 }, +}; + +//LRC +Schedule_t slTeleportToScript[] = +{ + { + tlScriptedTeleport, + ARRAYSIZE ( tlScriptedTeleport ), + SCRIPT_BREAK_CONDITIONS, + 0, + "TeleportToScript" + }, +}; + +Task_t tlScriptedWalk[] = +{ + { TASK_WALK_TO_SCRIPT, (float)TARGET_MOVE_SCRIPTED }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_PLANT_ON_SCRIPT, (float)0 }, + { TASK_FACE_SCRIPT, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_ENABLE_SCRIPT, (float)0 }, + { TASK_WAIT_FOR_SCRIPT, (float)0 }, + { TASK_PLAY_SCRIPT, (float)0 }, + { TASK_END_SCRIPT, (float)0 }, +}; + +Schedule_t slWalkToScript[] = +{ + { + tlScriptedWalk, + ARRAYSIZE ( tlScriptedWalk ), + SCRIPT_BREAK_CONDITIONS, + 0, + "WalkToScript" + }, +}; + + +Task_t tlScriptedRun[] = +{ + { TASK_RUN_TO_SCRIPT, (float)TARGET_MOVE_SCRIPTED }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_PLANT_ON_SCRIPT, (float)0 }, + { TASK_FACE_SCRIPT, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_ENABLE_SCRIPT, (float)0 }, + { TASK_WAIT_FOR_SCRIPT, (float)0 }, + { TASK_PLAY_SCRIPT, (float)0 }, + { TASK_END_SCRIPT, (float)0 }, +}; + +Schedule_t slRunToScript[] = +{ + { + tlScriptedRun, + ARRAYSIZE ( tlScriptedRun ), + SCRIPT_BREAK_CONDITIONS, + 0, + "RunToScript" + }, +}; + +Task_t tlScriptedWait[] = +{ + { TASK_STOP_MOVING, 0 }, +// { TASK_ENABLE_SCRIPT, (float)0 }, + { TASK_WAIT_FOR_SCRIPT, (float)0 }, + { TASK_PLAY_SCRIPT, (float)0 }, + { TASK_END_SCRIPT, (float)0 }, +}; + +Schedule_t slWaitScript[] = +{ + { + tlScriptedWait, + ARRAYSIZE ( tlScriptedWait ), + SCRIPT_BREAK_CONDITIONS, + 0, + "WaitForScript" + }, +}; + +Task_t tlScriptedFace[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_SCRIPT, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_WAIT_FOR_SCRIPT, (float)0 }, + { TASK_PLAY_SCRIPT, (float)0 }, + { TASK_END_SCRIPT, (float)0 }, +}; + +Schedule_t slFaceScript[] = +{ + { + tlScriptedFace, + ARRAYSIZE ( tlScriptedFace ), + SCRIPT_BREAK_CONDITIONS, + 0, + "FaceScript" + }, +}; + +//========================================================= +// Cower - this is what is usually done when attempts +// to escape danger fail. +//========================================================= +Task_t tlCower[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_COWER }, +}; + +Schedule_t slCower[] = +{ + { + tlCower, + ARRAYSIZE ( tlCower ), + 0, + 0, + "Cower" + }, +}; + +//========================================================= +// move away from where you're currently standing. +//========================================================= +Task_t tlTakeCoverFromOrigin[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FIND_COVER_FROM_ORIGIN, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_TURN_LEFT, (float)179 }, +}; + +Schedule_t slTakeCoverFromOrigin[] = +{ + { + tlTakeCoverFromOrigin, + ARRAYSIZE ( tlTakeCoverFromOrigin ), + bits_COND_NEW_ENEMY, + 0, + "TakeCoverFromOrigin" + }, +}; + +//========================================================= +// hide from the loudest sound source +//========================================================= +Task_t tlTakeCoverFromBestSound[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FIND_COVER_FROM_BEST_SOUND, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_TURN_LEFT, (float)179 }, +}; + +Schedule_t slTakeCoverFromBestSound[] = +{ + { + tlTakeCoverFromBestSound, + ARRAYSIZE ( tlTakeCoverFromBestSound ), + bits_COND_NEW_ENEMY, + 0, + "TakeCoverFromBestSound" + }, +}; + +//========================================================= +// Take cover from enemy! Tries lateral cover before node +// cover! +//========================================================= +Task_t tlTakeCoverFromEnemy[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_WAIT, (float)0.2 }, + { TASK_FIND_COVER_FROM_ENEMY, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, +// { TASK_TURN_LEFT, (float)179 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_WAIT, (float)1 }, +}; + +Schedule_t slTakeCoverFromEnemy[] = +{ + { + tlTakeCoverFromEnemy, + ARRAYSIZE ( tlTakeCoverFromEnemy ), + bits_COND_NEW_ENEMY, + 0, + "tlTakeCoverFromEnemy" + }, +}; + +Schedule_t *CBaseMonster::m_scheduleList[] = +{ + slIdleStand, + slIdleTrigger, + slIdleWalk, + slAmbush, + slActiveIdle, + slWakeAngry, + slAlertFace, + slAlertSmallFlinch, + slAlertStand, + slInvestigateSound, + slCombatStand, + slCombatFace, + slStandoff, + slArmWeapon, + slReload, + slRangeAttack1, + slRangeAttack2, + slPrimaryMeleeAttack, + slSecondaryMeleeAttack, + slSpecialAttack1, + slSpecialAttack2, + slChaseEnemy, + slChaseEnemyFailed, + slSmallFlinch, + slDie, + slVictoryDance, + slBarnacleVictimGrab, + slBarnacleVictimChomp, + slError, + slWalkToScript, + slRunToScript, + slWaitScript, + slFaceScript, + slCower, + slTakeCoverFromOrigin, + slTakeCoverFromBestSound, + slTakeCoverFromEnemy, + slFail +}; + +Schedule_t *CBaseMonster::ScheduleFromName( const char *pName ) +{ + return ScheduleInList( pName, m_scheduleList, ARRAYSIZE(m_scheduleList) ); +} + + +Schedule_t *CBaseMonster :: ScheduleInList( const char *pName, Schedule_t **pList, int listCount ) +{ + int i; + + if ( !pName ) + { + ALERT( at_debug, "%s set to unnamed schedule!\n", STRING(pev->classname) ); + return NULL; + } + + + for ( i = 0; i < listCount; i++ ) + { + if ( !pList[i]->pName ) + { + ALERT( at_debug, "Unnamed schedule!\n" ); + continue; + } + if ( stricmp( pName, pList[i]->pName ) == 0 ) + return pList[i]; + } + return NULL; +} + +//========================================================= +// GetScheduleOfType - returns a pointer to one of the +// monster's available schedules of the indicated type. +//========================================================= +Schedule_t* CBaseMonster :: GetScheduleOfType ( int Type ) +{ +// ALERT ( at_console, "Sched Type:%d\n", Type ); + switch ( Type ) + { + // This is the schedule for scripted sequences AND scripted AI. // LRC- And scripted actions, too. + case SCHED_AISCRIPT: + { +// ALERT(at_console, "Doing AISCRIPT\n"); + ASSERT( m_pCine != NULL ); + if ( !m_pCine ) + { + ALERT( at_aiconsole, "Script failed for %s\n", STRING(pev->classname) ); + CineCleanup(); + return GetScheduleOfType( SCHED_IDLE_STAND ); + } +// else +// ALERT( at_aiconsole, "Starting script %s for %s\n", STRING( m_pCine->m_iszPlay ), STRING(pev->classname) ); + + switch ( m_pCine->m_fMoveTo ) + { + case 0: + return slWaitScript; + case 4: case 6: + return slTeleportToScript; + case 1: + return slWalkToScript; + case 2: + return slRunToScript; + case 5: + return slFaceScript; + } + break; + } + case SCHED_IDLE_STAND: + { + if ( RANDOM_LONG(0,14) == 0 && FCanActiveIdle() ) + { + return &slActiveIdle[ 0 ]; + } + + return &slIdleStand[ 0 ]; + } + case SCHED_IDLE_WALK: + { + return &slIdleWalk[ 0 ]; + } + case SCHED_WAIT_TRIGGER: + { + return &slIdleTrigger[ 0 ]; + } + case SCHED_WAKE_ANGRY: + { + return &slWakeAngry[ 0 ]; + } + case SCHED_ALERT_FACE: + { + return &slAlertFace[ 0 ]; + } + case SCHED_ALERT_STAND: + { + return &slAlertStand[ 0 ]; + } + case SCHED_COMBAT_STAND: + { + return &slCombatStand[ 0 ]; + } + case SCHED_COMBAT_FACE: + { + return &slCombatFace[ 0 ]; + } + case SCHED_CHASE_ENEMY: + { + return &slChaseEnemy[ 0 ]; + } + case SCHED_CHASE_ENEMY_FAILED: + { + return &slFail[ 0 ]; + } + case SCHED_SMALL_FLINCH: + { + return &slSmallFlinch[ 0 ]; + } + case SCHED_ALERT_SMALL_FLINCH: + { + return &slAlertSmallFlinch[ 0 ]; + } + case SCHED_RELOAD: + { + return &slReload[ 0 ]; + } + case SCHED_ARM_WEAPON: + { + return &slArmWeapon[ 0 ]; + } + case SCHED_STANDOFF: + { + return &slStandoff[ 0 ]; + } + case SCHED_RANGE_ATTACK1: + { + return &slRangeAttack1[ 0 ]; + } + case SCHED_RANGE_ATTACK2: + { + return &slRangeAttack2[ 0 ]; + } + case SCHED_MELEE_ATTACK1: + { + return &slPrimaryMeleeAttack[ 0 ]; + } + case SCHED_MELEE_ATTACK2: + { + return &slSecondaryMeleeAttack[ 0 ]; + } + case SCHED_SPECIAL_ATTACK1: + { + return &slSpecialAttack1[ 0 ]; + } + case SCHED_SPECIAL_ATTACK2: + { + return &slSpecialAttack2[ 0 ]; + } + case SCHED_TAKE_COVER_FROM_BEST_SOUND: + { + return &slTakeCoverFromBestSound[ 0 ]; + } + case SCHED_TAKE_COVER_FROM_ENEMY: + { + return &slTakeCoverFromEnemy[ 0 ]; + } + case SCHED_COWER: + { + return &slCower[ 0 ]; + } + case SCHED_AMBUSH: + { + return &slAmbush[ 0 ]; + } + case SCHED_BARNACLE_VICTIM_GRAB: + { + return &slBarnacleVictimGrab[ 0 ]; + } + case SCHED_BARNACLE_VICTIM_CHOMP: + { + return &slBarnacleVictimChomp[ 0 ]; + } + case SCHED_INVESTIGATE_SOUND: + { + return &slInvestigateSound[ 0 ]; + } + case SCHED_DIE: + { + return &slDie[ 0 ]; + } + case SCHED_TAKE_COVER_FROM_ORIGIN: + { + return &slTakeCoverFromOrigin[ 0 ]; + } + case SCHED_VICTORY_DANCE: + { + return &slVictoryDance[ 0 ]; + } + case SCHED_FAIL: + { + return slFail; + } + default: + { + ALERT ( at_debug, "GetScheduleOfType()\nNo CASE for Schedule Type %d!\n", Type ); + + return &slIdleStand[ 0 ]; + break; + } + } + + return NULL; +} diff --git a/server/monsters/defaultai.h b/server/monsters/defaultai.h new file mode 100644 index 00000000..652d1085 --- /dev/null +++ b/server/monsters/defaultai.h @@ -0,0 +1,98 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#ifndef DEFAULTAI_H +#define DEFAULTAI_H + +//========================================================= +// Failed +//========================================================= +extern Schedule_t slFail[]; + +//========================================================= +// Idle Schedules +//========================================================= +extern Schedule_t slIdleStand[]; +extern Schedule_t slIdleTrigger[]; +extern Schedule_t slIdleWalk[]; + +//========================================================= +// Wake Schedules +//========================================================= +extern Schedule_t slWakeAngry[]; + +//========================================================= +// AlertTurn Schedules +//========================================================= +extern Schedule_t slAlertFace[]; + +//========================================================= +// AlertIdle Schedules +//========================================================= +extern Schedule_t slAlertStand[]; + +//========================================================= +// CombatIdle Schedule +//========================================================= +extern Schedule_t slCombatStand[]; + +//========================================================= +// CombatFace Schedule +//========================================================= +extern Schedule_t slCombatFace[]; + +//========================================================= +// reload schedule +//========================================================= +extern Schedule_t slReload[]; + +//========================================================= +// Attack Schedules +//========================================================= + +extern Schedule_t slRangeAttack1[]; +extern Schedule_t slRangeAttack2[]; + +extern Schedule_t slTakeCoverFromBestSound[]; + +// primary melee attack +extern Schedule_t slMeleeAttack[]; + +// Chase enemy schedule +extern Schedule_t slChaseEnemy[]; + +//========================================================= +// small flinch, used when a relatively minor bit of damage +// is inflicted. +//========================================================= +extern Schedule_t slSmallFlinch[]; + +//========================================================= +// Die! +//========================================================= +extern Schedule_t slDie[]; + +//========================================================= +// Universal Error Schedule +//========================================================= +extern Schedule_t slError[]; + +//========================================================= +// Scripted sequences +//========================================================= +extern Schedule_t slWalkToScript[]; +extern Schedule_t slRunToScript[]; +extern Schedule_t slWaitScript[]; + +#endif // DEFAULTAI_H diff --git a/server/monsters/flyingmonster.cpp b/server/monsters/flyingmonster.cpp new file mode 100644 index 00000000..cc475463 --- /dev/null +++ b/server/monsters/flyingmonster.cpp @@ -0,0 +1,281 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "flyingmonster.h" + +#define FLYING_AE_FLAP (8) +#define FLYING_AE_FLAPSOUND (9) + + +extern DLL_GLOBAL edict_t *g_pBodyQueueHead; + +int CFlyingMonster :: CheckLocalMove ( const Vector &vecStart, const Vector &vecEnd, CBaseEntity *pTarget, float *pflDist ) +{ + // UNDONE: need to check more than the endpoint + if (FBitSet( pev->flags, FL_SWIM) && (!(UTIL_PointContents( vecEnd ) & MASK_WATER ))) + { + // ALERT(at_aiconsole, "can't swim out of water\n"); + return FALSE; + } + + TraceResult tr; + + UTIL_TraceHull( vecStart + Vector( 0, 0, 32 ), vecEnd + Vector( 0, 0, 32 ), dont_ignore_monsters, large_hull, edict(), &tr ); + + // ALERT( at_console, "%.0f %.0f %.0f : ", vecStart.x, vecStart.y, vecStart.z ); + // ALERT( at_console, "%.0f %.0f %.0f\n", vecEnd.x, vecEnd.y, vecEnd.z ); + + if (pflDist) + { + *pflDist = ( (tr.vecEndPos - Vector( 0, 0, 32 )) - vecStart ).Length();// get the distance. + } + + // ALERT( at_console, "check %d %d %f\n", tr.fStartSolid, tr.fAllSolid, tr.flFraction ); + if (tr.fStartSolid || tr.flFraction < 1.0) + { + if ( pTarget && pTarget->edict() == gpGlobals->trace_ent ) + return LOCALMOVE_VALID; + return LOCALMOVE_INVALID; + } + + return LOCALMOVE_VALID; +} + + +BOOL CFlyingMonster :: FTriangulate ( const Vector &vecStart , const Vector &vecEnd, float flDist, CBaseEntity *pTargetEnt, Vector *pApex ) +{ + return CBaseMonster::FTriangulate( vecStart, vecEnd, flDist, pTargetEnt, pApex ); +} + + +Activity CFlyingMonster :: GetStoppedActivity( void ) +{ + if ( pev->movetype != MOVETYPE_FLY ) // UNDONE: Ground idle here, IDLE may be something else + return ACT_IDLE; + + return ACT_HOVER; +} + + +void CFlyingMonster :: Stop( void ) +{ + Activity stopped = GetStoppedActivity(); + if ( m_IdealActivity != stopped ) + { + m_flightSpeed = 0; + m_IdealActivity = stopped; + } + pev->angles.z = 0; + pev->angles.x = 0; + m_vecTravel = g_vecZero; +} + + +float CFlyingMonster :: ChangeYaw( int speed ) +{ + if ( pev->movetype == MOVETYPE_FLY ) + { + float diff = FlYawDiff(); + float target = 0; + + if ( m_IdealActivity != GetStoppedActivity() ) + { + if ( diff < -20 ) + target = 90; + else if ( diff > 20 ) + target = -90; + } + pev->angles.z = UTIL_Approach( target, pev->angles.z, 220.0 * gpGlobals->frametime ); + } + return CBaseMonster::ChangeYaw( speed ); +} + + +void CFlyingMonster :: Killed( entvars_t *pevAttacker, int iGib ) +{ + pev->movetype = MOVETYPE_STEP; + ClearBits( pev->flags, FL_ONGROUND ); + pev->angles.z = 0; + pev->angles.x = 0; + CBaseMonster::Killed( pevAttacker, iGib ); +} + + +void CFlyingMonster :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case FLYING_AE_FLAP: + m_flightSpeed = 400; + break; + + case FLYING_AE_FLAPSOUND: + if ( m_pFlapSound ) + EMIT_SOUND( edict(), CHAN_BODY, m_pFlapSound, 1, ATTN_NORM ); + break; + + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + + +void CFlyingMonster :: Move( float flInterval ) +{ + if ( pev->movetype == MOVETYPE_FLY ) + m_flGroundSpeed = m_flightSpeed; + CBaseMonster::Move( flInterval ); +} + + +BOOL CFlyingMonster:: ShouldAdvanceRoute( float flWaypointDist ) +{ + // Get true 3D distance to the goal so we actually reach the correct height + if ( m_Route[ m_iRouteIndex ].iType & bits_MF_IS_GOAL ) + flWaypointDist = ( m_Route[ m_iRouteIndex ].vecLocation - pev->origin ).Length(); + + if ( flWaypointDist <= 64 + (m_flGroundSpeed * gpGlobals->frametime) ) + return TRUE; + + return FALSE; +} + + +void CFlyingMonster::MoveExecute( CBaseEntity *pTargetEnt, const Vector &vecDir, float flInterval ) +{ + if ( pev->movetype == MOVETYPE_FLY ) + { + if ( gpGlobals->time - m_stopTime > 1.0 ) + { + if ( m_IdealActivity != m_movementActivity ) + { + m_IdealActivity = m_movementActivity; + m_flGroundSpeed = m_flightSpeed = 200; + } + } + Vector vecMove = pev->origin + (( vecDir + (m_vecTravel * m_momentum) ).Normalize() * (m_flGroundSpeed * flInterval)); + + if ( m_IdealActivity != m_movementActivity ) + { + m_flightSpeed = UTIL_Approach( 100, m_flightSpeed, 75 * gpGlobals->frametime ); + if ( m_flightSpeed < 100 ) + m_stopTime = gpGlobals->time; + } + else + m_flightSpeed = UTIL_Approach( 20, m_flightSpeed, 300 * gpGlobals->frametime ); + + if ( CheckLocalMove ( pev->origin, vecMove, pTargetEnt, NULL ) ) + { + m_vecTravel = (vecMove - pev->origin); + m_vecTravel = m_vecTravel.Normalize(); + UTIL_MoveToOrigin(ENT(pev), vecMove, (m_flGroundSpeed * flInterval), MOVE_STRAFE); + } + else + { + m_IdealActivity = GetStoppedActivity(); + m_stopTime = gpGlobals->time; + m_vecTravel = g_vecZero; + } + } + else + CBaseMonster::MoveExecute( pTargetEnt, vecDir, flInterval ); +} + + +float CFlyingMonster::CeilingZ( const Vector &position ) +{ + TraceResult tr; + + Vector minUp = position; + Vector maxUp = position; + maxUp.z += 4096.0; + + UTIL_TraceLine(position, maxUp, ignore_monsters, NULL, &tr); + if (tr.flFraction != 1.0) + maxUp.z = tr.vecEndPos.z; + + if( pev->flags & FL_SWIM ) + { + return UTIL_WaterLevel( position, minUp.z, maxUp.z ); + } + return maxUp.z; +} + +BOOL CFlyingMonster::ProbeZ( const Vector &position, const Vector &probe, float *pFraction) +{ + int conPosition = UTIL_PointContents( position ); + if ( ((pev->flags & FL_SWIM) == FL_SWIM) ^ (conPosition & CONTENTS_WATER)) + { + // SWIMING & !WATER + // or FLYING & WATER + // + *pFraction = 0.0; + return TRUE; // We hit a water boundary because we are where we don't belong. + } + int conProbe = UTIL_PointContents(probe); + if( conProbe == conPosition ) + { + // The probe is either entirely inside the water (for fish) or entirely + // outside the water (for birds). + // + *pFraction = 1.0; + return FALSE; + } + + Vector ProbeUnit = (probe-position).Normalize(); + float ProbeLength = (probe-position).Length(); + float maxProbeLength = ProbeLength; + float minProbeLength = 0; + + float diff = maxProbeLength - minProbeLength; + while (diff > 1.0) + { + float midProbeLength = minProbeLength + diff/2.0; + Vector midProbeVec = midProbeLength * ProbeUnit; + if( UTIL_PointContents( position + midProbeVec ) == conPosition ) + { + minProbeLength = midProbeLength; + } + else + { + maxProbeLength = midProbeLength; + } + diff = maxProbeLength - minProbeLength; + } + *pFraction = minProbeLength/ProbeLength; + + return TRUE; +} + +float CFlyingMonster::FloorZ( const Vector &position ) +{ + TraceResult tr; + + Vector down = position; + down.z -= 2048; + + UTIL_TraceLine( position, down, ignore_monsters, NULL, &tr ); + + if ( tr.flFraction != 1.0 ) + return tr.vecEndPos.z; + + return down.z; +} + diff --git a/server/monsters/flyingmonster.h b/server/monsters/flyingmonster.h new file mode 100644 index 00000000..616194db --- /dev/null +++ b/server/monsters/flyingmonster.h @@ -0,0 +1,53 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +// Base class for flying monsters. This overrides the movement test & execution code from CBaseMonster + +#ifndef FLYINGMONSTER_H +#define FLYINGMONSTER_H + +class CFlyingMonster : public CBaseMonster +{ +public: + int CheckLocalMove ( const Vector &vecStart, const Vector &vecEnd, CBaseEntity *pTarget, float *pflDist );// check validity of a straight move through space + BOOL FTriangulate ( const Vector &vecStart , const Vector &vecEnd, float flDist, CBaseEntity *pTargetEnt, Vector *pApex ); + Activity GetStoppedActivity( void ); + void Killed( entvars_t *pevAttacker, int iGib ); + void Stop( void ); + float ChangeYaw( int speed ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + void MoveExecute( CBaseEntity *pTargetEnt, const Vector &vecDir, float flInterval ); + void Move( float flInterval = 0.1 ); + BOOL ShouldAdvanceRoute( float flWaypointDist ); + + inline void SetFlyingMomentum( float momentum ) { m_momentum = momentum; } + inline void SetFlyingFlapSound( const char *pFlapSound ) { m_pFlapSound = pFlapSound; } + inline void SetFlyingSpeed( float speed ) { m_flightSpeed = speed; } + float CeilingZ( const Vector &position ); + float FloorZ( const Vector &position ); + BOOL ProbeZ( const Vector &position, const Vector &probe, float *pFraction ); + + + // UNDONE: Save/restore this stuff!!! +protected: + Vector m_vecTravel; // Current direction + float m_flightSpeed; // Current flight speed (decays when not flapping or gliding) + float m_stopTime; // Last time we stopped (to avoid switching states too soon) + float m_momentum; // Weight for desired vs. momentum velocity + const char *m_pFlapSound; +}; + + +#endif //FLYINGMONSTER_H + diff --git a/server/monsters/generic.cpp b/server/monsters/generic.cpp new file mode 100644 index 00000000..cfb5bdf3 --- /dev/null +++ b/server/monsters/generic.cpp @@ -0,0 +1,395 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// Generic Monster - purely for scripted sequence work. +//========================================================= +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "animation.h" +#include "talkmonster.h" +#include "basebeams.h" + +// For holograms, make them not solid so the player can walk through them +//LRC- this seems to interfere with SF_MONSTER_CLIP +#define SF_GENERICMONSTER_NOTSOLID 4 +#define SF_HEAD_CONTROLLER 8 +#define SF_GENERICMONSTER_INVULNERABLE 32 +#define SF_GENERICMONSTER_PLAYERMODEL 64 + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +//G-Cont. This code - support for htorch model from Op4 ;) +#define HTORCH_AE_SHOWGUN ( 17) +#define HTORCH_AE_SHOWTORCH ( 18) +#define HTORCH_AE_HIDETORCH ( 19) +#define HTORCH_AE_ONGAS ( 20) +#define HTORCH_AE_OFFGAS ( 21) +#define GUN_DEAGLE 0 +#define GUN_TORCH 1 +#define GUN_NONE 2 + +class CGenericMonster : public CTalkMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed( void ); + int Classify ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + int ISoundMask ( void ); + void KeyValue( KeyValueData *pkvd ); + void Torch ( void ); + void MakeGas( void ); + void UpdateGas( void ); + void KillGas( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + virtual int HasCustomGibs( void ) { return m_iszGibModel; } + + CBeam *m_pBeam; + int m_iszGibModel; +}; +LINK_ENTITY_TO_CLASS( monster_generic, CGenericMonster ); + +TYPEDESCRIPTION CGenericMonster::m_SaveData[] = +{ + DEFINE_FIELD( CGenericMonster, m_iszGibModel, FIELD_STRING ), +}; + +IMPLEMENT_SAVERESTORE( CGenericMonster, CBaseMonster ); + +void CGenericMonster::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "m_bloodColor")) + { + m_bloodColor = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszGibModel")) + { + m_iszGibModel = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseMonster::KeyValue( pkvd ); +} + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CGenericMonster :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_PLAYER_ALLY; +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CGenericMonster :: SetYawSpeed ( void ) +{ + int ys; + + switch ( m_Activity ) + { + case ACT_IDLE: + default: + ys = 90; + } + + pev->yaw_speed = ys; +} + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +//========================================================= +void CGenericMonster :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + Vector vecShootDir; + Vector vecShootOrigin; + + switch( pEvent->event ) + { + case HTORCH_AE_SHOWTORCH: + pev->body = GUN_NONE; + pev->body = GUN_TORCH; + break; + + case HTORCH_AE_SHOWGUN: + pev->body = GUN_NONE; + pev->body = GUN_DEAGLE; + break; + + case HTORCH_AE_HIDETORCH: + pev->body = GUN_NONE; + break; + + case HTORCH_AE_ONGAS: + { + int gas = 1; + MakeGas(); + UpdateGas(); + }; + break; + + case HTORCH_AE_OFFGAS: + { + int gas = 0; + KillGas(); + }; + break; + + default: + CBaseMonster::HandleAnimEvent( pEvent ); + } +} + +//========================================================= +// ISoundMask - generic monster can't hear. +//========================================================= +int CGenericMonster :: ISoundMask ( void ) +{ + return NULL; +} + +//========================================================= +// Spawn +//========================================================= +void CGenericMonster :: Spawn() +{ + Precache(); + + UTIL_SetModel( ENT(pev), STRING(pev->model) ); + + if ( FStrEq( STRING(pev->model), "models/player.mdl" ) || FStrEq( STRING(pev->model), "models/holo.mdl" )) + UTIL_SetSize(pev, VEC_HULL_MIN, VEC_HULL_MAX); + else UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX); + + //UTIL_AutoSetSize(); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + //pev->weaponmodel = MAKE_STRING("materials/weapons/pulserifle/wp_prifle.mdl"); + if (!m_bloodColor) m_bloodColor = BLOOD_COLOR_RED; + if (!pev->health) pev->health = 8; + m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + + MonsterInit(); + + m_afCapability = bits_CAP_TURN_HEAD; + + if ( pev->spawnflags & SF_GENERICMONSTER_NOTSOLID ) + { + pev->solid = SOLID_NOT; + pev->takedamage = DAMAGE_NO; + } + else if ( pev->spawnflags & SF_GENERICMONSTER_INVULNERABLE ) + { + pev->takedamage = DAMAGE_NO; + } +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CGenericMonster :: Precache() +{ + //PRECACHE_MODEL("materials/weapons/pulserifle/wp_prifle.mdl"); + CTalkMonster::Precache(); + TalkInit(); + UTIL_PrecacheModel((char *)STRING(pev->model) ); + if (m_iszGibModel) + PRECACHE_MODEL( (char*)STRING(m_iszGibModel) ); //LRC +} + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= + +//========================================================= +// monster-specific schedule types +//========================================================= +enum +{ + TASK_TORCH_CHECK_FIRE = LAST_COMMON_SCHEDULE + 1, + TASK_GAS, +}; + +// ========================================================= +// TORCH SUPPORT +// ========================================================= +void CGenericMonster :: Torch ( void ) +{ + Vector vecGunPos; + Vector vecGunAngles; + Vector vecShootDir; + + GetAttachment( 4, vecGunPos, vecGunAngles ); + pev->effects |= EF_MUZZLEFLASH; + + Vector angDir = UTIL_VecToAngles( vecShootDir ); + SetBlending( 0, angDir.x ); +} + +void CGenericMonster::UpdateGas( void ) { } + +void CGenericMonster::MakeGas( void ) +{ + Vector posGun, angleGun; + TraceResult tr; + UTIL_MakeVectors( pev->angles ); + { + KillGas(); + m_pBeam = CBeam::BeamCreate( "sprites/laserbeam.spr", 7 ); + if ( m_pBeam ) + { + GetAttachment( 4, posGun, angleGun ); + GetAttachment( 3, posGun, angleGun ); + + Vector vecEnd = (gpGlobals->v_forward * 5) + posGun; + UTIL_TraceLine( posGun, vecEnd, dont_ignore_monsters, edict(), &tr ); + + m_pBeam->EntsInit( entindex(), entindex() ); + m_pBeam->SetColor( 24, 121, 239 ); + m_pBeam->SetBrightness( 190 ); + m_pBeam->SetScrollRate( 20 ); + m_pBeam->SetStartAttachment( 4 ); + m_pBeam->SetEndAttachment( 3 ); + m_pBeam->DamageDecal( 28 ); + m_pBeam->DoSparks( tr.vecEndPos, posGun ); + m_pBeam->SetFlags( BEAM_FSHADEIN ); + m_pBeam->RelinkBeam(); + + UTIL_Sparks( tr.vecEndPos ); + UTIL_DecalTrace(&tr, 28 + RANDOM_LONG(0,4)); + } + } + // m_flNextAttack = gpGlobals->time + RANDOM_FLOAT( 0.5, 4.0 ); + if ( int gas = 1 ) + { + pev->nextthink = gpGlobals->time; + } +} + +void CGenericMonster :: KillGas( void ) +{ + if ( m_pBeam ) + { + UTIL_Remove( m_pBeam ); + m_pBeam = NULL; + } +} + +//========================================================= +// GENERIC DEAD MONSTER, PROP +//========================================================= +class CDeadGenericMonster : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + int Classify ( void ) { return CLASS_PLAYER_ALLY; } + void KeyValue( KeyValueData *pkvd ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + virtual int HasCustomGibs( void ) { return m_iszGibModel; } + + int m_iszGibModel; +}; + +LINK_ENTITY_TO_CLASS( monster_generic_dead, CDeadGenericMonster ); + +TYPEDESCRIPTION CDeadGenericMonster::m_SaveData[] = +{ + DEFINE_FIELD( CDeadGenericMonster, m_iszGibModel, FIELD_STRING ), +}; + +IMPLEMENT_SAVERESTORE( CDeadGenericMonster, CBaseMonster ); + +void CDeadGenericMonster::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "m_bloodColor")) + { + m_bloodColor = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszGibModel")) + { + m_iszGibModel = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseMonster::KeyValue( pkvd ); +} + +//========================================================= +// ********** DeadGenericMonster SPAWN ********** +//========================================================= +void CDeadGenericMonster :: Spawn( void ) +{ + Precache(); + SET_MODEL(ENT(pev), STRING(pev->model)); + + pev->effects = 0; + pev->yaw_speed = 8; //LRC -- what? + pev->sequence = 0; + + if (pev->netname) + { + pev->sequence = LookupSequence( STRING(pev->netname) ); + + if (pev->sequence == -1) + { + ALERT ( at_debug, "Invalid sequence name \"%s\" in monster_generic_dead\n", STRING(pev->netname) ); + } + } + else + { + pev->sequence = LookupActivity( pev->frags ); +// if (pev->sequence == -1) +// { +// ALERT ( at_error, "monster_generic_dead - specify a sequence name or choose a different death type: model \"%s\" has no available death sequences.\n", STRING(pev->model) ); +// } + //...and if that doesn't work, forget it. + } + + // Corpses have less health + pev->health = 8; + + MonsterInitDead(); + + ResetSequenceInfo( ); + pev->frame = 255; // pose at the _end_ of its death sequence. +} + +void CDeadGenericMonster :: Precache() +{ + PRECACHE_MODEL( (char*)STRING(pev->model) ); + if (m_iszGibModel) + PRECACHE_MODEL( (char*)STRING(m_iszGibModel) ); //LRC +} diff --git a/server/monsters/gman.cpp b/server/monsters/gman.cpp new file mode 100644 index 00000000..98ebcace --- /dev/null +++ b/server/monsters/gman.cpp @@ -0,0 +1,243 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// GMan - misunderstood servant of the people +//========================================================= +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "baseweapon.h" + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= + +class CGMan : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed( void ); + int Classify ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + int ISoundMask ( void ); + + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + void StartTask( Task_t *pTask ); + void RunTask( Task_t *pTask ); + int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ); + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + + void PlayScriptedSentence( const char *pszSentence, float duration, float volume, float attenuation, BOOL bConcurrent, CBaseEntity *pListener ); + + EHANDLE m_hPlayer; + EHANDLE m_hTalkTarget; + float m_flTalkTime; +}; +LINK_ENTITY_TO_CLASS( monster_gman, CGMan ); + + +TYPEDESCRIPTION CGMan::m_SaveData[] = +{ + DEFINE_FIELD( CGMan, m_hTalkTarget, FIELD_EHANDLE ), + DEFINE_FIELD( CGMan, m_flTalkTime, FIELD_TIME ), +}; +IMPLEMENT_SAVERESTORE( CGMan, CBaseMonster ); + + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CGMan :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_NONE; +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CGMan :: SetYawSpeed ( void ) +{ + int ys; + + switch ( m_Activity ) + { + case ACT_IDLE: + default: + ys = 90; + } + + pev->yaw_speed = ys; +} + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +//========================================================= +void CGMan :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case 0: + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + +//========================================================= +// ISoundMask - generic monster can't hear. +//========================================================= +int CGMan :: ISoundMask ( void ) +{ + return NULL; +} + +//========================================================= +// Spawn +//========================================================= +void CGMan :: Spawn() +{ + Precache(); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL( ENT(pev), "models/gman.mdl" ); + UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = DONT_BLEED; + pev->health = 100; + m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + + MonsterInit(); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CGMan :: Precache() +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL( "models/gman.mdl" ); +} + + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= + + +void CGMan :: StartTask( Task_t *pTask ) +{ + switch( pTask->iTask ) + { + case TASK_WAIT: + if (m_hPlayer == NULL) + { + m_hPlayer = UTIL_FindEntityByClassname( NULL, "player" ); + } + break; + } + CBaseMonster::StartTask( pTask ); +} + +void CGMan :: RunTask( Task_t *pTask ) +{ + switch( pTask->iTask ) + { + case TASK_WAIT: + // look at who I'm talking to + if (m_flTalkTime > gpGlobals->time && m_hTalkTarget != NULL) + { + float yaw = VecToYaw(m_hTalkTarget->pev->origin - pev->origin) - pev->angles.y; + + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + + // turn towards vector + SetBoneController( 0, yaw ); + } + // look at player, but only if playing a "safe" idle animation + else if (m_hPlayer != NULL && pev->sequence == 0) + { + float yaw = VecToYaw(m_hPlayer->pev->origin - pev->origin) - pev->angles.y; + + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + + // turn towards vector + SetBoneController( 0, yaw ); + } + else + { + SetBoneController( 0, 0 ); + } + CBaseMonster::RunTask( pTask ); + break; + default: + SetBoneController( 0, 0 ); + CBaseMonster::RunTask( pTask ); + break; + } +} + + +//========================================================= +// Override all damage +//========================================================= +int CGMan :: TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ) +{ + pev->health = pev->max_health / 2; // always trigger the 50% damage aitrigger + + if ( flDamage > 0 ) + { + SetConditions(bits_COND_LIGHT_DAMAGE); + } + + if ( flDamage >= 20 ) + { + SetConditions(bits_COND_HEAVY_DAMAGE); + } + return TRUE; +} + + +void CGMan::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + UTIL_Ricochet( ptr->vecEndPos, 1.0 ); + AddMultiDamage( pevAttacker, this, flDamage, bitsDamageType ); +} + + +void CGMan::PlayScriptedSentence( const char *pszSentence, float duration, float volume, float attenuation, BOOL bConcurrent, CBaseEntity *pListener ) +{ + CBaseMonster::PlayScriptedSentence( pszSentence, duration, volume, attenuation, bConcurrent, pListener ); + + m_flTalkTime = gpGlobals->time + duration; + m_hTalkTarget = pListener; +} diff --git a/server/monsters/hassassin.cpp b/server/monsters/hassassin.cpp new file mode 100644 index 00000000..29ec650b --- /dev/null +++ b/server/monsters/hassassin.cpp @@ -0,0 +1,1068 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#if !defined( OEM_BUILD ) && !defined( HLDEMO_BUILD ) + +//========================================================= +// hassassin - Human assassin, fast and stealthy +//========================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "squadmonster.h" +#include "baseweapon.h" +#include "soundent.h" +#include "scripted.h" +#include "game.h" +#include "defaults.h" + +//========================================================= +// monster-specific schedule types +//========================================================= +enum +{ + SCHED_ASSASSIN_EXPOSED = LAST_COMMON_SCHEDULE + 1,// cover was blown. + SCHED_ASSASSIN_JUMP, // fly through the air + SCHED_ASSASSIN_JUMP_ATTACK, // fly through the air and shoot + SCHED_ASSASSIN_JUMP_LAND, // hit and run away +}; + +//========================================================= +// monster-specific tasks +//========================================================= + +enum +{ + TASK_ASSASSIN_FALL_TO_GROUND = LAST_COMMON_TASK + 1, // falling and waiting to hit ground +}; + + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +#define ASSASSIN_AE_SHOOT1 1 +#define ASSASSIN_AE_TOSS1 2 +#define ASSASSIN_AE_JUMP 3 + + +#define bits_MEMORY_BADJUMP (bits_MEMORY_CUSTOM1) + +class CHAssassin : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed ( void ); + int Classify ( void ); + int ISoundMask ( void); + void Shoot( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + Schedule_t* GetSchedule ( void ); + Schedule_t* GetScheduleOfType ( int Type ); + BOOL CheckMeleeAttack1 ( float flDot, float flDist ); // jump + // BOOL CheckMeleeAttack2 ( float flDot, float flDist ); + BOOL CheckRangeAttack1 ( float flDot, float flDist ); // shoot + BOOL CheckRangeAttack2 ( float flDot, float flDist ); // throw grenade + void StartTask ( Task_t *pTask ); + void RunAI( void ); + void RunTask ( Task_t *pTask ); + void DeathSound ( void ); + void IdleSound ( void ); + CUSTOM_SCHEDULES; + + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + float m_flLastShot; + float m_flDiviation; + + float m_flNextJump; + Vector m_vecJumpVelocity; + + float m_flNextGrenadeCheck; + Vector m_vecTossVelocity; + BOOL m_fThrowGrenade; + + int m_iTargetRanderamt; + + int m_iFrustration; + + int m_iShell; +}; +LINK_ENTITY_TO_CLASS( monster_human_assassin, CHAssassin ); + + +TYPEDESCRIPTION CHAssassin::m_SaveData[] = +{ + DEFINE_FIELD( CHAssassin, m_flLastShot, FIELD_TIME ), + DEFINE_FIELD( CHAssassin, m_flDiviation, FIELD_FLOAT ), + + DEFINE_FIELD( CHAssassin, m_flNextJump, FIELD_TIME ), + DEFINE_FIELD( CHAssassin, m_vecJumpVelocity, FIELD_VECTOR ), + + DEFINE_FIELD( CHAssassin, m_flNextGrenadeCheck, FIELD_TIME ), + DEFINE_FIELD( CHAssassin, m_vecTossVelocity, FIELD_VECTOR ), + DEFINE_FIELD( CHAssassin, m_fThrowGrenade, FIELD_BOOLEAN ), + + DEFINE_FIELD( CHAssassin, m_iTargetRanderamt, FIELD_INTEGER ), + DEFINE_FIELD( CHAssassin, m_iFrustration, FIELD_INTEGER ), +}; + +IMPLEMENT_SAVERESTORE( CHAssassin, CBaseMonster ); + + +//========================================================= +// DieSound +//========================================================= +void CHAssassin :: DeathSound ( void ) +{ +} + +//========================================================= +// IdleSound +//========================================================= +void CHAssassin :: IdleSound ( void ) +{ +} + +//========================================================= +// ISoundMask - returns a bit mask indicating which types +// of sounds this monster regards. +//========================================================= +int CHAssassin :: ISoundMask ( void) +{ + return bits_SOUND_WORLD | + bits_SOUND_COMBAT | + bits_SOUND_DANGER | + bits_SOUND_PLAYER; +} + + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CHAssassin :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_HUMAN_MILITARY; +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CHAssassin :: SetYawSpeed ( void ) +{ + int ys; + + switch ( m_Activity ) + { + case ACT_TURN_LEFT: + case ACT_TURN_RIGHT: + ys = 360; + break; + default: + ys = 360; + break; + } + + pev->yaw_speed = ys; +} + + +//========================================================= +// Shoot +//========================================================= +void CHAssassin :: Shoot ( void ) +{ + if (m_hEnemy == NULL && !m_pCine) //LRC + { + return; + } + + Vector vecShootOrigin = GetGunPosition(); + Vector vecShootDir = ShootAtEnemy( vecShootOrigin ); + + if (m_flLastShot + 2 < gpGlobals->time) + { + m_flDiviation = 0.10; + } + else + { + m_flDiviation -= 0.01; + if (m_flDiviation < 0.02) + m_flDiviation = 0.02; + } + m_flLastShot = gpGlobals->time; + + UTIL_MakeVectors ( pev->angles ); + + Vector vecShellVelocity = gpGlobals->v_right * RANDOM_FLOAT(40,90) + gpGlobals->v_up * RANDOM_FLOAT(75,200) + gpGlobals->v_forward * RANDOM_FLOAT(-40, 40); + EjectBrass ( pev->origin + gpGlobals->v_up * 32 + gpGlobals->v_forward * 12, vecShellVelocity, pev->angles.y, m_iShell, TE_BOUNCE_SHELL); + FireBullets(1, vecShootOrigin, vecShootDir, Vector( m_flDiviation, m_flDiviation, m_flDiviation ), 2048, BULLET_9MM ); // shoot +-8 degrees + + switch(RANDOM_LONG(0,1)) + { + case 0: + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/pl_gun1.wav", RANDOM_FLOAT(0.6, 0.8), ATTN_NORM); + break; + case 1: + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/pl_gun2.wav", RANDOM_FLOAT(0.6, 0.8), ATTN_NORM); + break; + } + + pev->effects |= EF_MUZZLEFLASH; + + Vector angDir = UTIL_VecToAngles( vecShootDir ); + SetBlending( 0, angDir.x ); + + m_cAmmoLoaded--; +} + + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +// +// Returns number of events handled, 0 if none. +//========================================================= +void CHAssassin :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case ASSASSIN_AE_SHOOT1: + Shoot( ); + break; + case ASSASSIN_AE_TOSS1: + { + Vector vecGunPosition = pev->origin + gpGlobals->v_forward * 34 + Vector (0, 0, 32); + UTIL_MakeVectors( pev->angles ); + //LRC + if (m_pCine && m_pCine->IsAction()) + { + Vector vecToss; + if (m_pCine->PreciseAttack() && m_hTargetEnt != NULL) + { + vecToss = VecCheckToss( pev, vecGunPosition, m_hTargetEnt->pev->origin, 0.5 ); + //if (vecToss != g_vecZero) + // ALERT(at_console,"Assassin %s throws precise grenade\n",STRING(pev->targetname)); + } + else + { + //ALERT(at_console,"Assassin %s throws nonprecise grenade\n",STRING(pev->targetname)); + // what speed would be best to use, here? Borrowing the hgrunt grenade speed seems silly... + vecToss = ((gpGlobals->v_forward*0.5)+(gpGlobals->v_up*0.5)).Normalize()*HGRUNT_GRENADE_SPEED; + } + CGrenade::ShootTimed( pev, vecGunPosition, vecToss, 2.0 ); + } + else + CGrenade::ShootTimed( pev, vecGunPosition, m_vecTossVelocity, 2.0 ); + + m_flNextGrenadeCheck = gpGlobals->time + 6;// wait six seconds before even looking again to see if a grenade can be thrown. + m_fThrowGrenade = FALSE; + // !!!LATER - when in a group, only try to throw grenade if ordered. + } + break; + case ASSASSIN_AE_JUMP: + { + // ALERT( at_console, "jumping"); + UTIL_MakeAimVectors( pev->angles ); + pev->movetype = MOVETYPE_TOSS; + pev->flags &= ~FL_ONGROUND; + if (m_pCine) //LRC... + { + pev->velocity = g_vecZero; + if (m_pCine->PreciseAttack() && m_hTargetEnt != NULL) + { + Vector vecTemp = m_hTargetEnt->pev->origin; + vecTemp.y = vecTemp.y + 50; // put her feet on the target. + pev->velocity = VecCheckToss( pev, pev->origin, vecTemp, 0.5 ); + //if (pev->velocity != g_vecZero) + // ALERT(at_console,"Precise jump for assassin %s\n",STRING(pev->targetname)); + //else + // ALERT(at_console,"Precise jump failed. "); + } + if (pev->velocity == g_vecZero) + { // just jump, it doesn't matter where to. + //ALERT(at_console,"Nonprecise jump for assassin %s\n",STRING(pev->targetname)); + float flGravity = CVAR_GET_FLOAT( "sv_gravity" ); + float time = sqrt( 160 / (0.5 * flGravity)); + float speed = flGravity * time / 160; + UTIL_MakeVectors(pev->angles); + Vector vecDest = pev->origin + (gpGlobals->v_forward * 32); + vecDest.z += 160; // don't forget to jump into the air, now... + pev->velocity= (vecDest - pev->origin) * speed; + } + } + else + pev->velocity = m_vecJumpVelocity; + m_flNextJump = gpGlobals->time + 3.0; + } + return; + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + +//========================================================= +// Spawn +//========================================================= +void CHAssassin :: Spawn() +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/hassassin.mdl"); + UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_RED; + pev->effects = 0; + if (pev->health == 0) + pev->health = HASSASSIN_HEALTH; + m_flFieldOfView = VIEW_FIELD_WIDE; // indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + m_afCapability = bits_CAP_MELEE_ATTACK1 | bits_CAP_DOORS_GROUP; + pev->friction = 1; + + m_HackedGunPos = Vector( 0, 24, 48 ); + + m_iTargetRanderamt = 20; + pev->renderamt = 20; + pev->rendermode = kRenderTransTexture; + + MonsterInit(); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CHAssassin :: Precache() +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/hassassin.mdl"); + + PRECACHE_SOUND("weapons/pl_gun1.wav"); + PRECACHE_SOUND("weapons/pl_gun2.wav"); + + PRECACHE_SOUND("debris/beamstart1.wav"); + + m_iShell = UTIL_PrecacheModel ("models/shell556.mdl");// brass shell +} + + + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= + +//========================================================= +// Fail Schedule +//========================================================= +Task_t tlAssassinFail[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT_FACE_ENEMY, (float)2 }, + // { TASK_WAIT_PVS, (float)0 }, + { TASK_SET_SCHEDULE, (float)SCHED_CHASE_ENEMY }, +}; + +Schedule_t slAssassinFail[] = +{ + { + tlAssassinFail, + ARRAYSIZE ( tlAssassinFail ), + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_PROVOKED | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_CAN_MELEE_ATTACK1 | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER | + bits_SOUND_PLAYER, + "AssassinFail" + }, +}; + + +//========================================================= +// Enemy exposed Agrunt's cover +//========================================================= +Task_t tlAssassinExposed[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_ASSASSIN_JUMP }, + { TASK_SET_SCHEDULE, (float)SCHED_TAKE_COVER_FROM_ENEMY }, +}; + +Schedule_t slAssassinExposed[] = +{ + { + tlAssassinExposed, + ARRAYSIZE ( tlAssassinExposed ), + bits_COND_CAN_MELEE_ATTACK1, + 0, + "AssassinExposed", + }, +}; + + +//========================================================= +// Take cover from enemy! Tries lateral cover before node +// cover! +//========================================================= +Task_t tlAssassinTakeCoverFromEnemy[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_WAIT, (float)0.2 }, + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_RANGE_ATTACK1 }, + { TASK_FIND_COVER_FROM_ENEMY, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_FACE_ENEMY, (float)0 }, +}; + +Schedule_t slAssassinTakeCoverFromEnemy[] = +{ + { + tlAssassinTakeCoverFromEnemy, + ARRAYSIZE ( tlAssassinTakeCoverFromEnemy ), + bits_COND_NEW_ENEMY | + bits_COND_CAN_MELEE_ATTACK1 | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "AssassinTakeCoverFromEnemy" + }, +}; + + +//========================================================= +// Take cover from enemy! Tries lateral cover before node +// cover! +//========================================================= +Task_t tlAssassinTakeCoverFromEnemy2[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_WAIT, (float)0.2 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_RANGE_ATTACK2 }, + { TASK_FIND_FAR_NODE_COVER_FROM_ENEMY, (float)384 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_FACE_ENEMY, (float)0 }, +}; + +Schedule_t slAssassinTakeCoverFromEnemy2[] = +{ + { + tlAssassinTakeCoverFromEnemy2, + ARRAYSIZE ( tlAssassinTakeCoverFromEnemy2 ), + bits_COND_NEW_ENEMY | + bits_COND_CAN_MELEE_ATTACK2 | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "AssassinTakeCoverFromEnemy2" + }, +}; + + +//========================================================= +// hide from the loudest sound source +//========================================================= +Task_t tlAssassinTakeCoverFromBestSound[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_MELEE_ATTACK1 }, + { TASK_STOP_MOVING, (float)0 }, + { TASK_FIND_COVER_FROM_BEST_SOUND, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_TURN_LEFT, (float)179 }, +}; + +Schedule_t slAssassinTakeCoverFromBestSound[] = +{ + { + tlAssassinTakeCoverFromBestSound, + ARRAYSIZE ( tlAssassinTakeCoverFromBestSound ), + bits_COND_NEW_ENEMY, + 0, + "AssassinTakeCoverFromBestSound" + }, +}; + + + + + +//========================================================= +// AlertIdle Schedules +//========================================================= +Task_t tlAssassinHide[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT, (float)2 }, + { TASK_SET_SCHEDULE, (float)SCHED_CHASE_ENEMY }, +}; + +Schedule_t slAssassinHide[] = +{ + { + tlAssassinHide, + ARRAYSIZE ( tlAssassinHide ), + bits_COND_NEW_ENEMY | + bits_COND_SEE_ENEMY | + bits_COND_SEE_FEAR | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_PROVOKED | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "AssassinHide" + }, +}; + + + +//========================================================= +// HUNT Schedules +//========================================================= +Task_t tlAssassinHunt[] = +{ + { TASK_GET_PATH_TO_ENEMY, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, +}; + +Schedule_t slAssassinHunt[] = +{ + { + tlAssassinHunt, + ARRAYSIZE ( tlAssassinHunt ), + bits_COND_NEW_ENEMY | + // bits_COND_SEE_ENEMY | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "AssassinHunt" + }, +}; + + +//========================================================= +// Jumping Schedules +//========================================================= +Task_t tlAssassinJump[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_HOP }, + { TASK_SET_SCHEDULE, (float)SCHED_ASSASSIN_JUMP_ATTACK }, +}; + +Schedule_t slAssassinJump[] = +{ + { + tlAssassinJump, + ARRAYSIZE ( tlAssassinJump ), + 0, + 0, + "AssassinJump" + }, +}; + + +//========================================================= +// repel +//========================================================= +Task_t tlAssassinJumpAttack[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_ASSASSIN_JUMP_LAND }, + // { TASK_SET_ACTIVITY, (float)ACT_FLY }, + { TASK_ASSASSIN_FALL_TO_GROUND, (float)0 }, +}; + + +Schedule_t slAssassinJumpAttack[] = +{ + { + tlAssassinJumpAttack, + ARRAYSIZE ( tlAssassinJumpAttack ), + 0, + 0, + "AssassinJumpAttack" + }, +}; + + +//========================================================= +// repel +//========================================================= +Task_t tlAssassinJumpLand[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_ASSASSIN_EXPOSED }, + // { TASK_SET_FAIL_SCHEDULE, (float)SCHED_MELEE_ATTACK1 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_REMEMBER, (float)bits_MEMORY_BADJUMP }, + { TASK_FIND_NODE_COVER_FROM_ENEMY, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_FORGET, (float)bits_MEMORY_BADJUMP }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_RANGE_ATTACK1 }, +}; + +Schedule_t slAssassinJumpLand[] = +{ + { + tlAssassinJumpLand, + ARRAYSIZE ( tlAssassinJumpLand ), + 0, + 0, + "AssassinJumpLand" + }, +}; + +DEFINE_CUSTOM_SCHEDULES( CHAssassin ) +{ + slAssassinFail, + slAssassinExposed, + slAssassinTakeCoverFromEnemy, + slAssassinTakeCoverFromEnemy2, + slAssassinTakeCoverFromBestSound, + slAssassinHide, + slAssassinHunt, + slAssassinJump, + slAssassinJumpAttack, + slAssassinJumpLand, +}; + +IMPLEMENT_CUSTOM_SCHEDULES( CHAssassin, CBaseMonster ); + + +//========================================================= +// CheckMeleeAttack1 - jump like crazy if the enemy gets too close. +//========================================================= +BOOL CHAssassin :: CheckMeleeAttack1 ( float flDot, float flDist ) +{ + if ( m_flNextJump < gpGlobals->time && (flDist <= 128 || HasMemory( bits_MEMORY_BADJUMP )) && m_hEnemy != NULL ) + { + TraceResult tr; + + Vector vecDest = pev->origin + Vector( RANDOM_FLOAT( -64, 64), RANDOM_FLOAT( -64, 64 ), 160 ); + + UTIL_TraceHull( pev->origin + Vector( 0, 0, 36 ), vecDest + Vector( 0, 0, 36 ), dont_ignore_monsters, human_hull, ENT(pev), &tr); + + if ( tr.fStartSolid || tr.flFraction < 1.0) + { + return FALSE; + } + + float flGravity = CVAR_GET_FLOAT( "sv_gravity" ); + + float time = sqrt( 160 / (0.5 * flGravity)); + float speed = flGravity * time / 160; + m_vecJumpVelocity = (vecDest - pev->origin) * speed; + + return TRUE; + } + return FALSE; +} + +//========================================================= +// CheckRangeAttack1 - drop a cap in their ass +// +//========================================================= +BOOL CHAssassin :: CheckRangeAttack1 ( float flDot, float flDist ) +{ + if ( !HasConditions( bits_COND_ENEMY_OCCLUDED ) && flDist > 64 && flDist <= 2048 /* && flDot >= 0.5 */ /* && NoFriendlyFire() */ ) + { + TraceResult tr; + + Vector vecSrc = GetGunPosition(); + + // verify that a bullet fired from the gun will hit the enemy before the world. + UTIL_TraceLine( vecSrc, m_hEnemy->BodyTarget(vecSrc), dont_ignore_monsters, ENT(pev), &tr); + + if ( tr.flFraction == 1 || tr.pHit == m_hEnemy->edict() ) + { + return TRUE; + } + } + return FALSE; +} + +//========================================================= +// CheckRangeAttack2 - toss grenade is enemy gets in the way and is too close. +//========================================================= +BOOL CHAssassin :: CheckRangeAttack2 ( float flDot, float flDist ) +{ + m_fThrowGrenade = FALSE; + if ( !FBitSet ( m_hEnemy->pev->flags, FL_ONGROUND ) ) + { + // don't throw grenades at anything that isn't on the ground! + return FALSE; + } + + // don't get grenade happy unless the player starts to piss you off + if ( m_iFrustration <= 2) + return FALSE; + + if ( m_flNextGrenadeCheck < gpGlobals->time && !HasConditions( bits_COND_ENEMY_OCCLUDED ) && flDist <= 512 /* && flDot >= 0.5 */ /* && NoFriendlyFire() */ ) + { + Vector vecToss = VecCheckThrow( pev, GetGunPosition( ), m_hEnemy->Center(), flDist, 0.5 ); // use dist as speed to get there in 1 second + + if ( vecToss != g_vecZero ) + { + m_vecTossVelocity = vecToss; + + // throw a hand grenade + m_fThrowGrenade = TRUE; + + return TRUE; + } + } + + return FALSE; +} + + +//========================================================= +// RunAI +//========================================================= +void CHAssassin :: RunAI( void ) +{ + CBaseMonster :: RunAI(); + + // always visible if moving + // always visible is not on hard + if (m_hEnemy == NULL || pev->deadflag != DEAD_NO || m_Activity == ACT_RUN || m_Activity == ACT_WALK || !(pev->flags & FL_ONGROUND)) + m_iTargetRanderamt = 255; + else + m_iTargetRanderamt = 20; + + if (pev->renderamt > m_iTargetRanderamt) + { + if (pev->renderamt == 255) + { + EMIT_SOUND (ENT(pev), CHAN_BODY, "debris/beamstart1.wav", 0.2, ATTN_NORM ); + } + + pev->renderamt = max( pev->renderamt - 50, m_iTargetRanderamt ); + pev->rendermode = kRenderTransTexture; + } + else if (pev->renderamt < m_iTargetRanderamt) + { + pev->renderamt = min( pev->renderamt + 50, m_iTargetRanderamt ); + if (pev->renderamt == 255) + pev->rendermode = kRenderNormal; + } + + if (m_Activity == ACT_RUN || m_Activity == ACT_WALK) + { + static int iStep = 0; + iStep = ! iStep; + if (iStep) + { + switch( RANDOM_LONG( 0, 3 ) ) + { + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step1.wav", 0.5, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step3.wav", 0.5, ATTN_NORM); break; + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step2.wav", 0.5, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step4.wav", 0.5, ATTN_NORM); break; + } + } + } +} + + +//========================================================= +// StartTask +//========================================================= +void CHAssassin :: StartTask ( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_RANGE_ATTACK2: + if (!m_fThrowGrenade) + { + TaskComplete( ); + } + else + { + CBaseMonster :: StartTask ( pTask ); + } + break; + case TASK_ASSASSIN_FALL_TO_GROUND: + break; + default: + CBaseMonster :: StartTask ( pTask ); + break; + } +} + + +//========================================================= +// RunTask +//========================================================= +void CHAssassin :: RunTask ( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_ASSASSIN_FALL_TO_GROUND: + MakeIdealYaw( m_vecEnemyLKP ); + ChangeYaw( pev->yaw_speed ); + + if (m_fSequenceFinished) + { + if (pev->velocity.z > 0) + { + pev->sequence = LookupSequence( "fly_up" ); + } + else if (HasConditions ( bits_COND_SEE_ENEMY )) + { + pev->sequence = LookupSequence( "fly_attack" ); + pev->frame = 0; + } + else + { + pev->sequence = LookupSequence( "fly_down" ); + pev->frame = 0; + } + + ResetSequenceInfo( ); + SetYawSpeed(); + } + if (pev->flags & FL_ONGROUND) + { + // ALERT( at_console, "on ground\n"); + TaskComplete( ); + } + break; + default: + CBaseMonster :: RunTask ( pTask ); + break; + } +} + +//========================================================= +// GetSchedule - Decides which type of schedule best suits +// the monster's current state and conditions. Then calls +// monster's member function to get a pointer to a schedule +// of the proper type. +//========================================================= +Schedule_t *CHAssassin :: GetSchedule ( void ) +{ + switch ( m_MonsterState ) + { + case MONSTERSTATE_IDLE: + case MONSTERSTATE_ALERT: + { + if ( HasConditions ( bits_COND_HEAR_SOUND )) + { + CSound *pSound; + pSound = PBestSound(); + + ASSERT( pSound != NULL ); + if ( pSound && (pSound->m_iType & bits_SOUND_DANGER) ) + { + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_BEST_SOUND ); + } + if ( pSound && (pSound->m_iType & bits_SOUND_COMBAT) ) + { + return GetScheduleOfType( SCHED_INVESTIGATE_SOUND ); + } + } + } + break; + + case MONSTERSTATE_COMBAT: + { +// dead enemy + if ( HasConditions( bits_COND_ENEMY_DEAD ) ) + { + // call base class, all code to handle dead enemies is centralized there. + return CBaseMonster :: GetSchedule(); + } + + // flying? + if ( pev->movetype == MOVETYPE_TOSS) + { + if (pev->flags & FL_ONGROUND) + { + // ALERT( at_console, "landed\n"); + // just landed + pev->movetype = MOVETYPE_STEP; + return GetScheduleOfType ( SCHED_ASSASSIN_JUMP_LAND ); + } + else + { + // ALERT( at_console, "jump\n"); + // jump or jump/shoot + if ( m_MonsterState == MONSTERSTATE_COMBAT ) + return GetScheduleOfType ( SCHED_ASSASSIN_JUMP ); + else + return GetScheduleOfType ( SCHED_ASSASSIN_JUMP_ATTACK ); + } + } + + if ( HasConditions ( bits_COND_HEAR_SOUND )) + { + CSound *pSound; + pSound = PBestSound(); + + ASSERT( pSound != NULL ); + if ( pSound && (pSound->m_iType & bits_SOUND_DANGER) ) + { + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_BEST_SOUND ); + } + } + + if ( HasConditions ( bits_COND_LIGHT_DAMAGE ) ) + { + m_iFrustration++; + } + if ( HasConditions ( bits_COND_HEAVY_DAMAGE ) ) + { + m_iFrustration++; + } + + // jump player! + if ( HasConditions ( bits_COND_CAN_MELEE_ATTACK1 ) ) + { + // ALERT( at_console, "melee attack 1\n"); + return GetScheduleOfType ( SCHED_MELEE_ATTACK1 ); + } + + // throw grenade + if ( HasConditions ( bits_COND_CAN_RANGE_ATTACK2 ) ) + { + // ALERT( at_console, "range attack 2\n"); + return GetScheduleOfType ( SCHED_RANGE_ATTACK2 ); + } + + // spotted + if ( HasConditions ( bits_COND_SEE_ENEMY ) && HasConditions ( bits_COND_ENEMY_FACING_ME ) ) + { + // ALERT( at_console, "exposed\n"); + m_iFrustration++; + return GetScheduleOfType ( SCHED_ASSASSIN_EXPOSED ); + } + + // can attack + if ( HasConditions ( bits_COND_CAN_RANGE_ATTACK1 ) ) + { + // ALERT( at_console, "range attack 1\n"); + m_iFrustration = 0; + return GetScheduleOfType ( SCHED_RANGE_ATTACK1 ); + } + + if ( HasConditions ( bits_COND_SEE_ENEMY ) ) + { + // ALERT( at_console, "face\n"); + return GetScheduleOfType ( SCHED_COMBAT_FACE ); + } + + // new enemy + if ( HasConditions ( bits_COND_NEW_ENEMY ) ) + { + // ALERT( at_console, "take cover\n"); + return GetScheduleOfType ( SCHED_TAKE_COVER_FROM_ENEMY ); + } + + // ALERT( at_console, "stand\n"); + return GetScheduleOfType ( SCHED_ALERT_STAND ); + } + break; + } + + return CBaseMonster :: GetSchedule(); +} + +//========================================================= +//========================================================= +Schedule_t* CHAssassin :: GetScheduleOfType ( int Type ) +{ + // ALERT( at_console, "%d\n", m_iFrustration ); + switch ( Type ) + { + case SCHED_TAKE_COVER_FROM_ENEMY: + if (pev->health > 30) + return slAssassinTakeCoverFromEnemy; + else + return slAssassinTakeCoverFromEnemy2; + case SCHED_TAKE_COVER_FROM_BEST_SOUND: + return slAssassinTakeCoverFromBestSound; + case SCHED_ASSASSIN_EXPOSED: + return slAssassinExposed; + case SCHED_FAIL: + if (m_MonsterState == MONSTERSTATE_COMBAT) + return slAssassinFail; + break; + case SCHED_ALERT_STAND: + if (m_MonsterState == MONSTERSTATE_COMBAT) + return slAssassinHide; + break; + case SCHED_CHASE_ENEMY: + return slAssassinHunt; + case SCHED_MELEE_ATTACK1: + if (pev->flags & FL_ONGROUND) + { + if (m_flNextJump > gpGlobals->time) + { + // can't jump yet, go ahead and fail + return slAssassinFail; + } + else + { + return slAssassinJump; + } + } + else + { + return slAssassinJumpAttack; + } + case SCHED_ASSASSIN_JUMP: + case SCHED_ASSASSIN_JUMP_ATTACK: + return slAssassinJumpAttack; + case SCHED_ASSASSIN_JUMP_LAND: + return slAssassinJumpLand; + } + + return CBaseMonster :: GetScheduleOfType( Type ); +} + +#endif diff --git a/server/monsters/headcrab.cpp b/server/monsters/headcrab.cpp new file mode 100644 index 00000000..7a1d3bd2 --- /dev/null +++ b/server/monsters/headcrab.cpp @@ -0,0 +1,569 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// headcrab.cpp - tiny, jumpy alien parasite +//========================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "game.h" +#include "defaults.h" + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +#define HC_AE_JUMPATTACK ( 2 ) + +Task_t tlHCRangeAttack1[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_WAIT_RANDOM, (float)0.5 }, +}; + +Schedule_t slHCRangeAttack1[] = +{ + { + tlHCRangeAttack1, + ARRAYSIZE ( tlHCRangeAttack1 ), + bits_COND_ENEMY_OCCLUDED | + bits_COND_NO_AMMO_LOADED, + 0, + "HCRangeAttack1" + }, +}; + +Task_t tlHCRangeAttack1Fast[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, +}; + +Schedule_t slHCRangeAttack1Fast[] = +{ + { + tlHCRangeAttack1Fast, + ARRAYSIZE ( tlHCRangeAttack1Fast ), + bits_COND_ENEMY_OCCLUDED | + bits_COND_NO_AMMO_LOADED, + 0, + "HCRAFast" + }, +}; + +class CHeadCrab : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void RunTask ( Task_t *pTask ); + void StartTask ( Task_t *pTask ); + void SetYawSpeed ( void ); + void EXPORT LeapTouch ( CBaseEntity *pOther ); + Vector Center( void ); + Vector BodyTarget( const Vector &posSrc ); + void PainSound( void ); + void DeathSound( void ); + void IdleSound( void ); + void AlertSound( void ); + void PrescheduleThink( void ); + int Classify ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + BOOL CheckRangeAttack1 ( float flDot, float flDist ); + BOOL CheckRangeAttack2 ( float flDot, float flDist ); + int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ); + + virtual float GetDamageAmount( void ) { return HEADCRAB_DMG_BITE; } + virtual int GetVoicePitch( void ) { return 100; } + virtual float GetSoundVolue( void ) { return 1.0; } + Schedule_t* GetScheduleOfType ( int Type ); + + CUSTOM_SCHEDULES; + + static const char *pIdleSounds[]; + static const char *pAlertSounds[]; + static const char *pPainSounds[]; + static const char *pAttackSounds[]; + static const char *pDeathSounds[]; + static const char *pBiteSounds[]; +}; +LINK_ENTITY_TO_CLASS( monster_headcrab, CHeadCrab ); + +DEFINE_CUSTOM_SCHEDULES( CHeadCrab ) +{ + slHCRangeAttack1, + slHCRangeAttack1Fast, +}; + +IMPLEMENT_CUSTOM_SCHEDULES( CHeadCrab, CBaseMonster ); + +const char *CHeadCrab::pIdleSounds[] = +{ + "headcrab/hc_idle1.wav", + "headcrab/hc_idle2.wav", + "headcrab/hc_idle3.wav", +}; +const char *CHeadCrab::pAlertSounds[] = +{ + "headcrab/hc_alert1.wav", +}; +const char *CHeadCrab::pPainSounds[] = +{ + "headcrab/hc_pain1.wav", + "headcrab/hc_pain2.wav", + "headcrab/hc_pain3.wav", +}; +const char *CHeadCrab::pAttackSounds[] = +{ + "headcrab/hc_attack1.wav", + "headcrab/hc_attack2.wav", + "headcrab/hc_attack3.wav", +}; + +const char *CHeadCrab::pDeathSounds[] = +{ + "headcrab/hc_die1.wav", + "headcrab/hc_die2.wav", +}; + +const char *CHeadCrab::pBiteSounds[] = +{ + "headcrab/hc_headbite.wav", +}; + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CHeadCrab :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_ALIEN_PREY; +} + +//========================================================= +// Center - returns the real center of the headcrab. The +// bounding box is much larger than the actual creature so +// this is needed for targeting +//========================================================= +Vector CHeadCrab :: Center ( void ) +{ + return Vector( pev->origin.x, pev->origin.y, pev->origin.z + 6 ); +} + + +Vector CHeadCrab :: BodyTarget( const Vector &posSrc ) +{ + return Center( ); +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CHeadCrab :: SetYawSpeed ( void ) +{ + int ys; + + switch ( m_Activity ) + { + case ACT_IDLE: + ys = 30; + break; + case ACT_RUN: + case ACT_WALK: + ys = 20; + break; + case ACT_TURN_LEFT: + case ACT_TURN_RIGHT: + ys = 60; + break; + case ACT_RANGE_ATTACK1: + ys = 30; + break; + default: + ys = 30; + break; + } + + pev->yaw_speed = ys; +} + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +//========================================================= +void CHeadCrab :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case HC_AE_JUMPATTACK: + { + ClearBits( pev->flags, FL_ONGROUND ); + + UTIL_SetOrigin (this, pev->origin + Vector ( 0 , 0 , 1) );// take him off ground so engine doesn't instantly reset onground + UTIL_MakeVectors ( pev->angles ); + + Vector vecJumpDir; + if (m_hEnemy != NULL) + { + float gravity = CVAR_GET_FLOAT( "sv_gravity" ); + if (gravity <= 1) + gravity = 1; + + // How fast does the headcrab need to travel to reach that height given gravity? + float height = (m_hEnemy->pev->origin.z + m_hEnemy->pev->view_ofs.z - pev->origin.z); + if (height < 16) + height = 16; + float speed = sqrt( 2 * gravity * height ); + float time = speed / gravity; + + // Scale the sideways velocity to get there at the right time + vecJumpDir = (m_hEnemy->pev->origin + m_hEnemy->pev->view_ofs - pev->origin); + vecJumpDir = vecJumpDir * ( 1.0 / time ); + + // Speed to offset gravity at the desired height + vecJumpDir.z = speed; + + // Don't jump too far/fast + float distance = vecJumpDir.Length(); + + if (distance > 650) + { + vecJumpDir = vecJumpDir * ( 650.0 / distance ); + } + } + else + { + // jump hop, don't care where + vecJumpDir = Vector( gpGlobals->v_forward.x, gpGlobals->v_forward.y, gpGlobals->v_up.z ) * 350; + } + + int iSound = RANDOM_LONG(0,2); + if ( iSound != 0 ) + EMIT_SOUND_DYN( edict(), CHAN_VOICE, pAttackSounds[iSound], GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() ); + + pev->velocity = vecJumpDir; + m_flNextAttack = gpGlobals->time + 2; + } + break; + + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + +//========================================================= +// Spawn +//========================================================= +void CHeadCrab :: Spawn() +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/headcrab.mdl"); + UTIL_SetSize(pev, Vector(-12, -12, 0), Vector(12, 12, 24)); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_GREEN; + pev->effects = 0; + if (pev->health == 0) + pev->health = HEADCRAB_HEALTH; + pev->view_ofs = Vector ( 0, 0, 20 );// position of the eyes relative to monster's origin. + pev->yaw_speed = 5;//!!! should we put this in the monster's changeanim function since turn rates may vary with state/anim? + m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + + MonsterInit(); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CHeadCrab :: Precache() +{ + PRECACHE_SOUND_ARRAY(pIdleSounds); + PRECACHE_SOUND_ARRAY(pAlertSounds); + PRECACHE_SOUND_ARRAY(pPainSounds); + PRECACHE_SOUND_ARRAY(pAttackSounds); + PRECACHE_SOUND_ARRAY(pDeathSounds); + PRECACHE_SOUND_ARRAY(pBiteSounds); + + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/headcrab.mdl"); +} + + +//========================================================= +// RunTask +//========================================================= +void CHeadCrab :: RunTask ( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_RANGE_ATTACK1: + case TASK_RANGE_ATTACK2: + { + if ( m_fSequenceFinished ) + { + TaskComplete(); + SetTouch( NULL ); + m_IdealActivity = ACT_IDLE; + } + break; + } + default: + { + CBaseMonster :: RunTask(pTask); + } + } +} + +//========================================================= +// LeapTouch - this is the headcrab's touch function when it +// is in the air +//========================================================= +void CHeadCrab :: LeapTouch ( CBaseEntity *pOther ) +{ + if ( !pOther->pev->takedamage ) + { + return; + } + + if ( pOther->Classify() == Classify() ) + { + return; + } + + // Don't hit if back on ground + if ( !FBitSet( pev->flags, FL_ONGROUND ) ) + { + EMIT_SOUND_DYN( edict(), CHAN_WEAPON, RANDOM_SOUND_ARRAY(pBiteSounds), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() ); + + pOther->TakeDamage( pev, pev, GetDamageAmount(), DMG_SLASH ); + } + + SetTouch( NULL ); +} + +//========================================================= +// PrescheduleThink +//========================================================= +void CHeadCrab :: PrescheduleThink ( void ) +{ + // make the crab coo a little bit in combat state + if ( m_MonsterState == MONSTERSTATE_COMBAT && RANDOM_FLOAT( 0, 5 ) < 0.1 ) + { + IdleSound(); + } +} + +void CHeadCrab :: StartTask ( Task_t *pTask ) +{ + m_iTaskStatus = TASKSTATUS_RUNNING; + + switch ( pTask->iTask ) + { + case TASK_RANGE_ATTACK1: + { + EMIT_SOUND_DYN( edict(), CHAN_WEAPON, pAttackSounds[0], GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() ); + m_IdealActivity = ACT_RANGE_ATTACK1; + SetTouch(&CHeadCrab :: LeapTouch ); + break; + } + default: + { + CBaseMonster :: StartTask( pTask ); + } + } +} + + +//========================================================= +// CheckRangeAttack1 +//========================================================= +BOOL CHeadCrab :: CheckRangeAttack1 ( float flDot, float flDist ) +{ + if ( FBitSet( pev->flags, FL_ONGROUND ) && flDist <= 256 && flDot >= 0.65 ) + { + return TRUE; + } + return FALSE; +} + +//========================================================= +// CheckRangeAttack2 +//========================================================= +BOOL CHeadCrab :: CheckRangeAttack2 ( float flDot, float flDist ) +{ + return FALSE; + // BUGBUG: Why is this code here? There is no ACT_RANGE_ATTACK2 animation. I've disabled it for now. +#if 0 + if ( FBitSet( pev->flags, FL_ONGROUND ) && flDist > 64 && flDist <= 256 && flDot >= 0.5 ) + { + return TRUE; + } + return FALSE; +#endif +} + +int CHeadCrab :: TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + // Don't take any acid damage -- BigMomma's mortar is acid + if ( bitsDamageType & DMG_ACID ) + flDamage = 0; + + return CBaseMonster::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType ); +} + +//========================================================= +// IdleSound +//========================================================= +#define CRAB_ATTN_IDLE (float)1.5 +void CHeadCrab :: IdleSound ( void ) +{ + EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY(pIdleSounds), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() ); +} + +//========================================================= +// AlertSound +//========================================================= +void CHeadCrab :: AlertSound ( void ) +{ + EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY(pAlertSounds), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() ); +} + +//========================================================= +// AlertSound +//========================================================= +void CHeadCrab :: PainSound ( void ) +{ + EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY(pPainSounds), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() ); +} + +//========================================================= +// DeathSound +//========================================================= +void CHeadCrab :: DeathSound ( void ) +{ + EMIT_SOUND_DYN( edict(), CHAN_VOICE, RANDOM_SOUND_ARRAY(pDeathSounds), GetSoundVolue(), ATTN_IDLE, 0, GetVoicePitch() ); +} + +Schedule_t* CHeadCrab :: GetScheduleOfType ( int Type ) +{ + switch ( Type ) + { + case SCHED_RANGE_ATTACK1: + { + return &slHCRangeAttack1[ 0 ]; + } + break; + } + + return CBaseMonster::GetScheduleOfType( Type ); +} + + +class CBabyCrab : public CHeadCrab +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed ( void ); + float GetDamageAmount( void ) { return HEADCRAB_HEALTH * 0.3; } + BOOL CheckRangeAttack1 ( float flDot, float flDist ); + Schedule_t* GetScheduleOfType ( int Type ); + virtual int GetVoicePitch( void ) { return PITCH_NORM + RANDOM_LONG(40,50); } + virtual float GetSoundVolue( void ) { return 0.8; } +}; +LINK_ENTITY_TO_CLASS( monster_babycrab, CBabyCrab ); + +void CBabyCrab :: Spawn( void ) +{ + CHeadCrab::Spawn(); + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/baby_headcrab.mdl"); + pev->rendermode = kRenderTransTexture; + pev->renderamt = 192; + UTIL_SetSize(pev, Vector(-12, -12, 0), Vector(12, 12, 24)); + + pev->health = HEADCRAB_HEALTH * 0.25; // less health than full grown +} + +void CBabyCrab :: Precache( void ) +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL( "models/baby_headcrab.mdl" ); + CHeadCrab::Precache(); +} + + +void CBabyCrab :: SetYawSpeed ( void ) +{ + pev->yaw_speed = 120; +} + + +BOOL CBabyCrab :: CheckRangeAttack1( float flDot, float flDist ) +{ + if ( pev->flags & FL_ONGROUND ) + { + if ( pev->groundentity && (pev->groundentity->v.flags & (FL_CLIENT|FL_MONSTER)) ) + return TRUE; + + // A little less accurate, but jump from closer + if ( flDist <= 180 && flDot >= 0.55 ) + return TRUE; + } + + return FALSE; +} + + +Schedule_t* CBabyCrab :: GetScheduleOfType ( int Type ) +{ + switch( Type ) + { + case SCHED_FAIL: // If you fail, try to jump! + if ( m_hEnemy != NULL ) + return slHCRangeAttack1Fast; + break; + + case SCHED_RANGE_ATTACK1: + { + return slHCRangeAttack1Fast; + } + break; + } + + return CHeadCrab::GetScheduleOfType( Type ); +} diff --git a/server/monsters/hgrunt.cpp b/server/monsters/hgrunt.cpp new file mode 100644 index 00000000..a43ecf6c --- /dev/null +++ b/server/monsters/hgrunt.cpp @@ -0,0 +1,2617 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// hgrunt +//========================================================= + +//========================================================= +// Hit groups! +//========================================================= +/* + + 1 - Head + 2 - Stomach + 3 - Gun + +*/ + + +#include "extdll.h" +#include "plane.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "animation.h" +#include "squadmonster.h" +#include "baseweapon.h" +#include "talkmonster.h" +#include "soundent.h" +#include "basebeams.h" +#include "scripted.h" //LRC +#include "defaults.h" //LRC + +int g_fGruntQuestion; // true if an idle grunt asked a question. Cleared when someone answers. + +//========================================================= +// monster-specific DEFINE's +//========================================================= +#define GRUNT_CLIP_SIZE 36 // how many bullets in a clip? - NOTE: 3 round burst sound, so keep as 3 * x! +#define GRUNT_VOL 0.35 // volume of grunt sounds +#define GRUNT_ATTN ATTN_NORM // attenutation of grunt sentences +#define HGRUNT_LIMP_HEALTH 20 +#define HGRUNT_DMG_HEADSHOT ( DMG_BULLET | DMG_CLUB ) // damage types that can kill a grunt with a single headshot. +#define HGRUNT_NUM_HEADS 2 // how many grunt heads are there? +#define HGRUNT_MINIMUM_HEADSHOT_DAMAGE 15 // must do at least this much damage in one shot to head to score a headshot kill +#define HGRUNT_SENTENCE_VOLUME (float)0.35 // volume of grunt sentences + +#define HGRUNT_9MMAR ( 1 << 0) +#define HGRUNT_HANDGRENADE ( 1 << 1) +#define HGRUNT_GRENADELAUNCHER ( 1 << 2) +#define HGRUNT_SHOTGUN ( 1 << 3) + +#define HEAD_GROUP 1 +#define HEAD_GRUNT 0 +#define HEAD_COMMANDER 1 +#define HEAD_SHOTGUN 2 +#define HEAD_M203 3 + +#define GUN_GROUP 2 +#define GUN_MP5 0 +#define GUN_SHOTGUN 1 +#define GUN_NONE 2 + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +#define HGRUNT_AE_RELOAD ( 2 ) +#define HGRUNT_AE_KICK ( 3 ) +#define HGRUNT_AE_BURST1 ( 4 ) +#define HGRUNT_AE_BURST2 ( 5 ) +#define HGRUNT_AE_BURST3 ( 6 ) +#define HGRUNT_AE_GREN_TOSS ( 7 ) +#define HGRUNT_AE_GREN_LAUNCH ( 8 ) +#define HGRUNT_AE_GREN_DROP ( 9 ) +#define HGRUNT_AE_CAUGHT_ENEMY ( 10) // grunt established sight with an enemy (player only) that had previously eluded the squad. +#define HGRUNT_AE_DROP_GUN ( 11) // grunt (probably dead) is dropping his mp5. + +//========================================================= +// monster-specific schedule types +//========================================================= +enum +{ + SCHED_GRUNT_SUPPRESS = LAST_COMMON_SCHEDULE + 1, + SCHED_GRUNT_ESTABLISH_LINE_OF_FIRE,// move to a location to set up an attack against the enemy. (usually when a friendly is in the way). + SCHED_GRUNT_COVER_AND_RELOAD, + SCHED_GRUNT_SWEEP, + SCHED_GRUNT_FOUND_ENEMY, + SCHED_GRUNT_REPEL, + SCHED_GRUNT_REPEL_ATTACK, + SCHED_GRUNT_REPEL_LAND, + SCHED_GRUNT_WAIT_FACE_ENEMY, + SCHED_GRUNT_TAKECOVER_FAILED,// special schedule type that forces analysis of conditions and picks the best possible schedule to recover from this type of failure. + SCHED_GRUNT_ELOF_FAIL, +}; + +//========================================================= +// monster-specific tasks +//========================================================= +enum +{ + TASK_GRUNT_FACE_TOSS_DIR = LAST_COMMON_TASK + 1, + TASK_GRUNT_SPEAK_SENTENCE, + TASK_GRUNT_CHECK_FIRE, +}; + +//========================================================= +// monster-specific conditions +//========================================================= +#define bits_COND_GRUNT_NOFIRE ( bits_COND_SPECIAL1 ) + +class CHGrunt : public CSquadMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed ( void ); + int Classify ( void ); + int ISoundMask ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + BOOL FCanCheckAttacks ( void ); + BOOL CheckMeleeAttack1 ( float flDot, float flDist ); + BOOL CheckRangeAttack1 ( float flDot, float flDist ); + BOOL CheckRangeAttack2 ( float flDot, float flDist ); + void CheckAmmo ( void ); + void SetActivity ( Activity NewActivity ); + void StartTask ( Task_t *pTask ); + void RunTask ( Task_t *pTask ); + void DeathSound( void ); + void PainSound( void ); + void IdleSound ( void ); + Vector GetGunPosition( void ); + void Shoot ( void ); + void Shotgun ( void ); + void PrescheduleThink ( void ); + void GibMonster( void ); + void SpeakSentence( void ); + + int Save( CSave &save ); + int Restore( CRestore &restore ); + + CBaseEntity *Kick( void ); + Schedule_t *GetSchedule( void ); + Schedule_t *GetScheduleOfType ( int Type ); + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ); + + int IRelationship ( CBaseEntity *pTarget ); + + BOOL FOkToSpeak( void ); + void JustSpoke( void ); + + CUSTOM_SCHEDULES; + static TYPEDESCRIPTION m_SaveData[]; + + // checking the feasibility of a grenade toss is kind of costly, so we do it every couple of seconds, + // not every server frame. + float m_flNextGrenadeCheck; + float m_flNextPainTime; + float m_flLastEnemySightTime; + + Vector m_vecTossVelocity; + + BOOL m_fThrowGrenade; + BOOL m_fStanding; + BOOL m_fFirstEncounter;// only put on the handsign show in the squad's first encounter. + int m_cClipSize; + + int m_voicePitch; + + int m_iBrassShell; + int m_iShotgunShell; + + int m_iSentence; + + static const char *pGruntSentences[]; +}; + +LINK_ENTITY_TO_CLASS( monster_human_grunt, CHGrunt ); + +TYPEDESCRIPTION CHGrunt::m_SaveData[] = +{ + DEFINE_FIELD( CHGrunt, m_flNextGrenadeCheck, FIELD_TIME ), + DEFINE_FIELD( CHGrunt, m_flNextPainTime, FIELD_TIME ), +// DEFINE_FIELD( CHGrunt, m_flLastEnemySightTime, FIELD_TIME ), // don't save, go to zero + DEFINE_FIELD( CHGrunt, m_vecTossVelocity, FIELD_VECTOR ), + DEFINE_FIELD( CHGrunt, m_fThrowGrenade, FIELD_BOOLEAN ), + DEFINE_FIELD( CHGrunt, m_fStanding, FIELD_BOOLEAN ), + DEFINE_FIELD( CHGrunt, m_fFirstEncounter, FIELD_BOOLEAN ), + DEFINE_FIELD( CHGrunt, m_cClipSize, FIELD_INTEGER ), + DEFINE_FIELD( CHGrunt, m_voicePitch, FIELD_INTEGER ), + DEFINE_FIELD( CHGrunt, m_iSentence, FIELD_INTEGER ), +}; + +IMPLEMENT_SAVERESTORE( CHGrunt, CSquadMonster ); + +const char *CHGrunt::pGruntSentences[] = +{ + "HG_GREN", // grenade scared grunt + "HG_ALERT", // sees player + "HG_MONSTER", // sees monster + "HG_COVER", // running to cover + "HG_THROW", // about to throw grenade + "HG_CHARGE", // running out to get the enemy + "HG_TAUNT", // say rude things +}; + +enum +{ + HGRUNT_SENT_NONE = -1, + HGRUNT_SENT_GREN = 0, + HGRUNT_SENT_ALERT, + HGRUNT_SENT_MONSTER, + HGRUNT_SENT_COVER, + HGRUNT_SENT_THROW, + HGRUNT_SENT_CHARGE, + HGRUNT_SENT_TAUNT, +} HGRUNT_SENTENCE_TYPES; + +//========================================================= +// Speak Sentence - say your cued up sentence. +// +// Some grunt sentences (take cover and charge) rely on actually +// being able to execute the intended action. It's really lame +// when a grunt says 'COVER ME' and then doesn't move. The problem +// is that the sentences were played when the decision to TRY +// to move to cover was made. Now the sentence is played after +// we know for sure that there is a valid path. The schedule +// may still fail but in most cases, well after the grunt has +// started moving. +//========================================================= +void CHGrunt :: SpeakSentence( void ) +{ + if ( m_iSentence == HGRUNT_SENT_NONE ) + { + // no sentence cued up. + return; + } + + if (FOkToSpeak()) + { + SENTENCEG_PlayRndSz( ENT(pev), pGruntSentences[ m_iSentence ], HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + JustSpoke(); + } +} + +//========================================================= +// IRelationship - overridden because Alien Grunts are +// Human Grunt's nemesis. +//========================================================= +int CHGrunt::IRelationship ( CBaseEntity *pTarget ) +{ + //LRC- only hate alien grunts if my behaviour hasn't been overridden + if (!m_iClass && FClassnameIs( pTarget->pev, "monster_alien_grunt" ) || ( FClassnameIs( pTarget->pev, "monster_gargantua" ) ) ) + { + return R_NM; + } + + return CSquadMonster::IRelationship( pTarget ); +} + +//========================================================= +// GibMonster - make gun fly through the air. +//========================================================= +void CHGrunt :: GibMonster ( void ) +{ + Vector vecGunPos; + Vector vecGunAngles; + + if ( GetBodygroup( 2 ) != 2 && !(pev->spawnflags & SF_MONSTER_NO_WPN_DROP)) + {// throw a gun if the grunt has one + GetAttachment( 0, vecGunPos, vecGunAngles ); + + CBaseEntity *pGun; + if (FBitSet( pev->weapons, HGRUNT_SHOTGUN )) + { + pGun = DropItem( "weapon_shotgun", vecGunPos, vecGunAngles ); + } + else + { + pGun = DropItem( "weapon_mp5", vecGunPos, vecGunAngles ); + } + if ( pGun ) + { + pGun->pev->velocity = Vector (RANDOM_FLOAT(-100,100), RANDOM_FLOAT(-100,100), RANDOM_FLOAT(200,300)); + pGun->pev->avelocity = Vector ( 0, RANDOM_FLOAT( 200, 400 ), 0 ); + } + + if (FBitSet( pev->weapons, HGRUNT_GRENADELAUNCHER )) + { + pGun = DropItem( "ammo_ARgrenades", vecGunPos, vecGunAngles ); + if ( pGun ) + { + pGun->pev->velocity = Vector (RANDOM_FLOAT(-100,100), RANDOM_FLOAT(-100,100), RANDOM_FLOAT(200,300)); + pGun->pev->avelocity = Vector ( 0, RANDOM_FLOAT( 200, 400 ), 0 ); + } + } + } + + CBaseMonster :: GibMonster(); +} + +//========================================================= +// ISoundMask - Overidden for human grunts because they +// hear the DANGER sound that is made by hand grenades and +// other dangerous items. +//========================================================= +int CHGrunt :: ISoundMask ( void ) +{ + return bits_SOUND_WORLD | + bits_SOUND_COMBAT | + bits_SOUND_PLAYER | + bits_SOUND_DANGER; +} + +//========================================================= +// someone else is talking - don't speak +//========================================================= +BOOL CHGrunt :: FOkToSpeak( void ) +{ +// if someone else is talking, don't speak + if (gpGlobals->time <= CTalkMonster::g_talkWaitTime) + return FALSE; + + if ( pev->spawnflags & SF_MONSTER_GAG ) + { + if ( m_MonsterState != MONSTERSTATE_COMBAT ) + { + // no talking outside of combat if gagged. + return FALSE; + } + } + + // if player is not in pvs, don't speak +// if (FNullEnt(FIND_CLIENT_IN_PVS(edict()))) +// return FALSE; + + return TRUE; +} + +//========================================================= +//========================================================= +void CHGrunt :: JustSpoke( void ) +{ + CTalkMonster::g_talkWaitTime = gpGlobals->time + RANDOM_FLOAT(1.5, 2.0); + m_iSentence = HGRUNT_SENT_NONE; +} + +//========================================================= +// PrescheduleThink - this function runs after conditions +// are collected and before scheduling code is run. +//========================================================= +void CHGrunt :: PrescheduleThink ( void ) +{ + if ( InSquad() && m_hEnemy != NULL ) + { + if ( HasConditions ( bits_COND_SEE_ENEMY ) ) + { + // update the squad's last enemy sighting time. + MySquadLeader()->m_flLastEnemySightTime = gpGlobals->time; + } + else + { + if ( gpGlobals->time - MySquadLeader()->m_flLastEnemySightTime > 5 ) + { + // been a while since we've seen the enemy + MySquadLeader()->m_fEnemyEluded = TRUE; + } + } + } +} + +//========================================================= +// FCanCheckAttacks - this is overridden for human grunts +// because they can throw/shoot grenades when they can't see their +// target and the base class doesn't check attacks if the monster +// cannot see its enemy. +// +// !!!BUGBUG - this gets called before a 3-round burst is fired +// which means that a friendly can still be hit with up to 2 rounds. +// ALSO, grenades will not be tossed if there is a friendly in front, +// this is a bad bug. Friendly machine gun fire avoidance +// will unecessarily prevent the throwing of a grenade as well. +//========================================================= +BOOL CHGrunt :: FCanCheckAttacks ( void ) +{ + if ( !HasConditions( bits_COND_ENEMY_TOOFAR ) ) + { + return TRUE; + } + else + { + return FALSE; + } +} + + +//========================================================= +// CheckMeleeAttack1 +//========================================================= +BOOL CHGrunt :: CheckMeleeAttack1 ( float flDot, float flDist ) +{ + CBaseMonster *pEnemy; + + if ( m_hEnemy != NULL ) + { + pEnemy = m_hEnemy->MyMonsterPointer(); + + if ( !pEnemy ) + { + return FALSE; + } + } + + if ( flDist <= 64 && flDot >= 0.7 && + pEnemy->Classify() != CLASS_ALIEN_BIOWEAPON && + pEnemy->Classify() != CLASS_PLAYER_BIOWEAPON ) + { + return TRUE; + } + return FALSE; +} + +//========================================================= +// CheckRangeAttack1 - overridden for HGrunt, cause +// FCanCheckAttacks() doesn't disqualify all attacks based +// on whether or not the enemy is occluded because unlike +// the base class, the HGrunt can attack when the enemy is +// occluded (throw grenade over wall, etc). We must +// disqualify the machine gun attack if the enemy is occluded. +//========================================================= +BOOL CHGrunt :: CheckRangeAttack1 ( float flDot, float flDist ) +{ + if ( !HasConditions( bits_COND_ENEMY_OCCLUDED ) && flDist <= 2048 && flDot >= 0.5 && NoFriendlyFire() ) + { + TraceResult tr; + + if ( !m_hEnemy->IsPlayer() && flDist <= 64 ) + { + // kick nonclients who are close enough, but don't shoot at them. + return FALSE; + } + + Vector vecSrc = GetGunPosition(); + + // verify that a bullet fired from the gun will hit the enemy before the world. + UTIL_TraceLine( vecSrc, m_hEnemy->BodyTarget(vecSrc), ignore_monsters, ignore_glass, ENT(pev), &tr); + + if ( tr.flFraction == 1.0 ) + { + return TRUE; + } + } + + return FALSE; +} + +//========================================================= +// CheckRangeAttack2 - this checks the Grunt's grenade +// attack. +//========================================================= +BOOL CHGrunt :: CheckRangeAttack2 ( float flDot, float flDist ) +{ + if (! FBitSet(pev->weapons, (HGRUNT_HANDGRENADE | HGRUNT_GRENADELAUNCHER))) + { + return FALSE; + } + + // if the grunt isn't moving, it's ok to check. + if ( m_flGroundSpeed != 0 ) + { + m_fThrowGrenade = FALSE; + return m_fThrowGrenade; + } + + // assume things haven't changed too much since last time + if (gpGlobals->time < m_flNextGrenadeCheck ) + { + return m_fThrowGrenade; + } + + if ( !FBitSet ( m_hEnemy->pev->flags, FL_ONGROUND ) && (m_hEnemy->pev->waterlevel == 0 || m_hEnemy->pev->watertype == CONTENTS_FOG) && m_vecEnemyLKP.z > pev->absmax.z ) + { + //!!!BUGBUG - we should make this check movetype and make sure it isn't FLY? Players who jump a lot are unlikely to + // be grenaded. + // don't throw grenades at anything that isn't on the ground! + m_fThrowGrenade = FALSE; + return m_fThrowGrenade; + } + + Vector vecTarget; + + if (FBitSet( pev->weapons, HGRUNT_HANDGRENADE)) + { + // find feet + if (RANDOM_LONG(0,1)) + { + // magically know where they are + vecTarget = Vector( m_hEnemy->pev->origin.x, m_hEnemy->pev->origin.y, m_hEnemy->pev->absmin.z ); + } + else + { + // toss it to where you last saw them + vecTarget = m_vecEnemyLKP; + } + // vecTarget = m_vecEnemyLKP + (m_hEnemy->BodyTarget( pev->origin ) - m_hEnemy->pev->origin); + // estimate position + // vecTarget = vecTarget + m_hEnemy->pev->velocity * 2; + } + else + { + // find target + // vecTarget = m_hEnemy->BodyTarget( pev->origin ); + vecTarget = m_vecEnemyLKP + (m_hEnemy->BodyTarget( pev->origin ) - m_hEnemy->pev->origin); + // estimate position + if (HasConditions( bits_COND_SEE_ENEMY)) + vecTarget = vecTarget + ((vecTarget - pev->origin).Length() / HGRUNT_GRENADE_SPEED) * m_hEnemy->pev->velocity; + } + + // are any of my squad members near the intended grenade impact area? + if ( InSquad() ) + { + if (SquadMemberInRange( vecTarget, 256 )) + { + // crap, I might blow my own guy up. Don't throw a grenade and don't check again for a while. + m_flNextGrenadeCheck = gpGlobals->time + 1; // one full second. + m_fThrowGrenade = FALSE; + return m_fThrowGrenade; //AJH need this or it is overridden later. + } + } + + if ( ( vecTarget - pev->origin ).Length2D() <= 256 ) + { + // crap, I don't want to blow myself up + m_flNextGrenadeCheck = gpGlobals->time + 1; // one full second. + m_fThrowGrenade = FALSE; + return m_fThrowGrenade; + } + + + if (FBitSet( pev->weapons, HGRUNT_HANDGRENADE)) + { + Vector vecToss = VecCheckToss( pev, GetGunPosition(), vecTarget, 0.5 ); + + if ( vecToss != g_vecZero ) + { + m_vecTossVelocity = vecToss; + + // throw a hand grenade + m_fThrowGrenade = TRUE; + // don't check again for a while. + m_flNextGrenadeCheck = gpGlobals->time; // 1/3 second. + } + else + { + // don't throw + m_fThrowGrenade = FALSE; + // don't check again for a while. + m_flNextGrenadeCheck = gpGlobals->time + 1; // one full second. + } + } + else + { + Vector vecToss = VecCheckThrow( pev, GetGunPosition(), vecTarget, HGRUNT_GRENADE_SPEED, 0.5 ); + + if ( vecToss != g_vecZero ) + { + m_vecTossVelocity = vecToss; + + // throw a hand grenade + m_fThrowGrenade = TRUE; + // don't check again for a while. + m_flNextGrenadeCheck = gpGlobals->time + 0.3; // 1/3 second. + } + else + { + // don't throw + m_fThrowGrenade = FALSE; + // don't check again for a while. + m_flNextGrenadeCheck = gpGlobals->time + 1; // one full second. + } + } + + + + return m_fThrowGrenade; +} + + +//========================================================= +// TraceAttack - make sure we're not taking it in the helmet +//========================================================= +void CHGrunt :: TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + // check for helmet shot + if (ptr->iHitgroup == 11) + { + // make sure we're wearing one + if (GetBodygroup( 1 ) == HEAD_GRUNT && (bitsDamageType & (DMG_BULLET | DMG_SLASH | DMG_BLAST | DMG_CLUB))) + { + // absorb damage + flDamage -= 20; + if (flDamage <= 0) + { + UTIL_Ricochet( ptr->vecEndPos, 1.0 ); + flDamage = 0.01; + } + } + // it's head shot anyways + ptr->iHitgroup = HITGROUP_HEAD; + } + CSquadMonster::TraceAttack( pevAttacker, flDamage, vecDir, ptr, bitsDamageType ); +} + + +//========================================================= +// TakeDamage - overridden for the grunt because the grunt +// needs to forget that he is in cover if he's hurt. (Obviously +// not in a safe place anymore). +//========================================================= +int CHGrunt :: TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + Forget( bits_MEMORY_INCOVER ); + + return CSquadMonster :: TakeDamage ( pevInflictor, pevAttacker, flDamage, bitsDamageType ); +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CHGrunt :: SetYawSpeed ( void ) +{ + int ys; + + switch ( m_Activity ) + { + case ACT_IDLE: + ys = 150; + break; + case ACT_RUN: + ys = 150; + break; + case ACT_WALK: + ys = 180; + break; + case ACT_RANGE_ATTACK1: + ys = 120; + break; + case ACT_RANGE_ATTACK2: + ys = 120; + break; + case ACT_MELEE_ATTACK1: + ys = 120; + break; + case ACT_MELEE_ATTACK2: + ys = 120; + break; + case ACT_TURN_LEFT: + case ACT_TURN_RIGHT: + ys = 180; + break; + case ACT_GLIDE: + case ACT_FLY: + ys = 30; + break; + default: + ys = 90; + break; + } + + pev->yaw_speed = ys; +} + +void CHGrunt :: IdleSound( void ) +{ + if (FOkToSpeak() && (g_fGruntQuestion || RANDOM_LONG(0,1))) + { + if (!g_fGruntQuestion) + { + // ask question or make statement + switch (RANDOM_LONG(0,2)) + { + case 0: // check in + SENTENCEG_PlayRndSz(ENT(pev), "HG_CHECK", HGRUNT_SENTENCE_VOLUME, ATTN_NORM, 0, m_voicePitch); + g_fGruntQuestion = 1; + break; + case 1: // question + SENTENCEG_PlayRndSz(ENT(pev), "HG_QUEST", HGRUNT_SENTENCE_VOLUME, ATTN_NORM, 0, m_voicePitch); + g_fGruntQuestion = 2; + break; + case 2: // statement + SENTENCEG_PlayRndSz(ENT(pev), "HG_IDLE", HGRUNT_SENTENCE_VOLUME, ATTN_NORM, 0, m_voicePitch); + break; + } + } + else + { + switch (g_fGruntQuestion) + { + case 1: // check in + SENTENCEG_PlayRndSz(ENT(pev), "HG_CLEAR", HGRUNT_SENTENCE_VOLUME, ATTN_NORM, 0, m_voicePitch); + break; + case 2: // question + SENTENCEG_PlayRndSz(ENT(pev), "HG_ANSWER", HGRUNT_SENTENCE_VOLUME, ATTN_NORM, 0, m_voicePitch); + break; + } + g_fGruntQuestion = 0; + } + JustSpoke(); + } +} + +//========================================================= +// CheckAmmo - overridden for the grunt because he actually +// uses ammo! (base class doesn't) +//========================================================= +void CHGrunt :: CheckAmmo ( void ) +{ + if ( m_cAmmoLoaded <= 0 ) + { + SetConditions(bits_COND_NO_AMMO_LOADED); + } +} + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CHGrunt :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_HUMAN_MILITARY; +} + +//========================================================= +//========================================================= +CBaseEntity *CHGrunt :: Kick( void ) +{ + TraceResult tr; + + UTIL_MakeVectors( pev->angles ); + Vector vecStart = pev->origin; + vecStart.z += pev->size.z * 0.5; + Vector vecEnd = vecStart + (gpGlobals->v_forward * 70); + + UTIL_TraceHull( vecStart, vecEnd, dont_ignore_monsters, head_hull, ENT(pev), &tr ); + + if ( tr.pHit ) + { + CBaseEntity *pEntity = CBaseEntity::Instance( tr.pHit ); + return pEntity; + } + + return NULL; +} + +//========================================================= +// GetGunPosition return the end of the barrel +//========================================================= + +Vector CHGrunt :: GetGunPosition( ) +{ + if (m_fStanding ) + { + return pev->origin + Vector( 0, 0, 60 ); + } + else + { + return pev->origin + Vector( 0, 0, 48 ); + } +} + +//========================================================= +// Shoot +//========================================================= +void CHGrunt :: Shoot ( void ) +{ + if (m_hEnemy == NULL && m_pCine == NULL) //LRC - scripts may fire when you have no enemy + { + return; + } + + Vector vecShootOrigin = GetGunPosition(); + Vector vecShootDir = ShootAtEnemy( vecShootOrigin ); + + if (m_cAmmoLoaded > 0) + { + UTIL_MakeVectors ( pev->angles ); + + Vector vecShellVelocity = gpGlobals->v_right * RANDOM_FLOAT(40,90) + gpGlobals->v_up * RANDOM_FLOAT(75,200) + gpGlobals->v_forward * RANDOM_FLOAT(-40, 40); + EjectBrass ( vecShootOrigin - vecShootDir * 24, vecShellVelocity, pev->angles.y, m_iBrassShell, TE_BOUNCE_SHELL); + FireBullets(1, vecShootOrigin, vecShootDir, VECTOR_CONE_10DEGREES, 2048, BULLET_MP5 ); // shoot +-5 degrees + + pev->effects |= EF_MUZZLEFLASH; + + m_cAmmoLoaded--;// take away a bullet! + } + + Vector angDir = UTIL_VecToAngles( vecShootDir ); + SetBlending( 0, angDir.x ); + + // Teh_Freak: World Lighting! + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_DLIGHT ); + WRITE_COORD( vecShootOrigin.x ); // origin + WRITE_COORD( vecShootOrigin.y ); + WRITE_COORD( vecShootOrigin.z ); + WRITE_BYTE( 16 ); // radius + WRITE_BYTE( 255 ); // R + WRITE_BYTE( 255 ); // G + WRITE_BYTE( 128 ); // B + WRITE_BYTE( 0 ); // life * 10 + WRITE_BYTE( 0 ); // decay + MESSAGE_END(); + // Teh_Freak: World Lighting! + +} + +//========================================================= +// Shoot +//========================================================= +void CHGrunt :: Shotgun ( void ) +{ + if (m_hEnemy == NULL && m_pCine == NULL) + { + return; + } + + Vector vecShootOrigin = GetGunPosition(); + Vector vecShootDir = ShootAtEnemy( vecShootOrigin ); + + UTIL_MakeVectors ( pev->angles ); + + Vector vecShellVelocity = gpGlobals->v_right * RANDOM_FLOAT(40,90) + gpGlobals->v_up * RANDOM_FLOAT(75,200) + gpGlobals->v_forward * RANDOM_FLOAT(-40, 40); + EjectBrass ( vecShootOrigin - vecShootDir * 24, vecShellVelocity, pev->angles.y, m_iShotgunShell, TE_BOUNCE_SHOTSHELL); + FireBullets(HGRUNT_SHOTGUN_PELLETS, vecShootOrigin, vecShootDir, VECTOR_CONE_15DEGREES, 2048, BULLET_BUCKSHOT, 0 ); // shoot +-7.5 degrees + + pev->effects |= EF_MUZZLEFLASH; + + m_cAmmoLoaded--;// take away a bullet! + + Vector angDir = UTIL_VecToAngles( vecShootDir ); + SetBlending( 0, angDir.x ); + + // Teh_Freak: World Lighting! + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_DLIGHT ); + WRITE_COORD( vecShootOrigin.x ); // origin + WRITE_COORD( vecShootOrigin.y ); + WRITE_COORD( vecShootOrigin.z ); + WRITE_BYTE( 16 ); // radius + WRITE_BYTE( 255 ); // R + WRITE_BYTE( 255 ); // G + WRITE_BYTE( 128 ); // B + WRITE_BYTE( 0 ); // life * 10 + WRITE_BYTE( 0 ); // decay + MESSAGE_END(); + // Teh_Freak: World Lighting! + +} + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +//========================================================= +void CHGrunt :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + Vector vecShootDir; + Vector vecShootOrigin; + + switch( pEvent->event ) + { + case HGRUNT_AE_DROP_GUN: + { + if (pev->spawnflags & SF_MONSTER_NO_WPN_DROP) break; //LRC + + Vector vecGunPos; + Vector vecGunAngles; + + GetAttachment( 0, vecGunPos, vecGunAngles ); + + // switch to body group with no gun. + SetBodygroup( GUN_GROUP, GUN_NONE ); + + // now spawn a gun. + if (FBitSet( pev->weapons, HGRUNT_SHOTGUN )) + { + DropItem( "weapon_shotgun", vecGunPos, vecGunAngles ); + } + else + { + DropItem( "weapon_mp5", vecGunPos, vecGunAngles ); + } + if (FBitSet( pev->weapons, HGRUNT_GRENADELAUNCHER )) + { + DropItem( "ammo_ARgrenades", BodyTarget( pev->origin ), vecGunAngles ); + } + + } + break; + + case HGRUNT_AE_RELOAD: + EMIT_SOUND( ENT(pev), CHAN_WEAPON, "hgrunt/gr_reload1.wav", 1, ATTN_NORM ); + m_cAmmoLoaded = m_cClipSize; + ClearConditions(bits_COND_NO_AMMO_LOADED); + break; + + case HGRUNT_AE_GREN_TOSS: + { + UTIL_MakeVectors( pev->angles ); + // CGrenade::ShootTimed( pev, pev->origin + gpGlobals->v_forward * 34 + Vector (0, 0, 32), m_vecTossVelocity, 3.5 ); + //LRC - a bit of a hack. Ideally the grunts would work out in advance whether it's ok to throw. + if (m_pCine) + { + Vector vecToss = g_vecZero; + if (m_hTargetEnt != NULL && m_pCine->PreciseAttack()) + { + vecToss = VecCheckToss( pev, GetGunPosition(), m_hTargetEnt->pev->origin, 0.5 ); + } + if (vecToss == g_vecZero) + { + vecToss = (gpGlobals->v_forward*0.5+gpGlobals->v_up*0.5).Normalize()*HGRUNT_GRENADE_SPEED; + } + CGrenade::ShootTimed( pev, GetGunPosition(), vecToss, 3.5 ); + } + else + CGrenade::ShootTimed( pev, GetGunPosition(), m_vecTossVelocity, 3.5 ); + + m_fThrowGrenade = FALSE; + m_flNextGrenadeCheck = gpGlobals->time + 6;// wait six seconds before even looking again to see if a grenade can be thrown. + // !!!LATER - when in a group, only try to throw grenade if ordered. + } + break; + + case HGRUNT_AE_GREN_LAUNCH: + { + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/glauncher.wav", 0.8, ATTN_NORM); + //LRC: firing due to a script? + if (m_pCine) + { + Vector vecToss; + if (m_hTargetEnt != NULL && m_pCine->PreciseAttack()) + vecToss = VecCheckThrow( pev, GetGunPosition(), m_hTargetEnt->pev->origin, HGRUNT_GRENADE_SPEED, 0.5 ); + else + { + // just shoot diagonally up+forwards + UTIL_MakeVectors(pev->angles); + vecToss = (gpGlobals->v_forward*0.5 + gpGlobals->v_up*0.5).Normalize() * HGRUNT_GRENADE_SPEED; + } + CGrenade::ShootContact( pev, GetGunPosition(), vecToss ); + } + else + CGrenade::ShootContact( pev, GetGunPosition(), m_vecTossVelocity ); + m_fThrowGrenade = FALSE; + m_flNextGrenadeCheck = gpGlobals->time + RANDOM_FLOAT( 4, 6 );// wait six seconds before even looking again to see if a grenade can be thrown. + } + break; + + case HGRUNT_AE_GREN_DROP: + { + UTIL_MakeVectors( pev->angles ); + CGrenade::ShootTimed( pev, pev->origin + gpGlobals->v_forward * 17 - gpGlobals->v_right * 27 + gpGlobals->v_up * 6, g_vecZero, 3 ); + } + break; + + case HGRUNT_AE_BURST1: + { + if ( FBitSet( pev->weapons, HGRUNT_9MMAR )) + { + // the first round of the three round burst plays the sound and puts a sound in the world sound list. + if (m_cAmmoLoaded > 0) + { + if ( RANDOM_LONG(0,1) ) + { + EMIT_SOUND( ENT(pev), CHAN_WEAPON, "hgrunt/gr_mgun1.wav", 1, ATTN_NORM ); + } + else + { + EMIT_SOUND( ENT(pev), CHAN_WEAPON, "hgrunt/gr_mgun2.wav", 1, ATTN_NORM ); + } + } + else + { + EMIT_SOUND( ENT(pev), CHAN_WEAPON, "weapons/dryfire1.wav", 1, ATTN_NORM ); + } + + Shoot(); + } + else + { + Shotgun( ); + + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/sbarrel1.wav", 1, ATTN_NORM ); + } + + CSoundEnt::InsertSound ( bits_SOUND_COMBAT, pev->origin, 384, 0.3 ); + } + break; + + case HGRUNT_AE_BURST2: + case HGRUNT_AE_BURST3: + Shoot(); + break; + + case HGRUNT_AE_KICK: + { + CBaseEntity *pHurt = Kick(); + + if ( pHurt ) + { + // SOUND HERE! + UTIL_MakeVectors( pev->angles ); + pHurt->pev->punchangle.x = 15; + pHurt->pev->velocity = pHurt->pev->velocity + gpGlobals->v_forward * 100 + gpGlobals->v_up * 50; + pHurt->TakeDamage( pev, pev, HGRUNT_DMG_KICK, DMG_CLUB ); + } + } + break; + + case HGRUNT_AE_CAUGHT_ENEMY: + { + if ( FOkToSpeak() ) + { + SENTENCEG_PlayRndSz(ENT(pev), "HG_ALERT", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + JustSpoke(); + } + + } + + default: + CSquadMonster::HandleAnimEvent( pEvent ); + break; + } +} + +//========================================================= +// Spawn +//========================================================= +void CHGrunt :: Spawn() +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/hgrunt.mdl"); + UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_RED; + pev->effects = 0; + if (pev->health == 0) + pev->health = HGRUNT_HEALTH; + m_flFieldOfView = 0.2;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + m_flNextGrenadeCheck = gpGlobals->time + 1; + m_flNextPainTime = gpGlobals->time; + m_iSentence = HGRUNT_SENT_NONE; + + m_afCapability = bits_CAP_SQUAD | bits_CAP_TURN_HEAD | bits_CAP_DOORS_GROUP; + + m_fEnemyEluded = FALSE; + m_fFirstEncounter = TRUE;// this is true when the grunt spawns, because he hasn't encountered an enemy yet. + + m_HackedGunPos = Vector ( 0, 0, 55 ); + + if (pev->weapons == 0) + { + // initialize to original values + pev->weapons = HGRUNT_9MMAR | HGRUNT_HANDGRENADE; + // pev->weapons = HGRUNT_SHOTGUN; + // pev->weapons = HGRUNT_9MMAR | HGRUNT_GRENADELAUNCHER; + } + + if (FBitSet( pev->weapons, HGRUNT_SHOTGUN )) + { + SetBodygroup( GUN_GROUP, GUN_SHOTGUN ); + m_cClipSize = 8; + } + else + { + m_cClipSize = GRUNT_CLIP_SIZE; + } + m_cAmmoLoaded = m_cClipSize; + + if (RANDOM_LONG( 0, 99 ) < 80) + pev->skin = 0; // light skin + else + pev->skin = 1; // dark skin + + if (FBitSet( pev->weapons, HGRUNT_SHOTGUN )) + { + SetBodygroup( HEAD_GROUP, HEAD_SHOTGUN); + } + else if (FBitSet( pev->weapons, HGRUNT_GRENADELAUNCHER )) + { + SetBodygroup( HEAD_GROUP, HEAD_M203 ); + pev->skin = 1; // alway dark skin + } + + CTalkMonster::g_talkWaitTime = 0; + + MonsterInit(); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CHGrunt :: Precache() +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/hgrunt.mdl"); + + PRECACHE_SOUND( "weapons/dryfire1.wav" ); //LRC + + PRECACHE_SOUND( "hgrunt/gr_mgun1.wav" ); + PRECACHE_SOUND( "hgrunt/gr_mgun2.wav" ); + + PRECACHE_SOUND( "hgrunt/gr_die1.wav" ); + PRECACHE_SOUND( "hgrunt/gr_die2.wav" ); + PRECACHE_SOUND( "hgrunt/gr_die3.wav" ); + + PRECACHE_SOUND( "hgrunt/gr_pain1.wav" ); + PRECACHE_SOUND( "hgrunt/gr_pain2.wav" ); + PRECACHE_SOUND( "hgrunt/gr_pain3.wav" ); + PRECACHE_SOUND( "hgrunt/gr_pain4.wav" ); + PRECACHE_SOUND( "hgrunt/gr_pain5.wav" ); + + PRECACHE_SOUND( "hgrunt/gr_reload1.wav" ); + + PRECACHE_SOUND( "weapons/glauncher.wav" ); + + PRECACHE_SOUND( "weapons/sbarrel1.wav" ); + + PRECACHE_SOUND("zombie/claw_miss2.wav");// because we use the basemonster SWIPE animation event + + // get voice pitch + if (RANDOM_LONG(0,1)) + m_voicePitch = 109 + RANDOM_LONG(0,7); + else + m_voicePitch = 100; + + m_iBrassShell = UTIL_PrecacheModel ("models/shell556.mdl");// brass shell + m_iShotgunShell = UTIL_PrecacheModel ("models/shellBuck.mdl"); +} + +//========================================================= +// start task +//========================================================= +void CHGrunt :: StartTask ( Task_t *pTask ) +{ + m_iTaskStatus = TASKSTATUS_RUNNING; + + switch ( pTask->iTask ) + { + case TASK_GRUNT_CHECK_FIRE: + if ( !NoFriendlyFire() ) + { + SetConditions( bits_COND_GRUNT_NOFIRE ); + } + TaskComplete(); + break; + + case TASK_GRUNT_SPEAK_SENTENCE: + SpeakSentence(); + TaskComplete(); + break; + + case TASK_WALK_PATH: + case TASK_RUN_PATH: + // grunt no longer assumes he is covered if he moves + Forget( bits_MEMORY_INCOVER ); + CSquadMonster ::StartTask( pTask ); + break; + + case TASK_RELOAD: + m_IdealActivity = ACT_RELOAD; + break; + + case TASK_GRUNT_FACE_TOSS_DIR: + break; + + case TASK_FACE_IDEAL: + case TASK_FACE_ENEMY: + CSquadMonster :: StartTask( pTask ); + if (pev->movetype == MOVETYPE_FLY) + { + m_IdealActivity = ACT_GLIDE; + } + break; + + default: + CSquadMonster :: StartTask( pTask ); + break; + } +} + +//========================================================= +// RunTask +//========================================================= +void CHGrunt :: RunTask ( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_GRUNT_FACE_TOSS_DIR: + { + // project a point along the toss vector and turn to face that point. + MakeIdealYaw( pev->origin + m_vecTossVelocity * 64 ); + ChangeYaw( pev->yaw_speed ); + + if ( FacingIdeal() ) + { + m_iTaskStatus = TASKSTATUS_COMPLETE; + } + break; + } + default: + { + CSquadMonster :: RunTask( pTask ); + break; + } + } +} + +//========================================================= +// PainSound +//========================================================= +void CHGrunt :: PainSound ( void ) +{ + if ( gpGlobals->time > m_flNextPainTime ) + { +#if 0 + if ( RANDOM_LONG(0,99) < 5 ) + { + // pain sentences are rare + if (FOkToSpeak()) + { + SENTENCEG_PlayRndSz(ENT(pev), "HG_PAIN", HGRUNT_SENTENCE_VOLUME, ATTN_NORM, 0, PITCH_NORM); + JustSpoke(); + return; + } + } +#endif + switch ( RANDOM_LONG(0,6) ) + { + case 0: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_pain3.wav", 1, ATTN_NORM ); + break; + case 1: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_pain4.wav", 1, ATTN_NORM ); + break; + case 2: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_pain5.wav", 1, ATTN_NORM ); + break; + case 3: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_pain1.wav", 1, ATTN_NORM ); + break; + case 4: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_pain2.wav", 1, ATTN_NORM ); + break; + } + + m_flNextPainTime = gpGlobals->time + 1; + } +} + +//========================================================= +// DeathSound +//========================================================= +void CHGrunt :: DeathSound ( void ) +{ + switch ( RANDOM_LONG(0,2) ) + { + case 0: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_die1.wav", 1, ATTN_IDLE ); + break; + case 1: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_die2.wav", 1, ATTN_IDLE ); + break; + case 2: + EMIT_SOUND( ENT(pev), CHAN_VOICE, "hgrunt/gr_die3.wav", 1, ATTN_IDLE ); + break; + } +} + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= + +//========================================================= +// GruntFail +//========================================================= +Task_t tlGruntFail[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT, (float)2 }, + { TASK_WAIT_PVS, (float)0 }, +}; + +Schedule_t slGruntFail[] = +{ + { + tlGruntFail, + ARRAYSIZE ( tlGruntFail ), + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_CAN_MELEE_ATTACK1 | + bits_COND_CAN_MELEE_ATTACK2, + 0, + "Grunt Fail" + }, +}; + +//========================================================= +// Grunt Combat Fail +//========================================================= +Task_t tlGruntCombatFail[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT_FACE_ENEMY, (float)2 }, + { TASK_WAIT_PVS, (float)0 }, +}; + +Schedule_t slGruntCombatFail[] = +{ + { + tlGruntCombatFail, + ARRAYSIZE ( tlGruntCombatFail ), + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2, + 0, + "Grunt Combat Fail" + }, +}; + +//========================================================= +// Victory dance! +//========================================================= +Task_t tlGruntVictoryDance[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_WAIT, (float)1.5 }, + { TASK_GET_PATH_TO_ENEMY_CORPSE, (float)0 }, + { TASK_WALK_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_VICTORY_DANCE }, +}; + +Schedule_t slGruntVictoryDance[] = +{ + { + tlGruntVictoryDance, + ARRAYSIZE ( tlGruntVictoryDance ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE, + 0, + "GruntVictoryDance" + }, +}; + +//========================================================= +// Establish line of fire - move to a position that allows +// the grunt to attack. +//========================================================= +Task_t tlGruntEstablishLineOfFire[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_GRUNT_ELOF_FAIL }, + { TASK_GET_PATH_TO_ENEMY, (float)0 }, + { TASK_GRUNT_SPEAK_SENTENCE,(float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, +}; + +Schedule_t slGruntEstablishLineOfFire[] = +{ + { + tlGruntEstablishLineOfFire, + ARRAYSIZE ( tlGruntEstablishLineOfFire ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_MELEE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_CAN_MELEE_ATTACK2 | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "GruntEstablishLineOfFire" + }, +}; + +//========================================================= +// GruntFoundEnemy - grunt established sight with an enemy +// that was hiding from the squad. +//========================================================= +Task_t tlGruntFoundEnemy[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_PLAY_SEQUENCE_FACE_ENEMY,(float)ACT_SIGNAL1 }, +}; + +Schedule_t slGruntFoundEnemy[] = +{ + { + tlGruntFoundEnemy, + ARRAYSIZE ( tlGruntFoundEnemy ), + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "GruntFoundEnemy" + }, +}; + +//========================================================= +// GruntCombatFace Schedule +//========================================================= +Task_t tlGruntCombatFace1[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_WAIT, (float)1.5 }, + { TASK_SET_SCHEDULE, (float)SCHED_GRUNT_SWEEP }, +}; + +Schedule_t slGruntCombatFace[] = +{ + { + tlGruntCombatFace1, + ARRAYSIZE ( tlGruntCombatFace1 ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2, + 0, + "Combat Face" + }, +}; + +//========================================================= +// Suppressing fire - don't stop shooting until the clip is +// empty or grunt gets hurt. +//========================================================= +Task_t tlGruntSignalSuppress[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_PLAY_SEQUENCE_FACE_ENEMY, (float)ACT_SIGNAL2 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, +}; + +Schedule_t slGruntSignalSuppress[] = +{ + { + tlGruntSignalSuppress, + ARRAYSIZE ( tlGruntSignalSuppress ), + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_GRUNT_NOFIRE | + bits_COND_NO_AMMO_LOADED, + + bits_SOUND_DANGER, + "SignalSuppress" + }, +}; + +Task_t tlGruntSuppress[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, +}; + +Schedule_t slGruntSuppress[] = +{ + { + tlGruntSuppress, + ARRAYSIZE ( tlGruntSuppress ), + bits_COND_ENEMY_DEAD | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_GRUNT_NOFIRE | + bits_COND_NO_AMMO_LOADED, + + bits_SOUND_DANGER, + "Suppress" + }, +}; + + +//========================================================= +// grunt wait in cover - we don't allow danger or the ability +// to attack to break a grunt's run to cover schedule, but +// when a grunt is in cover, we do want them to attack if they can. +//========================================================= +Task_t tlGruntWaitInCover[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT_FACE_ENEMY, (float)1 }, +}; + +Schedule_t slGruntWaitInCover[] = +{ + { + tlGruntWaitInCover, + ARRAYSIZE ( tlGruntWaitInCover ), + bits_COND_NEW_ENEMY | + bits_COND_HEAR_SOUND | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_CAN_MELEE_ATTACK1 | + bits_COND_CAN_MELEE_ATTACK2, + + bits_SOUND_DANGER, + "GruntWaitInCover" + }, +}; + +//========================================================= +// run to cover. +// !!!BUGBUG - set a decent fail schedule here. +//========================================================= +Task_t tlGruntTakeCover1[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_GRUNT_TAKECOVER_FAILED }, + { TASK_WAIT, (float)0.2 }, + { TASK_FIND_COVER_FROM_ENEMY, (float)0 }, + { TASK_GRUNT_SPEAK_SENTENCE, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_SET_SCHEDULE, (float)SCHED_GRUNT_WAIT_FACE_ENEMY }, +}; + +Schedule_t slGruntTakeCover[] = +{ + { + tlGruntTakeCover1, + ARRAYSIZE ( tlGruntTakeCover1 ), + 0, + 0, + "TakeCover" + }, +}; + +//========================================================= +// drop grenade then run to cover. +//========================================================= +Task_t tlGruntGrenadeCover1[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FIND_COVER_FROM_ENEMY, (float)99 }, + { TASK_FIND_FAR_NODE_COVER_FROM_ENEMY, (float)384 }, + { TASK_PLAY_SEQUENCE, (float)ACT_SPECIAL_ATTACK1 }, + { TASK_CLEAR_MOVE_WAIT, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_SET_SCHEDULE, (float)SCHED_GRUNT_WAIT_FACE_ENEMY }, +}; + +Schedule_t slGruntGrenadeCover[] = +{ + { + tlGruntGrenadeCover1, + ARRAYSIZE ( tlGruntGrenadeCover1 ), + 0, + 0, + "GrenadeCover" + }, +}; + + +//========================================================= +// drop grenade then run to cover. +//========================================================= +Task_t tlGruntTossGrenadeCover1[] = +{ + { TASK_FACE_ENEMY, (float)0 }, + { TASK_RANGE_ATTACK2, (float)0 }, + { TASK_SET_SCHEDULE, (float)SCHED_TAKE_COVER_FROM_ENEMY }, +}; + +Schedule_t slGruntTossGrenadeCover[] = +{ + { + tlGruntTossGrenadeCover1, + ARRAYSIZE ( tlGruntTossGrenadeCover1 ), + 0, + 0, + "TossGrenadeCover" + }, +}; + +//========================================================= +// hide from the loudest sound source (to run from grenade) +//========================================================= +Task_t tlGruntTakeCoverFromBestSound[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_COWER },// duck and cover if cannot move from explosion + { TASK_STOP_MOVING, (float)0 }, + { TASK_FIND_COVER_FROM_BEST_SOUND, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_TURN_LEFT, (float)179 }, +}; + +Schedule_t slGruntTakeCoverFromBestSound[] = +{ + { + tlGruntTakeCoverFromBestSound, + ARRAYSIZE ( tlGruntTakeCoverFromBestSound ), + 0, + 0, + "GruntTakeCoverFromBestSound" + }, +}; + +//========================================================= +// Grunt reload schedule +//========================================================= +Task_t tlGruntHideReload[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_RELOAD }, + { TASK_FIND_COVER_FROM_ENEMY, (float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_REMEMBER, (float)bits_MEMORY_INCOVER }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_RELOAD }, +}; + +Schedule_t slGruntHideReload[] = +{ + { + tlGruntHideReload, + ARRAYSIZE ( tlGruntHideReload ), + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "GruntHideReload" + } +}; + +//========================================================= +// Do a turning sweep of the area +//========================================================= +Task_t tlGruntSweep[] = +{ + { TASK_TURN_LEFT, (float)179 }, + { TASK_WAIT, (float)1 }, + { TASK_TURN_LEFT, (float)179 }, + { TASK_WAIT, (float)1 }, +}; + +Schedule_t slGruntSweep[] = +{ + { + tlGruntSweep, + ARRAYSIZE ( tlGruntSweep ), + + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_CAN_RANGE_ATTACK1 | + bits_COND_CAN_RANGE_ATTACK2 | + bits_COND_HEAR_SOUND, + + bits_SOUND_WORLD |// sound flags + bits_SOUND_DANGER | + bits_SOUND_PLAYER, + + "Grunt Sweep" + }, +}; + +//========================================================= +// primary range attack. Overriden because base class stops attacking when the enemy is occluded. +// grunt's grenade toss requires the enemy be occluded. +//========================================================= +Task_t tlGruntRangeAttack1A[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_PLAY_SEQUENCE_FACE_ENEMY, (float)ACT_CROUCH }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, +}; + +Schedule_t slGruntRangeAttack1A[] = +{ + { + tlGruntRangeAttack1A, + ARRAYSIZE ( tlGruntRangeAttack1A ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED | + bits_COND_HEAR_SOUND | + bits_COND_GRUNT_NOFIRE | + bits_COND_NO_AMMO_LOADED, + + bits_SOUND_DANGER, + "Range Attack1A" + }, +}; + + +//========================================================= +// primary range attack. Overriden because base class stops attacking when the enemy is occluded. +// grunt's grenade toss requires the enemy be occluded. +//========================================================= +Task_t tlGruntRangeAttack1B[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_PLAY_SEQUENCE_FACE_ENEMY,(float)ACT_IDLE_ANGRY }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_GRUNT_CHECK_FIRE, (float)0 }, + { TASK_RANGE_ATTACK1, (float)0 }, +}; + +Schedule_t slGruntRangeAttack1B[] = +{ + { + tlGruntRangeAttack1B, + ARRAYSIZE ( tlGruntRangeAttack1B ), + bits_COND_NEW_ENEMY | + bits_COND_ENEMY_DEAD | + bits_COND_HEAVY_DAMAGE | + bits_COND_ENEMY_OCCLUDED | + bits_COND_NO_AMMO_LOADED | + bits_COND_GRUNT_NOFIRE | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER, + "Range Attack1B" + }, +}; + +//========================================================= +// secondary range attack. Overriden because base class stops attacking when the enemy is occluded. +// grunt's grenade toss requires the enemy be occluded. +//========================================================= +Task_t tlGruntRangeAttack2[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_GRUNT_FACE_TOSS_DIR, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_RANGE_ATTACK2 }, + { TASK_SET_SCHEDULE, (float)SCHED_GRUNT_WAIT_FACE_ENEMY },// don't run immediately after throwing grenade. +}; + +Schedule_t slGruntRangeAttack2[] = +{ + { + tlGruntRangeAttack2, + ARRAYSIZE ( tlGruntRangeAttack2 ), + 0, + 0, + "RangeAttack2" + }, +}; + + +//========================================================= +// repel +//========================================================= +Task_t tlGruntRepel[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_IDEAL, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_GLIDE }, +}; + +Schedule_t slGruntRepel[] = +{ + { + tlGruntRepel, + ARRAYSIZE ( tlGruntRepel ), + bits_COND_SEE_ENEMY | + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER | + bits_SOUND_COMBAT | + bits_SOUND_PLAYER, + "Repel" + }, +}; + + +//========================================================= +// repel +//========================================================= +Task_t tlGruntRepelAttack[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_FLY }, +}; + +Schedule_t slGruntRepelAttack[] = +{ + { + tlGruntRepelAttack, + ARRAYSIZE ( tlGruntRepelAttack ), + bits_COND_ENEMY_OCCLUDED, + 0, + "Repel Attack" + }, +}; + +//========================================================= +// repel land +//========================================================= +Task_t tlGruntRepelLand[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_LAND }, + { TASK_GET_PATH_TO_LASTPOSITION,(float)0 }, + { TASK_RUN_PATH, (float)0 }, + { TASK_WAIT_FOR_MOVEMENT, (float)0 }, + { TASK_CLEAR_LASTPOSITION, (float)0 }, +}; + +Schedule_t slGruntRepelLand[] = +{ + { + tlGruntRepelLand, + ARRAYSIZE ( tlGruntRepelLand ), + bits_COND_SEE_ENEMY | + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND, + + bits_SOUND_DANGER | + bits_SOUND_COMBAT | + bits_SOUND_PLAYER, + "Repel Land" + }, +}; + + +DEFINE_CUSTOM_SCHEDULES( CHGrunt ) +{ + slGruntFail, + slGruntCombatFail, + slGruntVictoryDance, + slGruntEstablishLineOfFire, + slGruntFoundEnemy, + slGruntCombatFace, + slGruntSignalSuppress, + slGruntSuppress, + slGruntWaitInCover, + slGruntTakeCover, + slGruntGrenadeCover, + slGruntTossGrenadeCover, + slGruntTakeCoverFromBestSound, + slGruntHideReload, + slGruntSweep, + slGruntRangeAttack1A, + slGruntRangeAttack1B, + slGruntRangeAttack2, + slGruntRepel, + slGruntRepelAttack, + slGruntRepelLand, +}; + +IMPLEMENT_CUSTOM_SCHEDULES( CHGrunt, CSquadMonster ); + +//========================================================= +// SetActivity +//========================================================= +void CHGrunt :: SetActivity ( Activity NewActivity ) +{ + int iSequence = ACTIVITY_NOT_AVAILABLE; + void *pmodel = GET_MODEL_PTR( ENT(pev) ); + + switch ( NewActivity) + { + case ACT_RANGE_ATTACK1: + // grunt is either shooting standing or shooting crouched + if (FBitSet( pev->weapons, HGRUNT_9MMAR)) + { + if ( m_fStanding ) + { + // get aimable sequence + iSequence = LookupSequence( "standing_mp5" ); + } + else + { + // get crouching shoot + iSequence = LookupSequence( "crouching_mp5" ); + } + } + else + { + if ( m_fStanding ) + { + // get aimable sequence + iSequence = LookupSequence( "standing_shotgun" ); + } + else + { + // get crouching shoot + iSequence = LookupSequence( "crouching_shotgun" ); + } + } + break; + case ACT_RANGE_ATTACK2: + // grunt is going to a secondary long range attack. This may be a thrown + // grenade or fired grenade, we must determine which and pick proper sequence + if ( pev->weapons & HGRUNT_HANDGRENADE ) + { + // get toss anim + iSequence = LookupSequence( "throwgrenade" ); + } + // LRC: added a test to stop a marine without a launcher from firing. + else if ( pev->weapons & HGRUNT_GRENADELAUNCHER ) + { + // get launch anim + iSequence = LookupSequence( "launchgrenade" ); + } + else + { + ALERT( at_debug, "No grenades available. "); // flow into the error message we get at the end... + } + break; + case ACT_RUN: + if ( pev->health <= HGRUNT_LIMP_HEALTH ) + { + // limp! + iSequence = LookupActivity ( ACT_RUN_HURT ); + } + else + { + iSequence = LookupActivity ( NewActivity ); + } + break; + case ACT_WALK: + if ( pev->health <= HGRUNT_LIMP_HEALTH ) + { + // limp! + iSequence = LookupActivity ( ACT_WALK_HURT ); + } + else + { + iSequence = LookupActivity ( NewActivity ); + } + break; + case ACT_IDLE: + if ( m_MonsterState == MONSTERSTATE_COMBAT ) + { + NewActivity = ACT_IDLE_ANGRY; + } + iSequence = LookupActivity ( NewActivity ); + break; + default: + iSequence = LookupActivity ( NewActivity ); + break; + } + + m_Activity = NewActivity; // Go ahead and set this so it doesn't keep trying when the anim is not present + + // Set to the desired anim, or default anim if the desired is not present + if ( iSequence > ACTIVITY_NOT_AVAILABLE ) + { + if ( pev->sequence != iSequence || !m_fSequenceLoops ) + { + pev->frame = 0; + } + + pev->sequence = iSequence; // Set to the reset anim (if it's there) + ResetSequenceInfo( ); + SetYawSpeed(); + } + else + { + // Not available try to get default anim + ALERT ( at_debug, "%s has no sequence for act:%d\n", STRING(pev->classname), NewActivity ); + pev->sequence = 0; // Set to the reset anim (if it's there) + } +} + +//========================================================= +// Get Schedule! +//========================================================= +Schedule_t *CHGrunt :: GetSchedule( void ) +{ + + // clear old sentence + m_iSentence = HGRUNT_SENT_NONE; + + // flying? If PRONE, barnacle has me. IF not, it's assumed I am rapelling. + if ( pev->movetype == MOVETYPE_FLY && m_MonsterState != MONSTERSTATE_PRONE ) + { + if (pev->flags & FL_ONGROUND) + { + // just landed + pev->movetype = MOVETYPE_STEP; + return GetScheduleOfType ( SCHED_GRUNT_REPEL_LAND ); + } + else + { + // repel down a rope, + if ( m_MonsterState == MONSTERSTATE_COMBAT ) + return GetScheduleOfType ( SCHED_GRUNT_REPEL_ATTACK ); + else + return GetScheduleOfType ( SCHED_GRUNT_REPEL ); + } + } + + // grunts place HIGH priority on running away from danger sounds. + if ( HasConditions(bits_COND_HEAR_SOUND) ) + { + CSound *pSound; + pSound = PBestSound(); + + ASSERT( pSound != NULL ); + if ( pSound) + { + if (pSound->m_iType & bits_SOUND_DANGER) + { + // dangerous sound nearby! + + //!!!KELLY - currently, this is the grunt's signal that a grenade has landed nearby, + // and the grunt should find cover from the blast + // good place for "SHIT!" or some other colorful verbal indicator of dismay. + // It's not safe to play a verbal order here "Scatter", etc cause + // this may only affect a single individual in a squad. + + if (FOkToSpeak()) + { + SENTENCEG_PlayRndSz( ENT(pev), "HG_GREN", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + JustSpoke(); + } + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_BEST_SOUND ); + } + /* + if (!HasConditions( bits_COND_SEE_ENEMY ) && ( pSound->m_iType & (bits_SOUND_PLAYER | bits_SOUND_COMBAT) )) + { + MakeIdealYaw( pSound->m_vecOrigin ); + } + */ + } + } + switch ( m_MonsterState ) + { + case MONSTERSTATE_COMBAT: + { +// dead enemy + if ( HasConditions( bits_COND_ENEMY_DEAD ) ) + { + // call base class, all code to handle dead enemies is centralized there. + return CBaseMonster :: GetSchedule(); + } + +// new enemy + if ( HasConditions(bits_COND_NEW_ENEMY) ) + { + if ( InSquad() ) + { + MySquadLeader()->m_fEnemyEluded = FALSE; + + if ( !IsLeader() ) + { + return GetScheduleOfType ( SCHED_TAKE_COVER_FROM_ENEMY ); + } + else + { + ALERT(at_aiconsole,"leader spotted player!\n"); + //!!!KELLY - the leader of a squad of grunts has just seen the player or a + // monster and has made it the squad's enemy. You + // can check pev->flags for FL_CLIENT to determine whether this is the player + // or a monster. He's going to immediately start + // firing, though. If you'd like, we can make an alternate "first sight" + // schedule where the leader plays a handsign anim + // that gives us enough time to hear a short sentence or spoken command + // before he starts pluggin away. + if (FOkToSpeak())// && RANDOM_LONG(0,1)) + { + if ((m_hEnemy != NULL) && m_hEnemy->IsPlayer()) + // player + SENTENCEG_PlayRndSz( ENT(pev), "HG_ALERT", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + else if ((m_hEnemy != NULL) && + (m_hEnemy->Classify() != CLASS_PLAYER_ALLY) && + (m_hEnemy->Classify() != CLASS_HUMAN_PASSIVE) && + (m_hEnemy->Classify() != CLASS_MACHINE)) + // monster + SENTENCEG_PlayRndSz( ENT(pev), "HG_MONST", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + + JustSpoke(); + } + + if ( HasConditions ( bits_COND_CAN_RANGE_ATTACK1 ) ) + { + return GetScheduleOfType ( SCHED_GRUNT_SUPPRESS ); + } + else + { + return GetScheduleOfType ( SCHED_GRUNT_ESTABLISH_LINE_OF_FIRE ); + } + } + } + } +// no ammo + else if ( HasConditions ( bits_COND_NO_AMMO_LOADED ) ) + { + //!!!KELLY - this individual just realized he's out of bullet ammo. + // He's going to try to find cover to run to and reload, but rarely, if + // none is available, he'll drop and reload in the open here. + return GetScheduleOfType ( SCHED_GRUNT_COVER_AND_RELOAD ); + } + +// damaged just a little + else if ( HasConditions( bits_COND_LIGHT_DAMAGE ) ) + { + // if hurt: + // 90% chance of taking cover + // 10% chance of flinch. + int iPercent = RANDOM_LONG(0,99); + + if ( iPercent <= 90 && m_hEnemy != NULL ) + { + // only try to take cover if we actually have an enemy! + + //!!!KELLY - this grunt was hit and is going to run to cover. + if (FOkToSpeak()) // && RANDOM_LONG(0,1)) + { + //SENTENCEG_PlayRndSz( ENT(pev), "HG_COVER", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + m_iSentence = HGRUNT_SENT_COVER; + //JustSpoke(); + } + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_ENEMY ); + } + else + { + return GetScheduleOfType( SCHED_SMALL_FLINCH ); + } + } +// can kick + else if ( HasConditions ( bits_COND_CAN_MELEE_ATTACK1 ) ) + { + return GetScheduleOfType ( SCHED_MELEE_ATTACK1 ); + } +// can grenade launch + + else if ( FBitSet( pev->weapons, HGRUNT_GRENADELAUNCHER) && HasConditions ( bits_COND_CAN_RANGE_ATTACK2 ) && OccupySlot( bits_SLOTS_HGRUNT_GRENADE ) ) + { + // shoot a grenade if you can + return GetScheduleOfType( SCHED_RANGE_ATTACK2 ); + } +// can shoot + else if ( HasConditions ( bits_COND_CAN_RANGE_ATTACK1 ) ) + { + if ( InSquad() ) + { + // if the enemy has eluded the squad and a squad member has just located the enemy + // and the enemy does not see the squad member, issue a call to the squad to waste a + // little time and give the player a chance to turn. + if ( MySquadLeader()->m_fEnemyEluded && !HasConditions ( bits_COND_ENEMY_FACING_ME ) ) + { + MySquadLeader()->m_fEnemyEluded = FALSE; + return GetScheduleOfType ( SCHED_GRUNT_FOUND_ENEMY ); + } + } + + if ( OccupySlot ( bits_SLOTS_HGRUNT_ENGAGE ) ) + { + // try to take an available ENGAGE slot + return GetScheduleOfType( SCHED_RANGE_ATTACK1 ); + } + else if ( HasConditions ( bits_COND_CAN_RANGE_ATTACK2 ) && OccupySlot( bits_SLOTS_HGRUNT_GRENADE ) ) + { + // throw a grenade if can and no engage slots are available + return GetScheduleOfType( SCHED_RANGE_ATTACK2 ); + } + else + { + // hide! + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_ENEMY ); + } + } +// can't see enemy + else if ( HasConditions( bits_COND_ENEMY_OCCLUDED ) ) + { + if ( HasConditions( bits_COND_CAN_RANGE_ATTACK2 ) && OccupySlot( bits_SLOTS_HGRUNT_GRENADE ) ) + { + //!!!KELLY - this grunt is about to throw or fire a grenade at the player. Great place for "fire in the hole" "frag out" etc + if (FOkToSpeak()) + { + SENTENCEG_PlayRndSz( ENT(pev), "HG_THROW", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + JustSpoke(); + } + return GetScheduleOfType( SCHED_RANGE_ATTACK2 ); + } + else if ( OccupySlot( bits_SLOTS_HGRUNT_ENGAGE ) ) + { + //!!!KELLY - grunt cannot see the enemy and has just decided to + // charge the enemy's position. + if (FOkToSpeak())// && RANDOM_LONG(0,1)) + { + //SENTENCEG_PlayRndSz( ENT(pev), "HG_CHARGE", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + m_iSentence = HGRUNT_SENT_CHARGE; + //JustSpoke(); + } + + return GetScheduleOfType( SCHED_GRUNT_ESTABLISH_LINE_OF_FIRE ); + } + else + { + //!!!KELLY - grunt is going to stay put for a couple seconds to see if + // the enemy wanders back out into the open, or approaches the + // grunt's covered position. Good place for a taunt, I guess? + if (FOkToSpeak() && RANDOM_LONG(0,1)) + { + SENTENCEG_PlayRndSz( ENT(pev), "HG_TAUNT", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + JustSpoke(); + } + return GetScheduleOfType( SCHED_STANDOFF ); + } + } + + if ( HasConditions( bits_COND_SEE_ENEMY ) && !HasConditions ( bits_COND_CAN_RANGE_ATTACK1 ) ) + { + return GetScheduleOfType ( SCHED_GRUNT_ESTABLISH_LINE_OF_FIRE ); + } + } + } + + // no special cases here, call the base class + return CSquadMonster :: GetSchedule(); +} + +//========================================================= +//========================================================= +Schedule_t* CHGrunt :: GetScheduleOfType ( int Type ) +{ + switch ( Type ) + { + case SCHED_TAKE_COVER_FROM_ENEMY: + { + if ( InSquad() ) + { + if (HasConditions( bits_COND_CAN_RANGE_ATTACK2 ) && OccupySlot( bits_SLOTS_HGRUNT_GRENADE ) ) + { + if (FOkToSpeak()) + { + SENTENCEG_PlayRndSz( ENT(pev), "HG_THROW", HGRUNT_SENTENCE_VOLUME, GRUNT_ATTN, 0, m_voicePitch); + JustSpoke(); + } + return slGruntTossGrenadeCover; + } + else + { + return &slGruntTakeCover[ 0 ]; + } + } + else + { + if ( OccupySlot( bits_SLOTS_HGRUNT_GRENADE ) && RANDOM_LONG(0,1) ) + { + return &slGruntGrenadeCover[ 0 ]; + } + else + { + return &slGruntTakeCover[ 0 ]; + } + } + } + case SCHED_TAKE_COVER_FROM_BEST_SOUND: + { + return &slGruntTakeCoverFromBestSound[ 0 ]; + } + case SCHED_GRUNT_TAKECOVER_FAILED: + { + if ( HasConditions( bits_COND_CAN_RANGE_ATTACK1 ) && OccupySlot( bits_SLOTS_HGRUNT_ENGAGE ) ) + { + return GetScheduleOfType( SCHED_RANGE_ATTACK1 ); + } + + return GetScheduleOfType ( SCHED_FAIL ); + } + break; + case SCHED_GRUNT_ELOF_FAIL: + { + // human grunt is unable to move to a position that allows him to attack the enemy. + return GetScheduleOfType ( SCHED_TAKE_COVER_FROM_ENEMY ); + } + break; + case SCHED_GRUNT_ESTABLISH_LINE_OF_FIRE: + { + return &slGruntEstablishLineOfFire[ 0 ]; + } + break; + case SCHED_RANGE_ATTACK1: + { + // randomly stand or crouch + if (RANDOM_LONG(0,9) == 0) + m_fStanding = RANDOM_LONG(0,1); + + if (m_fStanding) + return &slGruntRangeAttack1B[ 0 ]; + else + return &slGruntRangeAttack1A[ 0 ]; + } + case SCHED_RANGE_ATTACK2: + { + return &slGruntRangeAttack2[ 0 ]; + } + case SCHED_COMBAT_FACE: + { + return &slGruntCombatFace[ 0 ]; + } + case SCHED_GRUNT_WAIT_FACE_ENEMY: + { + return &slGruntWaitInCover[ 0 ]; + } + case SCHED_GRUNT_SWEEP: + { + return &slGruntSweep[ 0 ]; + } + case SCHED_GRUNT_COVER_AND_RELOAD: + { + return &slGruntHideReload[ 0 ]; + } + case SCHED_GRUNT_FOUND_ENEMY: + { + return &slGruntFoundEnemy[ 0 ]; + } + case SCHED_VICTORY_DANCE: + { + if ( InSquad() ) + { + if ( !IsLeader() ) + { + return &slGruntFail[ 0 ]; + } + } + + return &slGruntVictoryDance[ 0 ]; + } + case SCHED_GRUNT_SUPPRESS: + { + if ( m_hEnemy->IsPlayer() && m_fFirstEncounter ) + { + m_fFirstEncounter = FALSE;// after first encounter, leader won't issue handsigns anymore when he has a new enemy + return &slGruntSignalSuppress[ 0 ]; + } + else + { + return &slGruntSuppress[ 0 ]; + } + } + case SCHED_FAIL: + { + if ( m_hEnemy != NULL ) + { + // grunt has an enemy, so pick a different default fail schedule most likely to help recover. + return &slGruntCombatFail[ 0 ]; + } + + return &slGruntFail[ 0 ]; + } + case SCHED_GRUNT_REPEL: + { + if (pev->velocity.z > -128) + pev->velocity.z -= 32; + return &slGruntRepel[ 0 ]; + } + case SCHED_GRUNT_REPEL_ATTACK: + { + if (pev->velocity.z > -128) + pev->velocity.z -= 32; + return &slGruntRepelAttack[ 0 ]; + } + case SCHED_GRUNT_REPEL_LAND: + { + return &slGruntRepelLand[ 0 ]; + } + default: + { + return CSquadMonster :: GetScheduleOfType ( Type ); + } + } +} + + +//========================================================= +// CHGruntRepel - when triggered, spawns a monster_human_grunt +// repelling down a line. +//========================================================= + +class CHGruntRepel : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void EXPORT RepelUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + int m_iSpriteTexture; // Don't save, precache +}; + +LINK_ENTITY_TO_CLASS( monster_grunt_repel, CHGruntRepel ); + +void CHGruntRepel::Spawn( void ) +{ + Precache( ); + pev->solid = SOLID_NOT; + + SetUse(&CHGruntRepel:: RepelUse ); +} + +void CHGruntRepel::Precache( void ) +{ + UTIL_PrecacheEntity( "monster_human_grunt" ); + m_iSpriteTexture = PRECACHE_MODEL( "sprites/rope.spr" ); +} + +void CHGruntRepel::RepelUse ( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + TraceResult tr; + UTIL_TraceLine( pev->origin, pev->origin + Vector( 0, 0, -4096.0), dont_ignore_monsters, ENT(pev), &tr); + /* + if ( tr.pHit && Instance( tr.pHit )->pev->solid != SOLID_BSP) + return NULL; + */ + + CBaseEntity *pEntity = Create( "monster_human_grunt", pev->origin, pev->angles ); + CBaseMonster *pGrunt = pEntity->MyMonsterPointer( ); + pGrunt->pev->movetype = MOVETYPE_FLY; + pGrunt->pev->velocity = Vector( 0, 0, RANDOM_FLOAT( -196, -128 ) ); + pGrunt->SetActivity( ACT_GLIDE ); + // UNDONE: position? + pGrunt->m_vecLastPosition = tr.vecEndPos; + + CBeam *pBeam = CBeam::BeamCreate( "sprites/rope.spr", 10 ); + pBeam->PointEntInit( pev->origin + Vector(0,0,112), pGrunt->entindex() ); + pBeam->SetFlags( BEAM_FSOLID ); + pBeam->SetColor( 255, 255, 255 ); + pBeam->SetThink( Remove ); + pBeam->SetNextThink( -4096.0 * tr.flFraction / pGrunt->pev->velocity.z + 0.5 ); + + UTIL_Remove( this ); +} + + + +//========================================================= +// DEAD HGRUNT PROP +//========================================================= +class CDeadHGrunt : public CBaseMonster +{ +public: + void Spawn( void ); + int Classify ( void ) { return CLASS_HUMAN_MILITARY; } + + void KeyValue( KeyValueData *pkvd ); + + int m_iPose;// which sequence to display -- temporary, don't need to save + static char *m_szPoses[3]; +}; + +char *CDeadHGrunt::m_szPoses[] = { "deadstomach", "deadside", "deadsitting" }; + +void CDeadHGrunt::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "pose")) + { + m_iPose = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseMonster::KeyValue( pkvd ); +} + +LINK_ENTITY_TO_CLASS( monster_hgrunt_dead, CDeadHGrunt ); + +//========================================================= +// ********** DeadHGrunt SPAWN ********** +//========================================================= +void CDeadHGrunt :: Spawn( void ) +{ + int oldBody; + + PRECACHE_MODEL("models/hgrunt.mdl"); + SET_MODEL(ENT(pev), "models/hgrunt.mdl"); + + pev->effects = 0; + pev->yaw_speed = 8; + pev->sequence = 0; + m_bloodColor = BLOOD_COLOR_RED; + + pev->sequence = LookupSequence( m_szPoses[m_iPose] ); + + if (pev->sequence == -1) + { + ALERT ( at_debug, "Dead hgrunt with bad pose\n" ); + } + + // Corpses have less health + pev->health = 8; + + oldBody = pev->body; + pev->body = 0; + + if (oldBody >= 5 && oldBody <= 7) + pev->skin = 1; + else + pev->skin = 0; + + switch( pev->weapons ) + { + case 0: // MP5 + SetBodygroup( GUN_GROUP, GUN_MP5 ); + break; + case 1: // Shotgun + SetBodygroup( GUN_GROUP, GUN_SHOTGUN ); + break; + case 2: // No gun + SetBodygroup( GUN_GROUP, GUN_NONE ); + break; + } + + switch( oldBody ) + { + case 2: // Gasmask, no gun + SetBodygroup( GUN_GROUP, GUN_NONE ); //fall through + case 0: case 6: // Gasmask (white/black) + SetBodygroup( HEAD_GROUP, HEAD_GRUNT ); + break; + case 3: // Commander, no gun + SetBodygroup( GUN_GROUP, GUN_NONE ); //fall through + case 1: // Commander + SetBodygroup( HEAD_GROUP, HEAD_COMMANDER ); + break; + case 4: case 7: // Skimask (white/black) + SetBodygroup( HEAD_GROUP, HEAD_SHOTGUN ); + break; + case 5: // Commander + SetBodygroup( HEAD_GROUP, HEAD_M203 ); + break; + } + + MonsterInitDead(); +} diff --git a/server/monsters/leech.cpp b/server/monsters/leech.cpp new file mode 100644 index 00000000..95c002d0 --- /dev/null +++ b/server/monsters/leech.cpp @@ -0,0 +1,743 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// leech - basic little swimming monster +//========================================================= +// +// UNDONE: +// DONE:Steering force model for attack +// DONE:Attack animation control / damage +// DONE:Establish range of up/down motion and steer around vertical obstacles +// DONE:Re-evaluate height periodically +// DONE:Fall (MOVETYPE_TOSS) and play different anim if out of water +// Test in complex room (c2a3?) +// DONE:Sounds? - Kelly will fix +// Blood cloud? Hurt effect? +// Group behavior? +// DONE:Save/restore +// Flop animation - just bind to ACT_TWITCH +// Fix fatal push into wall case +// +// Try this on a bird +// Try this on a model with hulls/tracehull? +// + + +#include "float.h" +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "defaults.h" + + + +// Animation events +#define LEECH_AE_ATTACK 1 +#define LEECH_AE_FLOP 2 + + +// Movement constants + +#define LEECH_ACCELERATE 10 +#define LEECH_CHECK_DIST 45 +#define LEECH_SWIM_SPEED 50 +#define LEECH_SWIM_ACCEL 80 +#define LEECH_SWIM_DECEL 10 +#define LEECH_TURN_RATE 90 +#define LEECH_SIZEX 10 +#define LEECH_FRAMETIME 0.1 + + + +#define DEBUG_BEAMS 0 + +#if DEBUG_BEAMS +#include "basebeams.h" +#endif + + +class CLeech : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + + void EXPORT SwimThink( void ); + void EXPORT DeadThink( void ); + void Touch( CBaseEntity *pOther ) + { + if ( pOther->IsPlayer() ) + { + // If the client is pushing me, give me some velocity + if( gpGlobals->trace_ent && gpGlobals->trace_ent == edict() ) + { + pev->velocity = pOther->pev->velocity; + } + } + } + + void SetObjectCollisionBox( void ) + { + pev->absmin = pev->origin + Vector(-8,-8,0); + pev->absmax = pev->origin + Vector(8,8,2); + } + + void AttackSound( void ); + void AlertSound( void ); + void UpdateMotion( void ); + float ObstacleDistance( CBaseEntity *pTarget ); + void MakeVectors( void ); + void RecalculateWaterlevel( void ); + void SwitchLeechState( void ); + + // Base entity functions + void HandleAnimEvent( MonsterEvent_t *pEvent ); + int BloodColor( void ) { return DONT_BLEED; } + void Killed( entvars_t *pevAttacker, int iGib ); + void Activate( void ); + int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ); + int Classify( void ) { return CLASS_INSECT; } + int IRelationship( CBaseEntity *pTarget ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + static const char *pAttackSounds[]; + static const char *pAlertSounds[]; + +private: + // UNDONE: Remove unused boid vars, do group behavior + float m_flTurning;// is this boid turning? + BOOL m_fPathBlocked;// TRUE if there is an obstacle ahead + float m_flAccelerate; + float m_obstacle; + float m_top; + float m_bottom; + float m_height; + float m_waterTime; + float m_sideTime; // Timer to randomly check clearance on sides + float m_zTime; + float m_stateTime; + float m_attackSoundTime; + +#if DEBUG_BEAMS + CBeam *m_pb; + CBeam *m_pt; +#endif +}; + + + +LINK_ENTITY_TO_CLASS( monster_leech, CLeech ); + +TYPEDESCRIPTION CLeech::m_SaveData[] = +{ + DEFINE_FIELD( CLeech, m_flTurning, FIELD_FLOAT ), + DEFINE_FIELD( CLeech, m_fPathBlocked, FIELD_BOOLEAN ), + DEFINE_FIELD( CLeech, m_flAccelerate, FIELD_FLOAT ), + DEFINE_FIELD( CLeech, m_obstacle, FIELD_FLOAT ), + DEFINE_FIELD( CLeech, m_top, FIELD_FLOAT ), + DEFINE_FIELD( CLeech, m_bottom, FIELD_FLOAT ), + DEFINE_FIELD( CLeech, m_height, FIELD_FLOAT ), + DEFINE_FIELD( CLeech, m_waterTime, FIELD_TIME ), + DEFINE_FIELD( CLeech, m_sideTime, FIELD_TIME ), + DEFINE_FIELD( CLeech, m_zTime, FIELD_TIME ), + DEFINE_FIELD( CLeech, m_stateTime, FIELD_TIME ), + DEFINE_FIELD( CLeech, m_attackSoundTime, FIELD_TIME ), +}; + +IMPLEMENT_SAVERESTORE( CLeech, CBaseMonster ); + + +const char *CLeech::pAttackSounds[] = +{ + "leech/leech_bite1.wav", + "leech/leech_bite2.wav", + "leech/leech_bite3.wav", +}; + +const char *CLeech::pAlertSounds[] = +{ + "leech/leech_alert1.wav", + "leech/leech_alert2.wav", +}; + + +void CLeech::Spawn( void ) +{ + Precache(); + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/leech.mdl"); + // Just for fun + // SET_MODEL(ENT(pev), "models/icky.mdl"); + +// UTIL_SetSize( pev, g_vecZero, g_vecZero ); + UTIL_SetSize( pev, Vector(-1,-1,0), Vector(1,1,2)); + // Don't push the minz down too much or the water check will fail because this entity is really point-sized + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_FLY; + SetBits( pev->flags, FL_SWIM ); + if (pev->health == 0) + pev->health = LEECH_HEALTH; + + m_flFieldOfView = -0.5; // 180 degree FOV + m_flDistLook = 750; + MonsterInit(); + SetThink(&CLeech:: SwimThink ); + SetUse( NULL ); + SetTouch( NULL ); + pev->view_ofs = g_vecZero; + + m_flTurning = 0; + m_fPathBlocked = FALSE; + SetActivity( ACT_SWIM ); + SetState( MONSTERSTATE_IDLE ); + m_stateTime = gpGlobals->time + RANDOM_FLOAT( 1, 5 ); +} + + +void CLeech::Activate( void ) +{ + RecalculateWaterlevel(); + CBaseMonster::Activate(); +} + + + +void CLeech::RecalculateWaterlevel( void ) +{ + // Calculate boundaries + Vector vecTest = pev->origin - Vector(0,0,400); + + TraceResult tr; + + UTIL_TraceLine(pev->origin, vecTest, missile, edict(), &tr); + if ( tr.flFraction != 1.0 ) + m_bottom = tr.vecEndPos.z + 1; + else + m_bottom = vecTest.z; + + m_top = UTIL_WaterLevel( pev->origin, pev->origin.z, pev->origin.z + 400 ) - 1; + + // Chop off 20% of the outside range + float newBottom = m_bottom * 0.8 + m_top * 0.2; + m_top = m_bottom * 0.2 + m_top * 0.8; + m_bottom = newBottom; + m_height = RANDOM_FLOAT( m_bottom, m_top ); + m_waterTime = gpGlobals->time + RANDOM_FLOAT( 5, 7 ); +} + + +void CLeech::SwitchLeechState( void ) +{ + m_stateTime = gpGlobals->time + RANDOM_FLOAT( 3, 6 ); + if ( m_MonsterState == MONSTERSTATE_COMBAT ) + { + m_hEnemy = NULL; + SetState( MONSTERSTATE_IDLE ); + // We may be up against the player, so redo the side checks + m_sideTime = 0; + } + else + { + Look( m_flDistLook ); + CBaseEntity *pEnemy = BestVisibleEnemy(); + if ( pEnemy && pEnemy->pev->waterlevel != 0 && pEnemy->pev->watertype != CONTENTS_FOG ) + { + m_hEnemy = pEnemy; + SetState( MONSTERSTATE_COMBAT ); + m_stateTime = gpGlobals->time + RANDOM_FLOAT( 18, 25 ); + AlertSound(); + } + } +} + + +int CLeech::IRelationship( CBaseEntity *pTarget ) +{ + if ( pTarget->IsPlayer() ) + return R_DL; + return CBaseMonster::IRelationship( pTarget ); +} + + + +void CLeech::AttackSound( void ) +{ + if ( gpGlobals->time > m_attackSoundTime ) + { + EMIT_SOUND_DYN ( ENT(pev), CHAN_VOICE, pAttackSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackSounds)-1) ], 1.0, ATTN_NORM, 0, PITCH_NORM ); + m_attackSoundTime = gpGlobals->time + 0.5; + } +} + + +void CLeech::AlertSound( void ) +{ + EMIT_SOUND_DYN ( ENT(pev), CHAN_VOICE, pAlertSounds[ RANDOM_LONG(0,ARRAYSIZE(pAlertSounds)-1) ], 1.0, ATTN_NORM * 0.5, 0, PITCH_NORM ); +} + + +void CLeech::Precache( void ) +{ + int i; + + //PRECACHE_MODEL("models/icky.mdl"); + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/leech.mdl"); + + for ( i = 0; i < ARRAYSIZE( pAttackSounds ); i++ ) + PRECACHE_SOUND((char *)pAttackSounds[i]); + for ( i = 0; i < ARRAYSIZE( pAlertSounds ); i++ ) + PRECACHE_SOUND((char *)pAlertSounds[i]); +} + + +int CLeech::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + pev->velocity = g_vecZero; + + // Nudge the leech away from the damage + if ( pevInflictor ) + { + pev->velocity = (pev->origin - pevInflictor->origin).Normalize() * 25; + } + else if ( pev->movetype == MOVETYPE_TOSS ) + { + ALERT(at_console, "Waterlevel is out\n" ); +// if ( RANDOM_LONG( 0, 99 ) < 1 ) + pev->dmg += 2; + + + } + return CBaseMonster::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType ); +} + + +void CLeech::HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case LEECH_AE_ATTACK: + AttackSound(); + CBaseEntity *pEnemy; + + pEnemy = m_hEnemy; + if ( pEnemy != NULL ) + { + Vector dir, face; + + UTIL_MakeVectorsPrivate( pev->angles, face, NULL, NULL ); + face.z = 0; + dir = (pEnemy->pev->origin - pev->origin); + dir.z = 0; + dir = dir.Normalize(); + face = face.Normalize(); + + + if ( DotProduct(dir, face) > 0.9 ) // Only take damage if the leech is facing the prey + pEnemy->TakeDamage( pev, pev, LEECH_DMG_BITE, DMG_SLASH ); + } + m_stateTime -= 2; + break; + + case LEECH_AE_FLOP: + // Play flop sound + break; + + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + + +void CLeech::MakeVectors( void ) +{ + Vector tmp = pev->angles; + tmp.x = -tmp.x; + UTIL_MakeVectors ( tmp ); +} + + +// +// ObstacleDistance - returns normalized distance to obstacle +// +float CLeech::ObstacleDistance( CBaseEntity *pTarget ) +{ + TraceResult tr; + Vector vecTest; + + // use VELOCITY, not angles, not all boids point the direction they are flying + //Vector vecDir = UTIL_VecToAngles( pev->velocity ); + MakeVectors(); + + // check for obstacle ahead + vecTest = pev->origin + gpGlobals->v_forward * LEECH_CHECK_DIST; + UTIL_TraceLine(pev->origin, vecTest, missile, edict(), &tr); + + if ( tr.fStartSolid ) + { + pev->speed = -LEECH_SWIM_SPEED * 0.5; +// ALERT( at_console, "Stuck from (%f %f %f) to (%f %f %f)\n", pev->oldorigin.x, pev->oldorigin.y, pev->oldorigin.z, pev->origin.x, pev->origin.y, pev->origin.z ); +// UTIL_SetOrigin( pev, pev->oldorigin ); + } + + if ( tr.flFraction != 1.0 ) + { + if ( (pTarget == NULL || tr.pHit != pTarget->edict()) ) + { + return tr.flFraction; + } + else + { + if ( fabs(m_height - pev->origin.z) > 10 ) + return tr.flFraction; + } + } + + if ( m_sideTime < gpGlobals->time ) + { + // extra wide checks + vecTest = pev->origin + gpGlobals->v_right * LEECH_SIZEX * 2 + gpGlobals->v_forward * LEECH_CHECK_DIST; + UTIL_TraceLine(pev->origin, vecTest, missile, edict(), &tr); + if (tr.flFraction != 1.0) + return tr.flFraction; + + vecTest = pev->origin - gpGlobals->v_right * LEECH_SIZEX * 2 + gpGlobals->v_forward * LEECH_CHECK_DIST; + UTIL_TraceLine(pev->origin, vecTest, missile, edict(), &tr); + if (tr.flFraction != 1.0) + return tr.flFraction; + + // Didn't hit either side, so stop testing for another 0.5 - 1 seconds + m_sideTime = gpGlobals->time + RANDOM_FLOAT(0.5,1); + } + return 1.0; +} + + +void CLeech::DeadThink( void ) +{ + if ( m_fSequenceFinished ) + { + if ( m_Activity == ACT_DIEFORWARD ) + { + SetThink( NULL ); + StopAnimation(); + return; + } + else if ( pev->flags & FL_ONGROUND ) + { + pev->solid = SOLID_NOT; + SetActivity(ACT_DIEFORWARD); + } + } + StudioFrameAdvance(); + SetNextThink( 0.1 ); + + // Apply damage velocity, but keep out of the walls + if ( pev->velocity.x != 0 || pev->velocity.y != 0 ) + { + TraceResult tr; + + // Look 0.5 seconds ahead + UTIL_TraceLine(pev->origin, pev->origin + pev->velocity * 0.5, missile, edict(), &tr); + if (tr.flFraction != 1.0) + { + pev->velocity.x = 0; + pev->velocity.y = 0; + } + } +} + + + +void CLeech::UpdateMotion( void ) +{ + float flapspeed = (pev->speed - m_flAccelerate) / LEECH_ACCELERATE; + m_flAccelerate = m_flAccelerate * 0.8 + pev->speed * 0.2; + + if (flapspeed < 0) + flapspeed = -flapspeed; + flapspeed += 1.0; + if (flapspeed < 0.5) + flapspeed = 0.5; + if (flapspeed > 1.9) + flapspeed = 1.9; + + pev->framerate = flapspeed; + + if ( !m_fPathBlocked ) + pev->avelocity.y = pev->ideal_yaw; + else + pev->avelocity.y = pev->ideal_yaw * m_obstacle; + + if ( pev->avelocity.y > 150 ) + m_IdealActivity = ACT_TURN_LEFT; + else if ( pev->avelocity.y < -150 ) + m_IdealActivity = ACT_TURN_RIGHT; + else + m_IdealActivity = ACT_SWIM; + + // lean + float targetPitch, delta; + delta = m_height - pev->origin.z; + + if ( delta < -10 ) + targetPitch = -30; + else if ( delta > 10 ) + targetPitch = 30; + else + targetPitch = 0; + + pev->angles.x = UTIL_Approach( targetPitch, pev->angles.x, 60 * LEECH_FRAMETIME ); + + // bank + pev->avelocity.z = - (pev->angles.z + (pev->avelocity.y * 0.25)); + + if ( m_MonsterState == MONSTERSTATE_COMBAT && HasConditions( bits_COND_CAN_MELEE_ATTACK1 ) ) + m_IdealActivity = ACT_MELEE_ATTACK1; + + // Out of water check + if ( !pev->waterlevel || pev->watertype == CONTENTS_FOG ) + { + pev->movetype = MOVETYPE_TOSS; + m_IdealActivity = ACT_TWITCH; + pev->velocity = g_vecZero; + + // Animation will intersect the floor if either of these is non-zero + pev->angles.z = 0; + pev->angles.x = 0; + + if ( pev->framerate < 1.0 ) + pev->framerate = 1.0; + } + else if ( pev->movetype == MOVETYPE_TOSS ) + { + pev->movetype = MOVETYPE_FLY; + pev->flags &= ~FL_ONGROUND; + RecalculateWaterlevel(); + ALERT(at_console, "Waterlevel is out\n" ); + if ( RANDOM_LONG( 0, 99 ) < 1 ) + { + pev->gravity = 0.02; + pev->takedamage += 2; + } + m_waterTime = gpGlobals->time + 2; // Recalc again soon, water may be rising + } + + if ( m_Activity != m_IdealActivity ) + { + SetActivity ( m_IdealActivity ); + } + float flInterval = StudioFrameAdvance(); + DispatchAnimEvents ( flInterval ); + +#if DEBUG_BEAMS + if ( !m_pb ) + m_pb = CBeam::BeamCreate( "sprites/laserbeam.spr", 5 ); + if ( !m_pt ) + m_pt = CBeam::BeamCreate( "sprites/laserbeam.spr", 5 ); + m_pb->PointsInit( pev->origin, pev->origin + gpGlobals->v_forward * LEECH_CHECK_DIST ); + m_pt->PointsInit( pev->origin, pev->origin - gpGlobals->v_right * (pev->avelocity.y*0.25) ); + if ( m_fPathBlocked ) + { + float color = m_obstacle * 30; + if ( m_obstacle == 1.0 ) + color = 0; + if ( color > 255 ) + color = 255; + m_pb->SetColor( 255, (int)color, (int)color ); + } + else + m_pb->SetColor( 255, 255, 0 ); + m_pt->SetColor( 0, 0, 255 ); +#endif +} + + +void CLeech::SwimThink( void ) +{ + TraceResult tr; + float flLeftSide; + float flRightSide; + float targetSpeed; + float targetYaw = 0; + CBaseEntity *pTarget; + + if ( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) && !HaveCamerasInPVS( edict() )) + { + SetNextThink( RANDOM_FLOAT(1,1.5) ); + pev->velocity = g_vecZero; + return; + } + else + SetNextThink( 0.1 ); + + targetSpeed = LEECH_SWIM_SPEED; + + if ( m_waterTime < gpGlobals->time ) + RecalculateWaterlevel(); + + if ( m_stateTime < gpGlobals->time ) + SwitchLeechState(); + + ClearConditions( bits_COND_CAN_MELEE_ATTACK1 ); + switch( m_MonsterState ) + { + case MONSTERSTATE_COMBAT: + pTarget = m_hEnemy; + if ( !pTarget ) + SwitchLeechState(); + else + { + // Chase the enemy's eyes + m_height = pTarget->pev->origin.z + pTarget->pev->view_ofs.z - 5; + // Clip to viable water area + if ( m_height < m_bottom ) + m_height = m_bottom; + else if ( m_height > m_top ) + m_height = m_top; + Vector location = pTarget->pev->origin - pev->origin; + location.z += (pTarget->pev->view_ofs.z); + if ( location.Length() < 40 ) + SetConditions( bits_COND_CAN_MELEE_ATTACK1 ); + // Turn towards target ent + targetYaw = UTIL_VecToYaw( location ); + + targetYaw = UTIL_AngleDiff( targetYaw, UTIL_AngleMod( pev->angles.y ) ); + + if ( targetYaw < (-LEECH_TURN_RATE*0.75) ) + targetYaw = (-LEECH_TURN_RATE*0.75); + else if ( targetYaw > (LEECH_TURN_RATE*0.75) ) + targetYaw = (LEECH_TURN_RATE*0.75); + else + targetSpeed *= 2; + } + + break; + + default: + if ( m_zTime < gpGlobals->time ) + { + float newHeight = RANDOM_FLOAT( m_bottom, m_top ); + m_height = 0.5 * m_height + 0.5 * newHeight; + m_zTime = gpGlobals->time + RANDOM_FLOAT( 1, 4 ); + } + if ( RANDOM_LONG( 0, 100 ) < 10 ) + targetYaw = RANDOM_LONG( -30, 30 ); + pTarget = NULL; + // oldorigin test + if ( (pev->origin - pev->oldorigin).Length() < 1 ) + { + // If leech didn't move, there must be something blocking it, so try to turn + m_sideTime = 0; + } + + break; + } + + m_obstacle = ObstacleDistance( pTarget ); + pev->oldorigin = pev->origin; + if ( m_obstacle < 0.1 ) + m_obstacle = 0.1; + + // is the way ahead clear? + if ( m_obstacle == 1.0 ) + { + // if the leech is turning, stop the trend. + if ( m_flTurning != 0 ) + { + m_flTurning = 0; + } + + m_fPathBlocked = FALSE; + pev->speed = UTIL_Approach( targetSpeed, pev->speed, LEECH_SWIM_ACCEL * LEECH_FRAMETIME ); + pev->velocity = gpGlobals->v_forward * pev->speed; + + } + else + { + m_obstacle = 1.0 / m_obstacle; + // IF we get this far in the function, the leader's path is blocked! + m_fPathBlocked = TRUE; + + if ( m_flTurning == 0 )// something in the way and leech is not already turning to avoid + { + Vector vecTest; + // measure clearance on left and right to pick the best dir to turn + vecTest = pev->origin + (gpGlobals->v_right * LEECH_SIZEX) + (gpGlobals->v_forward * LEECH_CHECK_DIST); + UTIL_TraceLine(pev->origin, vecTest, missile, edict(), &tr); + flRightSide = tr.flFraction; + + vecTest = pev->origin + (gpGlobals->v_right * -LEECH_SIZEX) + (gpGlobals->v_forward * LEECH_CHECK_DIST); + UTIL_TraceLine(pev->origin, vecTest, missile, edict(), &tr); + flLeftSide = tr.flFraction; + + // turn left, right or random depending on clearance ratio + float delta = (flRightSide - flLeftSide); + if ( delta > 0.1 || (delta > -0.1 && RANDOM_LONG(0,100)<50) ) + m_flTurning = -LEECH_TURN_RATE; + else + m_flTurning = LEECH_TURN_RATE; + } + pev->speed = UTIL_Approach( -(LEECH_SWIM_SPEED*0.5), pev->speed, LEECH_SWIM_DECEL * LEECH_FRAMETIME * m_obstacle ); + pev->velocity = gpGlobals->v_forward * pev->speed; + } + pev->ideal_yaw = m_flTurning + targetYaw; + UpdateMotion(); +} + + +void CLeech::Killed(entvars_t *pevAttacker, int iGib) +{ + Vector vecSplatDir; + TraceResult tr; + + ALERT(at_aiconsole, "Leech: killed\n"); + // tell owner ( if any ) that we're dead.This is mostly for MonsterMaker functionality. + CBaseEntity *pOwner = CBaseEntity::Instance(pev->owner); + if (pOwner) + pOwner->DeathNotice(pev); + + // When we hit the ground, play the "death_end" activity + if( pev->waterlevel && pev->watertype != CONTENTS_FOG ) + { + pev->angles.z = 0; + pev->angles.x = 0; + pev->origin.z += 1; + pev->avelocity = g_vecZero; + if ( RANDOM_LONG( 0, 99 ) < 70 ) + pev->avelocity.y = RANDOM_LONG( -720, 720 ); + + pev->gravity = 0.02; + ClearBits(pev->flags, FL_ONGROUND); + SetActivity( ACT_DIESIMPLE ); + } + else + SetActivity( ACT_DIEFORWARD ); + + pev->movetype = MOVETYPE_TOSS; + pev->takedamage = DAMAGE_NO; + SetThink(&CLeech:: DeadThink ); +} + + diff --git a/server/monsters/monsterevent.h b/server/monsters/monsterevent.h new file mode 100644 index 00000000..58357e18 --- /dev/null +++ b/server/monsters/monsterevent.h @@ -0,0 +1,34 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef MONSTEREVENT_H +#define MONSTEREVENT_H + +typedef struct +{ + int event; + char *options; +} MonsterEvent_t; + +#define EVENT_SPECIFIC 0 +#define EVENT_SCRIPTED 1000 +#define EVENT_SHARED 2000 +#define EVENT_CLIENT 5000 + +#define MONSTER_EVENT_BODYDROP_LIGHT 2001 +#define MONSTER_EVENT_BODYDROP_HEAVY 2002 + +#define MONSTER_EVENT_SWISHSOUND 2010 + +#endif // MONSTEREVENT_H diff --git a/server/monsters/monsters.h b/server/monsters/monsters.h new file mode 100644 index 00000000..5d1beafa --- /dev/null +++ b/server/monsters/monsters.h @@ -0,0 +1,163 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#ifndef MONSTERS_H +#define MONSTERS_H + +/* + +===== monsters.h ======================================================== + + Header file for monster-related utility code + +*/ + +#define MAX_PATH_SIZE 10 // max number of nodes available for a path. + +// CHECKLOCALMOVE result types +#define LOCALMOVE_INVALID 0 // move is not possible +#define LOCALMOVE_INVALID_DONT_TRIANGULATE 1 // move is not possible, don't try to triangulate +#define LOCALMOVE_VALID 2 // move is possible + +// Hit Group standards +#define HITGROUP_GENERIC 0 +#define HITGROUP_HEAD 1 +#define HITGROUP_CHEST 2 +#define HITGROUP_STOMACH 3 +#define HITGROUP_LEFTARM 4 +#define HITGROUP_RIGHTARM 5 +#define HITGROUP_LEFTLEG 6 +#define HITGROUP_RIGHTLEG 7 + + +// Monster Spawnflags +#define SF_MONSTER_WAIT_TILL_SEEN 1// spawnflag that makes monsters wait until player can see them before attacking. +#define SF_MONSTER_GAG 2 // no idle noises from this monster +#define SF_MONSTER_HITMONSTERCLIP 4 +// 8 +#define SF_MONSTER_PRISONER 16 // monster won't attack anyone, no one will attacke him. +// 32 +// 64 +#define SF_MONSTER_NO_YELLOW_BLOBS 128 //LRC- if the monster is stuck, don't give errors or show yellow blobs. +//LRC- wasn't implemented. #define SF_MONSTER_WAIT_FOR_SCRIPT 128 //spawnflag that makes monsters wait to check for attacking until the script is done or they've been attacked +#define SF_MONSTER_PREDISASTER 256 //this is a predisaster scientist or barney. Influences how they speak. +#define SF_MONSTER_NO_WPN_DROP 1024 //LRC- never drop your weapon (player can't pick it up.) +//LRC - this clashes with 'not in deathmatch'. Replaced with m_iPlayerReact. +//#define SF_MONSTER_INVERT_PLAYERREACT 2048 //LRC- if this monster would usually attack the player, don't attack unless provoked. If you would usually NOT attack the player, attack him. +#define SF_MONSTER_FALL_TO_GROUND 0x80000000 + +// specialty spawnflags +#define SF_MONSTER_TURRET_AUTOACTIVATE 32 +#define SF_MONSTER_TURRET_STARTINACTIVE 64 +#define SF_MONSTER_WAIT_UNTIL_PROVOKED 64 // don't attack the player unless provoked + + + +// MoveToOrigin stuff +#define MOVE_START_TURN_DIST 64 // when this far away from moveGoal, start turning to face next goal +#define MOVE_STUCK_DIST 32 // if a monster can't step this far, it is stuck. + + +// MoveToOrigin stuff +#define MOVE_NORMAL 0// normal move in the direction monster is facing +#define MOVE_STRAFE 1// moves in direction specified, no matter which way monster is facing + +// spawn flags 256 and above are already taken by the engine +extern void UTIL_MoveToOrigin( edict_t* pent, const Vector &vecGoal, float flDist, int iMoveType ); + +Vector VecCheckToss ( entvars_t *pev, const Vector &vecSpot1, Vector vecSpot2, float flGravityAdj = 1.0 ); +Vector VecCheckThrow ( entvars_t *pev, const Vector &vecSpot1, Vector vecSpot2, float flSpeed, float flGravityAdj = 1.0 ); +extern DLL_GLOBAL Vector g_vecAttackDir; +extern DLL_GLOBAL CONSTANT float g_flMeleeRange; +extern DLL_GLOBAL CONSTANT float g_flMediumRange; +extern DLL_GLOBAL CONSTANT float g_flLongRange; +extern void EjectBrass (const Vector &vecOrigin, const Vector &vecVelocity, float rotation, int model, int soundtype ); +extern void ExplodeModel( const Vector &vecOrigin, float speed, int model, int count ); + +BOOL FBoxVisible ( entvars_t *pevLooker, entvars_t *pevTarget ); +BOOL FBoxVisible ( entvars_t *pevLooker, entvars_t *pevTarget, Vector &vecTargetOrigin, float flSize = 0.0 ); + +// monster to monster relationship types +#define R_AL -2 // (ALLY) pals. Good alternative to R_NO when applicable. +#define R_FR -1// (FEAR)will run +#define R_NO 0// (NO RELATIONSHIP) disregard +#define R_DL 1// (DISLIKE) will attack +#define R_HT 2// (HATE)will attack this character instead of any visible DISLIKEd characters +#define R_NM 3// (NEMESIS) A monster Will ALWAYS attack its nemsis, no matter what + + +// these bits represent the monster's memory +#define MEMORY_CLEAR 0 +#define bits_MEMORY_PROVOKED ( 1 << 0 )// right now only used for houndeyes. +#define bits_MEMORY_INCOVER ( 1 << 1 )// monster knows it is in a covered position. +#define bits_MEMORY_SUSPICIOUS ( 1 << 2 )// Ally is suspicious of the player, and will move to provoked more easily +#define bits_MEMORY_PATH_FINISHED ( 1 << 3 )// Finished monster path (just used by big momma for now) +#define bits_MEMORY_ON_PATH ( 1 << 4 )// Moving on a path +#define bits_MEMORY_MOVE_FAILED ( 1 << 5 )// Movement has already failed +#define bits_MEMORY_FLINCHED ( 1 << 6 )// Has already flinched +#define bits_MEMORY_KILLED ( 1 << 7 )// HACKHACK -- remember that I've already called my Killed() +#define bits_MEMORY_CUSTOM4 ( 1 << 28 ) // Monster-specific memory +#define bits_MEMORY_CUSTOM3 ( 1 << 29 ) // Monster-specific memory +#define bits_MEMORY_CUSTOM2 ( 1 << 30 ) // Monster-specific memory +#define bits_MEMORY_CUSTOM1 ( 1 << 31 ) // Monster-specific memory + +// trigger conditions for scripted AI +// these MUST match the CHOICES interface in halflife.fgd for the base monster +enum +{ + AITRIGGER_NONE = 0, + AITRIGGER_SEEPLAYER_ANGRY_AT_PLAYER, + AITRIGGER_TAKEDAMAGE, + AITRIGGER_HALFHEALTH, + AITRIGGER_DEATH, + AITRIGGER_SQUADMEMBERDIE, + AITRIGGER_SQUADLEADERDIE, + AITRIGGER_HEARWORLD, + AITRIGGER_HEARPLAYER, + AITRIGGER_HEARCOMBAT, + AITRIGGER_SEEPLAYER_UNCONDITIONAL, + AITRIGGER_SEEPLAYER_NOT_IN_COMBAT, +}; +/* + 0 : "No Trigger" + 1 : "See Player" + 2 : "Take Damage" + 3 : "50% Health Remaining" + 4 : "Death" + 5 : "Squad Member Dead" + 6 : "Squad Leader Dead" + 7 : "Hear World" + 8 : "Hear Player" + 9 : "Hear Combat" +*/ + +#define CUSTOM_SCHEDULES\ + virtual Schedule_t *ScheduleFromName( const char *pName );\ + static Schedule_t *m_scheduleList[]; + +#define DEFINE_CUSTOM_SCHEDULES(derivedClass)\ + Schedule_t *derivedClass::m_scheduleList[] = + +#define IMPLEMENT_CUSTOM_SCHEDULES(derivedClass, baseClass)\ + Schedule_t *derivedClass::ScheduleFromName( const char *pName )\ + {\ + Schedule_t *pSchedule = ScheduleInList( pName, m_scheduleList, ARRAYSIZE(m_scheduleList) );\ + if ( !pSchedule )\ + return baseClass::ScheduleFromName(pName);\ + return pSchedule;\ + } + + + +#endif //MONSTERS_H diff --git a/server/monsters/nodes.cpp b/server/monsters/nodes.cpp new file mode 100644 index 00000000..25c0cded --- /dev/null +++ b/server/monsters/nodes.cpp @@ -0,0 +1,3660 @@ +/*** +* +* Copyright (c) 1999, 2000 Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// nodes.cpp - AI node tree stuff. +//========================================================= + +#include +#include + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "nodes.h" +#include "animation.h" + +#define HULL_STEP_SIZE 16// how far the test hull moves on each step +#define NODE_HEIGHT 8 // how high to lift nodes off the ground after we drop them all (make stair/ramp mapping easier) + +// to help eliminate node clutter by level designers, this is used to cap how many other nodes +// any given node is allowed to 'see' in the first stage of graph creation "LinkVisibleNodes()". +#define MAX_NODE_INITIAL_LINKS 128 +#define MAX_NODES 1024 + +extern DLL_GLOBAL edict_t *g_pBodyQueueHead; + +Vector VecBModelOrigin( entvars_t* pevBModel ); + +CGraph WorldGraph; + +LINK_ENTITY_TO_CLASS( info_node, CNodeEnt ); +LINK_ENTITY_TO_CLASS( info_node_air, CNodeEnt ); +#ifdef __linux__ +#include +#define CreateDirectory(p, n) mkdir(p, 0777) +#endif +//========================================================= +// CGraph - InitGraph - prepares the graph for use. Frees any +// memory currently in use by the world graph, NULLs +// all pointers, and zeros the node count. +//========================================================= +void CGraph :: InitGraph( void) +{ + + // Make the graph unavailable + // + m_fGraphPresent = FALSE; + m_fGraphPointersSet = FALSE; + m_fRoutingComplete = FALSE; + + // Free the link pool + // + if ( m_pLinkPool ) + { + FREE ( m_pLinkPool ); + m_pLinkPool = NULL; + } + + // Free the node info + // + if ( m_pNodes ) + { + FREE ( m_pNodes ); + m_pNodes = NULL; + } + + if ( m_di ) + { + FREE ( m_di ); + m_di = NULL; + } + + // Free the routing info. + // + if ( m_pRouteInfo ) + { + FREE ( m_pRouteInfo ); + m_pRouteInfo = NULL; + } + + if (m_pHashLinks) + { + FREE( m_pHashLinks ); + m_pHashLinks = NULL; + } + + // Zero node and link counts + // + m_cNodes = 0; + m_cLinks = 0; + m_nRouteInfo = 0; + + m_iLastActiveIdleSearch = 0; + m_iLastCoverSearch = 0; +} + +//========================================================= +// CGraph - AllocNodes - temporary function that mallocs a +// reasonable number of nodes so we can build the path which +// will be saved to disk. +//========================================================= +int CGraph :: AllocNodes ( void ) +{ +// malloc all of the nodes + WorldGraph.m_pNodes = (CNode *)CALLOC ( sizeof ( CNode ), MAX_NODES ); + +// could not malloc space for all the nodes! + if ( !WorldGraph.m_pNodes ) + { + ALERT ( at_aiconsole, "**ERROR**\nCouldn't malloc %d nodes!\n", WorldGraph.m_cNodes ); + return FALSE; + } + + return TRUE; +} + +//========================================================= +// CGraph - LinkEntForLink - sometimes the ent that blocks +// a path is a usable door, in which case the monster just +// needs to face the door and fire it. In other cases, the +// monster needs to operate a button or lever to get the +// door to open. This function will return a pointer to the +// button if the monster needs to hit a button to open the +// door, or returns a pointer to the door if the monster +// need only use the door. +// +// pNode is the node the monster will be standing on when it +// will need to stop and trigger the ent. +//========================================================= +entvars_t* CGraph :: LinkEntForLink ( CLink *pLink, CNode *pNode ) +{ + CBaseEntity *pSearch; + CBaseEntity *pTrigger; + entvars_t *pevTrigger; + entvars_t *pevLinkEnt; + TraceResult tr; + + pevLinkEnt = pLink->m_pLinkEnt; + if ( !pevLinkEnt ) + return NULL; + + pSearch = NULL;// start search at the top of the ent list. + + if ( FClassnameIs ( pevLinkEnt, "func_door" ) || FClassnameIs ( pevLinkEnt, "func_door_rotating" ) ) + { + + ///!!!UNDONE - check for TOGGLE or STAY open doors here. If a door is in the way, and is + // TOGGLE or STAY OPEN, even monsters that can't open doors can go that way. + + if ( ( pevLinkEnt->spawnflags & SF_DOOR_USE_ONLY ) ) + {// door is use only, so the door is all the monster has to worry about + return pevLinkEnt; + } + + while ( 1 ) + { + pTrigger = UTIL_FindEntityByTarget ( pSearch, STRING( pevLinkEnt->targetname ) );// find the button or trigger + + if ( !pTrigger ) + {// no trigger found + + // right now this is a problem among auto-open doors, or any door that opens through the use + // of a trigger brush. Trigger brushes have no models, and don't show up in searches. Just allow + // monsters to open these sorts of doors for now. + return pevLinkEnt; + } + + pSearch = pTrigger; + pevTrigger = pTrigger->pev; + + if ( FClassnameIs(pevTrigger, "func_button") || FClassnameIs(pevTrigger, "func_rot_button" ) ) + {// only buttons are handled right now. + + // trace from the node to the trigger, make sure it's one we can see from the node. + // !!!HACKHACK Use bodyqueue here cause there are no ents we really wish to ignore! + UTIL_TraceLine ( pNode->m_vecOrigin, VecBModelOrigin( pevTrigger ), ignore_monsters, g_pBodyQueueHead, &tr ); + + + if ( VARS(tr.pHit) == pevTrigger ) + {// good to go! + return VARS( tr.pHit ); + } + } + } + } + else + { + ALERT ( at_aiconsole, "Unsupported PathEnt:\n'%s'\n", STRING ( pevLinkEnt->classname ) ); + return NULL; + } +} + +//========================================================= +// CGraph - HandleLinkEnt - a brush ent is between two +// nodes that would otherwise be able to see each other. +// Given the monster's capability, determine whether +// or not the monster can go this way. +//========================================================= +int CGraph :: HandleLinkEnt ( int iNode, entvars_t *pevLinkEnt, int afCapMask, NODEQUERY queryType ) +{ + edict_t *pentWorld; + CBaseEntity *pDoor; + TraceResult tr; + + if ( !m_fGraphPresent || !m_fGraphPointersSet ) + {// protect us in the case that the node graph isn't available + ALERT ( at_aiconsole, "Graph not ready!\n" ); + return FALSE; + } + + if ( FNullEnt ( pevLinkEnt ) ) + { + ALERT ( at_aiconsole, "dead path ent!\n" ); + return TRUE; + } + pentWorld = NULL; + +// func_door + if ( FClassnameIs( pevLinkEnt, "func_door" ) || FClassnameIs( pevLinkEnt, "func_door_rotating" ) ) + {// ent is a door. + + pDoor = ( CBaseEntity::Instance( pevLinkEnt ) ); + + if ( ( pevLinkEnt->spawnflags & SF_DOOR_USE_ONLY ) ) + {// door is use only. + + if ( ( afCapMask & bits_CAP_OPEN_DOORS ) ) + {// let monster right through if he can open doors + return TRUE; + } + else + { + // monster should try for it if the door is open and looks as if it will stay that way + if ( pDoor->GetState() == STATE_ON && ( pevLinkEnt->spawnflags & SF_DOOR_NO_AUTO_RETURN )) + { + return TRUE; + } + + return FALSE; + } + } + else + {// door must be opened with a button or trigger field. + + // monster should try for it if the door is open and looks as if it will stay that way + if ( pDoor->GetState() == STATE_ON && ( pevLinkEnt->spawnflags & SF_DOOR_NO_AUTO_RETURN )) + { + return TRUE; + } + if ( ( afCapMask & bits_CAP_OPEN_DOORS ) ) + { + if ( !( pevLinkEnt->spawnflags & SF_DOOR_NOMONSTERS ) || queryType == NODEGRAPH_STATIC ) + return TRUE; + } + + return FALSE; + } + } +// func_breakable + else if ( FClassnameIs( pevLinkEnt, "func_breakable" ) && queryType == NODEGRAPH_STATIC ) + { + return TRUE; + } + else + { + ALERT ( at_aiconsole, "Unhandled Ent in Path %s\n", STRING( pevLinkEnt->classname ) ); + return FALSE; + } + + return FALSE; +} + +#if 0 +//========================================================= +// FindNearestLink - finds the connection (line) nearest +// the given point. Returns FALSE if fails, or TRUE if it +// has stuffed the index into the nearest link pool connection +// into the passed int pointer, and a BOOL telling whether or +// not the point is along the line into the passed BOOL pointer. +//========================================================= +int CGraph :: FindNearestLink ( const Vector &vecTestPoint, int *piNearestLink, BOOL *pfAlongLine ) +{ + int i, j;// loops + + int iNearestLink;// index into the link pool, this is the nearest node at any time. + float flMinDist;// the distance of of the nearest case so far + float flDistToLine;// the distance of the current test case + + BOOL fCurrentAlongLine; + BOOL fSuccess; + + //float flConstant;// line constant + Vector vecSpot1, vecSpot2; + Vector2D vec2Spot1, vec2Spot2, vec2TestPoint; + Vector2D vec2Normal;// line normal + Vector2D vec2Line; + + TraceResult tr; + + iNearestLink = -1;// prepare for failure + fSuccess = FALSE; + + flMinDist = 9999;// anything will be closer than this + +// go through all of the nodes, and each node's connections + int cSkip = 0;// how many links proper pairing allowed us to skip + int cChecked = 0;// how many links were checked + + for ( i = 0 ; i < m_cNodes ; i++ ) + { + vecSpot1 = m_pNodes[ i ].m_vecOrigin; + + if ( m_pNodes[ i ].m_cNumLinks <= 0 ) + {// this shouldn't happen! + ALERT ( at_aiconsole, "**Node %d has no links\n", i ); + continue; + } + + for ( j = 0 ; j < m_pNodes[ i ].m_cNumLinks ; j++ ) + { + /* + !!!This optimization only works when the node graph consists of properly linked pairs. + if ( INodeLink ( i, j ) <= i ) + { + // since we're going through the nodes in order, don't check + // any connections whose second node is lower in the list + // than the node we're currently working with. This eliminates + // redundant checks. + cSkip++; + continue; + } + */ + + vecSpot2 = PNodeLink ( i, j )->m_vecOrigin; + + // these values need a little attention now and then, or sometimes ramps cause trouble. + if ( fabs ( vecSpot1.z - vecTestPoint.z ) > 48 && fabs ( vecSpot2.z - vecTestPoint.z ) > 48 ) + { + // if both endpoints of the line are 32 units or more above or below the monster, + // the monster won't be able to get to them, so we do a bit of trivial rejection here. + // this may change if monsters are allowed to jump down. + // + // !!!LATER: some kind of clever X/Y hashing should be used here, too + continue; + } + +// now we have two endpoints for a line segment that we've not already checked. +// since all lines that make it this far are within -/+ 32 units of the test point's +// Z Plane, we can get away with doing the point->line check in 2d. + + cChecked++; + + vec2Spot1 = vecSpot1.Make2D(); + vec2Spot2 = vecSpot2.Make2D(); + vec2TestPoint = vecTestPoint.Make2D(); + + // get the line normal. + vec2Line = ( vec2Spot1 - vec2Spot2 ).Normalize(); + vec2Normal.x = -vec2Line.y; + vec2Normal.y = vec2Line.x; + + if ( DotProduct ( vec2Line, ( vec2TestPoint - vec2Spot1 ) ) > 0 ) + {// point outside of line + flDistToLine = ( vec2TestPoint - vec2Spot1 ).Length(); + fCurrentAlongLine = FALSE; + } + else if ( DotProduct ( vec2Line, ( vec2TestPoint - vec2Spot2 ) ) < 0 ) + {// point outside of line + flDistToLine = ( vec2TestPoint - vec2Spot2 ).Length(); + fCurrentAlongLine = FALSE; + } + else + {// point inside line + flDistToLine = fabs( DotProduct ( vec2TestPoint - vec2Spot2, vec2Normal ) ); + fCurrentAlongLine = TRUE; + } + + if ( flDistToLine < flMinDist ) + {// just found a line nearer than any other so far + + UTIL_TraceLine ( vecTestPoint, SourceNode( i, j ).m_vecOrigin, ignore_monsters, g_pBodyQueueHead, &tr ); + + if ( tr.flFraction != 1.0 ) + {// crap. can't see the first node of this link, try to see the other + + UTIL_TraceLine ( vecTestPoint, DestNode( i, j ).m_vecOrigin, ignore_monsters, g_pBodyQueueHead, &tr ); + if ( tr.flFraction != 1.0 ) + {// can't use this link, cause can't see either node! + continue; + } + + } + + fSuccess = TRUE;// we know there will be something to return. + flMinDist = flDistToLine; + iNearestLink = m_pNodes [ i ].m_iFirstLink + j; + *piNearestLink = m_pNodes[ i ].m_iFirstLink + j; + *pfAlongLine = fCurrentAlongLine; + } + } + } + +/* + if ( fSuccess ) + { + WRITE_BYTE(MSG_BROADCAST, SVC_TEMPENTITY); + WRITE_BYTE(MSG_BROADCAST, TE_SHOWLINE); + + WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iSrcNode ].m_vecOrigin.x ); + WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iSrcNode ].m_vecOrigin.y ); + WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iSrcNode ].m_vecOrigin.z + NODE_HEIGHT); + + WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iDestNode ].m_vecOrigin.x ); + WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iDestNode ].m_vecOrigin.y ); + WRITE_COORD(MSG_BROADCAST, m_pNodes[ m_pLinkPool[ iNearestLink ].m_iDestNode ].m_vecOrigin.z + NODE_HEIGHT); + } +*/ + + ALERT ( at_aiconsole, "%d Checked\n", cChecked ); + return fSuccess; +} + +#endif + +int CGraph::HullIndex( const CBaseEntity *pEntity ) +{ + if ( pEntity->pev->movetype == MOVETYPE_FLY) + return NODE_FLY_HULL; + + if ( pEntity->pev->mins == Vector( -12, -12, 0 ) ) + return NODE_SMALL_HULL; + else if ( pEntity->pev->mins == VEC_HUMAN_HULL_MIN ) + return NODE_HUMAN_HULL; + else if ( pEntity->pev->mins == Vector ( -32, -32, 0 ) ) + return NODE_LARGE_HULL; + +// ALERT ( at_aiconsole, "Unknown Hull Mins!\n" ); + return NODE_HUMAN_HULL; +} + + +int CGraph::NodeType( const CBaseEntity *pEntity ) +{ + if ( pEntity->pev->movetype == MOVETYPE_FLY) + { + if( pEntity->pev->waterlevel != 0 && pEntity->pev->watertype != CONTENTS_FOG ) + { + return bits_NODE_WATER; + } + else + { + return bits_NODE_AIR; + } + } + return bits_NODE_LAND; +} + + +// Sum up graph weights on the path from iStart to iDest to determine path length +float CGraph::PathLength( int iStart, int iDest, int iHull, int afCapMask ) +{ + float distance = 0; + int iNext; + + int iMaxLoop = m_cNodes; + + int iCurrentNode = iStart; + int iCap = CapIndex( afCapMask ); + + while (iCurrentNode != iDest) + { + if (iMaxLoop-- <= 0) + { + ALERT( at_debug, "Route Failure\n" ); + return 0; + } + + iNext = NextNodeInRoute( iCurrentNode, iDest, iHull, iCap ); + if (iCurrentNode == iNext) + { + //ALERT(at_aiconsole, "SVD: Can't get there from here..\n"); + return 0; + } + + int iLink; + HashSearch(iCurrentNode, iNext, iLink); + if (iLink < 0) + { + ALERT(at_debug, "HashLinks is broken from %d to %d.\n", iCurrentNode, iDest); + return 0; + } + CLink &link = Link(iLink); + distance += link.m_flWeight; + + iCurrentNode = iNext; + } + + return distance; +} + + +// Parse the routing table at iCurrentNode for the next node on the shortest path to iDest +int CGraph::NextNodeInRoute( int iCurrentNode, int iDest, int iHull, int iCap ) +{ + int iNext = iCurrentNode; + int nCount = iDest+1; + char *pRoute = m_pRouteInfo + m_pNodes[ iCurrentNode ].m_pNextBestNode[iHull][iCap]; + + // Until we decode the next best node + // + while (nCount > 0) + { + char ch = *pRoute++; + //ALERT(at_aiconsole, "C(%d)", ch); + if (ch < 0) + { + // Sequence phrase + // + ch = -ch; + if (nCount <= ch) + { + iNext = iDest; + nCount = 0; + //ALERT(at_aiconsole, "SEQ: iNext/iDest=%d\n", iNext); + } + else + { + //ALERT(at_aiconsole, "SEQ: nCount + ch (%d + %d)\n", nCount, ch); + nCount = nCount - ch; + } + } + else + { + //ALERT(at_aiconsole, "C(%d)", *pRoute); + + // Repeat phrase + // + if (nCount <= ch+1) + { + iNext = iCurrentNode + *pRoute; + if (iNext >= m_cNodes) iNext -= m_cNodes; + else if (iNext < 0) iNext += m_cNodes; + nCount = 0; + //ALERT(at_aiconsole, "REP: iNext=%d\n", iNext); + } + else + { + //ALERT(at_aiconsole, "REP: nCount - ch+1 (%d - %d+1)\n", nCount, ch); + nCount = nCount - ch - 1; + } + pRoute++; + } + } + + return iNext; +} + + +//========================================================= +// CGraph - FindShortestPath +// +// accepts a capability mask (afCapMask), and will only +// find a path usable by a monster with those capabilities +// returns the number of nodes copied into supplied array +//========================================================= +int CGraph :: FindShortestPath ( int *piPath, int iStart, int iDest, int iHull, int afCapMask) +{ + int iVisitNode; + int iCurrentNode; + int iNumPathNodes; + int iHullMask; + + if ( !m_fGraphPresent || !m_fGraphPointersSet ) + {// protect us in the case that the node graph isn't available or built + ALERT ( at_aiconsole, "Graph not ready!\n" ); + return FALSE; + } + + if ( iStart < 0 || iStart > m_cNodes ) + {// The start node is bad? + ALERT ( at_aiconsole, "Can't build a path, iStart is %d!\n", iStart ); + return FALSE; + } + + if (iStart == iDest) + { + piPath[0] = iStart; + piPath[1] = iDest; + return 2; + } + + // Is routing information present. + // + if (m_fRoutingComplete) + { + int iCap = CapIndex( afCapMask ); + + iNumPathNodes = 0; + piPath[iNumPathNodes++] = iStart; + iCurrentNode = iStart; + int iNext; + + //ALERT(at_aiconsole, "GOAL: %d to %d\n", iStart, iDest); + + // Until we arrive at the destination + // + while (iCurrentNode != iDest) + { + iNext = NextNodeInRoute( iCurrentNode, iDest, iHull, iCap ); + if (iCurrentNode == iNext) + { + //ALERT(at_aiconsole, "SVD: Can't get there from here..\n"); + return 0; + break; + } + if (iNumPathNodes >= MAX_PATH_SIZE) + { + //ALERT(at_aiconsole, "SVD: Don't return the entire path.\n"); + break; + } + piPath[iNumPathNodes++] = iNext; + iCurrentNode = iNext; + } + //ALERT( at_aiconsole, "SVD: Path with %d nodes.\n", iNumPathNodes); + } + else + { + CQueuePriority queue; + + switch( iHull ) + { + case NODE_SMALL_HULL: + iHullMask = bits_LINK_SMALL_HULL; + break; + case NODE_HUMAN_HULL: + iHullMask = bits_LINK_HUMAN_HULL; + break; + case NODE_LARGE_HULL: + iHullMask = bits_LINK_LARGE_HULL; + break; + case NODE_FLY_HULL: + iHullMask = bits_LINK_FLY_HULL; + break; + } + + // Mark all the nodes as unvisited. + // + int i = 0; + for ( i = 0; i < m_cNodes; i++) + { + m_pNodes[ i ].m_flClosestSoFar = -1.0; + } + + m_pNodes[ iStart ].m_flClosestSoFar = 0.0; + m_pNodes[ iStart ].m_iPreviousNode = iStart;// tag this as the origin node + queue.Insert( iStart, 0.0 );// insert start node + + while ( !queue.Empty() ) + { + // now pull a node out of the queue + float flCurrentDistance; + iCurrentNode = queue.Remove(flCurrentDistance); + + // For straight-line weights, the following Shortcut works. For arbitrary weights, + // it doesn't. + // + if (iCurrentNode == iDest) break; + + CNode *pCurrentNode = &m_pNodes[ iCurrentNode ]; + + for ( i = 0 ; i < pCurrentNode->m_cNumLinks ; i++ ) + {// run through all of this node's neighbors + + iVisitNode = INodeLink ( iCurrentNode, i ); + if ( ( m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_afLinkInfo & iHullMask ) != iHullMask ) + {// monster is too large to walk this connection + //ALERT ( at_aiconsole, "fat ass %d/%d\n",m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_afLinkInfo, iMonsterHull ); + continue; + } + // check the connection from the current node to the node we're about to mark visited and push into the queue + if ( m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_pLinkEnt != NULL ) + {// there's a brush ent in the way! Don't mark this node or put it into the queue unless the monster can negotiate it + + if ( !HandleLinkEnt ( iCurrentNode, m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i ].m_pLinkEnt, afCapMask, NODEGRAPH_STATIC ) ) + {// monster should not try to go this way. + continue; + } + } + float flOurDistance = flCurrentDistance + m_pLinkPool[ m_pNodes[ iCurrentNode ].m_iFirstLink + i].m_flWeight; + if ( m_pNodes[ iVisitNode ].m_flClosestSoFar < -0.5 + || flOurDistance < m_pNodes[ iVisitNode ].m_flClosestSoFar - 0.001 ) + { + m_pNodes[iVisitNode].m_flClosestSoFar = flOurDistance; + m_pNodes[iVisitNode].m_iPreviousNode = iCurrentNode; + + queue.Insert ( iVisitNode, flOurDistance ); + } + } + } + if ( m_pNodes[iDest].m_flClosestSoFar < -0.5 ) + {// Destination is unreachable, no path found. + return 0; + } + + // the queue is not empty + + // now we must walk backwards through the m_iPreviousNode field, and count how many connections there are in the path + iCurrentNode = iDest; + iNumPathNodes = 1;// count the dest + + while ( iCurrentNode != iStart ) + { + iNumPathNodes++; + iCurrentNode = m_pNodes[ iCurrentNode ].m_iPreviousNode; + } + + iCurrentNode = iDest; + for ( i = iNumPathNodes - 1 ; i >= 0 ; i-- ) + { + piPath[ i ] = iCurrentNode; + iCurrentNode = m_pNodes [ iCurrentNode ].m_iPreviousNode; + } + } + +#if 0 + + if (m_fRoutingComplete) + { + // This will draw the entire path that was generated for the monster. + + for ( int i = 0 ; i < iNumPathNodes - 1 ; i++ ) + { + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE); + + WRITE_COORD( m_pNodes[ piPath[ i ] ].m_vecOrigin.x ); + WRITE_COORD( m_pNodes[ piPath[ i ] ].m_vecOrigin.y ); + WRITE_COORD( m_pNodes[ piPath[ i ] ].m_vecOrigin.z + NODE_HEIGHT ); + + WRITE_COORD( m_pNodes[ piPath[ i + 1 ] ].m_vecOrigin.x ); + WRITE_COORD( m_pNodes[ piPath[ i + 1 ] ].m_vecOrigin.y ); + WRITE_COORD( m_pNodes[ piPath[ i + 1 ] ].m_vecOrigin.z + NODE_HEIGHT ); + MESSAGE_END(); + } + } + +#endif +#if 0 // MAZE map + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE); + + WRITE_COORD( m_pNodes[ 4 ].m_vecOrigin.x ); + WRITE_COORD( m_pNodes[ 4 ].m_vecOrigin.y ); + WRITE_COORD( m_pNodes[ 4 ].m_vecOrigin.z + NODE_HEIGHT ); + + WRITE_COORD( m_pNodes[ 9 ].m_vecOrigin.x ); + WRITE_COORD( m_pNodes[ 9 ].m_vecOrigin.y ); + WRITE_COORD( m_pNodes[ 9 ].m_vecOrigin.z + NODE_HEIGHT ); + MESSAGE_END(); +#endif + + return iNumPathNodes; +} + +inline USHORT Hash( void *p, int len ) +{ + word usCrc; + + CRC_INIT( &usCrc ); + CRC_PROCESS_BUFFER( &usCrc, p, len ); + + return CRC_FINAL( usCrc ); +} + +void inline CalcBounds(int &Lower, int &Upper, int Goal, int Best) +{ + int Temp = 2*Goal - Best; + if (Best > Goal) + { + Lower = max(0, Temp); + Upper = Best; + } + else + { + Upper = min(255, Temp); + Lower = Best; + } +} + +// Convert from [-8192,8192] to [0, 255] +// +inline int CALC_RANGE(int x, int lower, int upper) +{ + return NUM_RANGES*(x-lower)/((upper-lower+1)); +} + + +void inline UpdateRange(int &minValue, int &maxValue, int Goal, int Best) +{ + int Lower, Upper; + CalcBounds(Lower, Upper, Goal, Best); + if (Upper < maxValue) maxValue = Upper; + if (minValue < Lower) minValue = Lower; +} + +void CGraph :: CheckNode(Vector vecOrigin, int iNode) +{ + // Have we already seen this point before?. + // + if (m_di[iNode].m_CheckedEvent == m_CheckedCounter) return; + m_di[iNode].m_CheckedEvent = m_CheckedCounter; + + float flDist = ( vecOrigin - m_pNodes[ iNode ].m_vecOriginPeek ).Length(); + + if ( flDist < m_flShortest ) + { + TraceResult tr; + + // make sure that vecOrigin can trace to this node! + UTIL_TraceLine ( vecOrigin, m_pNodes[ iNode ].m_vecOriginPeek, ignore_monsters, 0, &tr ); + + if ( tr.flFraction == 1.0 ) + { + m_iNearest = iNode; + m_flShortest = flDist; + + UpdateRange(m_minX, m_maxX, CALC_RANGE(vecOrigin.x, m_RegionMin[0], m_RegionMax[0]), m_pNodes[iNode].m_Region[0]); + UpdateRange(m_minY, m_maxY, CALC_RANGE(vecOrigin.y, m_RegionMin[1], m_RegionMax[1]), m_pNodes[iNode].m_Region[1]); + UpdateRange(m_minZ, m_maxZ, CALC_RANGE(vecOrigin.z, m_RegionMin[2], m_RegionMax[2]), m_pNodes[iNode].m_Region[2]); + + // From maxCircle, calculate maximum bounds box. All points must be + // simultaneously inside all bounds of the box. + // + m_minBoxX = CALC_RANGE(vecOrigin.x - flDist, m_RegionMin[0], m_RegionMax[0]); + m_maxBoxX = CALC_RANGE(vecOrigin.x + flDist, m_RegionMin[0], m_RegionMax[0]); + m_minBoxY = CALC_RANGE(vecOrigin.y - flDist, m_RegionMin[1], m_RegionMax[1]); + m_maxBoxY = CALC_RANGE(vecOrigin.y + flDist, m_RegionMin[1], m_RegionMax[1]); + m_minBoxZ = CALC_RANGE(vecOrigin.z - flDist, m_RegionMin[2], m_RegionMax[2]); + m_maxBoxZ = CALC_RANGE(vecOrigin.z + flDist, m_RegionMin[2], m_RegionMax[2]); + } + } +} + +//========================================================= +// CGraph - FindNearestNode - returns the index of the node nearest +// the given vector -1 is failure (couldn't find a valid +// near node ) +//========================================================= +int CGraph :: FindNearestNode ( const Vector &vecOrigin, CBaseEntity *pEntity ) +{ + return FindNearestNode( vecOrigin, NodeType( pEntity ) ); +} + +int CGraph :: FindNearestNode ( const Vector &vecOrigin, int afNodeTypes ) +{ + int i; + TraceResult tr; + + if ( !m_fGraphPresent || !m_fGraphPointersSet ) + {// protect us in the case that the node graph isn't available + ALERT ( at_aiconsole, "Graph not ready!\n" ); + return -1; + } + + // Check with the cache + // + ULONG iHash = (CACHE_SIZE-1) & Hash((void *)(const float *)vecOrigin, sizeof(vecOrigin)); + if (m_Cache[iHash].v == vecOrigin) + { + //ALERT(at_aiconsole, "Cache Hit.\n"); + return m_Cache[iHash].n; + } + else + { + //ALERT(at_aiconsole, "Cache Miss.\n"); + } + + // Mark all points as unchecked. + // + m_CheckedCounter++; + if (m_CheckedCounter == 0) + { + for (int i = 0; i < m_cNodes; i++) + { + m_di[i].m_CheckedEvent = 0; + } + m_CheckedCounter++; + } + + m_iNearest = -1; + m_flShortest = 999999.0; // just a big number. + + // If we can find a visible point, then let CalcBounds set the limits, but if + // we have no visible point at all to start with, then don't restrict the limits. + // +#if 1 + m_minX = 0; m_maxX = 255; + m_minY = 0; m_maxY = 255; + m_minZ = 0; m_maxZ = 255; + m_minBoxX = 0; m_maxBoxX = 255; + m_minBoxY = 0; m_maxBoxY = 255; + m_minBoxZ = 0; m_maxBoxZ = 255; +#else + m_minBoxX = CALC_RANGE(vecOrigin.x - flDist, m_RegionMin[0], m_RegionMax[0]); + m_maxBoxX = CALC_RANGE(vecOrigin.x + flDist, m_RegionMin[0], m_RegionMax[0]); + m_minBoxY = CALC_RANGE(vecOrigin.y - flDist, m_RegionMin[1], m_RegionMax[1]); + m_maxBoxY = CALC_RANGE(vecOrigin.y + flDist, m_RegionMin[1], m_RegionMax[1]); + m_minBoxZ = CALC_RANGE(vecOrigin.z - flDist, m_RegionMin[2], m_RegionMax[2]); + m_maxBoxZ = CALC_RANGE(vecOrigin.z + flDist, m_RegionMin[2], m_RegionMax[2]) + CalcBounds(m_minX, m_maxX, CALC_RANGE(vecOrigin.x, m_RegionMin[0], m_RegionMax[0]), m_pNodes[m_iNearest].m_Region[0]); + CalcBounds(m_minY, m_maxY, CALC_RANGE(vecOrigin.y, m_RegionMin[1], m_RegionMax[1]), m_pNodes[m_iNearest].m_Region[1]); + CalcBounds(m_minZ, m_maxZ, CALC_RANGE(vecOrigin.z, m_RegionMin[2], m_RegionMax[2]), m_pNodes[m_iNearest].m_Region[2]); +#endif + + int halfX = (m_minX+m_maxX)/2; + int halfY = (m_minY+m_maxY)/2; + int halfZ = (m_minZ+m_maxZ)/2; + + int j; + + for (i = halfX; i >= m_minX; i--) + { + for (j = m_RangeStart[0][i]; j <= m_RangeEnd[0][i]; j++) + { + if (!(m_pNodes[m_di[j].m_SortedBy[0]].m_afNodeInfo & afNodeTypes)) continue; + + int rgY = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[1]; + if (rgY > m_maxBoxY) break; + if (rgY < m_minBoxY) continue; + + int rgZ = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[2]; + if (rgZ < m_minBoxZ) continue; + if (rgZ > m_maxBoxZ) continue; + CheckNode(vecOrigin, m_di[j].m_SortedBy[0]); + } + } + + for (i = max(m_minY,halfY+1); i <= m_maxY; i++) + { + for (j = m_RangeStart[1][i]; j <= m_RangeEnd[1][i]; j++) + { + if (!(m_pNodes[m_di[j].m_SortedBy[1]].m_afNodeInfo & afNodeTypes)) continue; + + int rgZ = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[2]; + if (rgZ > m_maxBoxZ) break; + if (rgZ < m_minBoxZ) continue; + int rgX = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[0]; + if (rgX < m_minBoxX) continue; + if (rgX > m_maxBoxX) continue; + CheckNode(vecOrigin, m_di[j].m_SortedBy[1]); + } + } + + for (i = min(m_maxZ,halfZ); i >= m_minZ; i--) + { + for (j = m_RangeStart[2][i]; j <= m_RangeEnd[2][i]; j++) + { + if (!(m_pNodes[m_di[j].m_SortedBy[2]].m_afNodeInfo & afNodeTypes)) continue; + + int rgX = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[0]; + if (rgX > m_maxBoxX) break; + if (rgX < m_minBoxX) continue; + int rgY = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[1]; + if (rgY < m_minBoxY) continue; + if (rgY > m_maxBoxY) continue; + CheckNode(vecOrigin, m_di[j].m_SortedBy[2]); + } + } + + for (i = max(m_minX,halfX+1); i <= m_maxX; i++) + { + for (j = m_RangeStart[0][i]; j <= m_RangeEnd[0][i]; j++) + { + if (!(m_pNodes[m_di[j].m_SortedBy[0]].m_afNodeInfo & afNodeTypes)) continue; + + int rgY = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[1]; + if (rgY > m_maxBoxY) break; + if (rgY < m_minBoxY) continue; + + int rgZ = m_pNodes[m_di[j].m_SortedBy[0]].m_Region[2]; + if (rgZ < m_minBoxZ) continue; + if (rgZ > m_maxBoxZ) continue; + CheckNode(vecOrigin, m_di[j].m_SortedBy[0]); + } + } + + for (i = min(m_maxY,halfY); i >= m_minY; i--) + { + for (j = m_RangeStart[1][i]; j <= m_RangeEnd[1][i]; j++) + { + if (!(m_pNodes[m_di[j].m_SortedBy[1]].m_afNodeInfo & afNodeTypes)) continue; + + int rgZ = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[2]; + if (rgZ > m_maxBoxZ) break; + if (rgZ < m_minBoxZ) continue; + int rgX = m_pNodes[m_di[j].m_SortedBy[1]].m_Region[0]; + if (rgX < m_minBoxX) continue; + if (rgX > m_maxBoxX) continue; + CheckNode(vecOrigin, m_di[j].m_SortedBy[1]); + } + } + + for (i = max(m_minZ,halfZ+1); i <= m_maxZ; i++) + { + for (j = m_RangeStart[2][i]; j <= m_RangeEnd[2][i]; j++) + { + if (!(m_pNodes[m_di[j].m_SortedBy[2]].m_afNodeInfo & afNodeTypes)) continue; + + int rgX = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[0]; + if (rgX > m_maxBoxX) break; + if (rgX < m_minBoxX) continue; + int rgY = m_pNodes[m_di[j].m_SortedBy[2]].m_Region[1]; + if (rgY < m_minBoxY) continue; + if (rgY > m_maxBoxY) continue; + CheckNode(vecOrigin, m_di[j].m_SortedBy[2]); + } + } + +#if 0 + // Verify our answers. + // + int iNearestCheck = -1; + m_flShortest = 8192;// find nodes within this radius + + for ( i = 0 ; i < m_cNodes ; i++ ) + { + float flDist = ( vecOrigin - m_pNodes[ i ].m_vecOriginPeek ).Length(); + + if ( flDist < m_flShortest ) + { + // make sure that vecOrigin can trace to this node! + UTIL_TraceLine ( vecOrigin, m_pNodes[ i ].m_vecOriginPeek, ignore_monsters, 0, &tr ); + + if ( tr.flFraction == 1.0 ) + { + iNearestCheck = i; + m_flShortest = flDist; + } + } + } + + if (iNearestCheck != m_iNearest) + { + ALERT( at_aiconsole, "NOT closest %d(%f,%f,%f) %d(%f,%f,%f).\n", + iNearestCheck, + m_pNodes[iNearestCheck].m_vecOriginPeek.x, + m_pNodes[iNearestCheck].m_vecOriginPeek.y, + m_pNodes[iNearestCheck].m_vecOriginPeek.z, + m_iNearest, + (m_iNearest == -1?0.0:m_pNodes[m_iNearest].m_vecOriginPeek.x), + (m_iNearest == -1?0.0:m_pNodes[m_iNearest].m_vecOriginPeek.y), + (m_iNearest == -1?0.0:m_pNodes[m_iNearest].m_vecOriginPeek.z)); + } + if (m_iNearest == -1) + { + ALERT(at_aiconsole, "All that work for nothing.\n"); + } +#endif + m_Cache[iHash].v = vecOrigin; + m_Cache[iHash].n = m_iNearest; + return m_iNearest; +} + +//========================================================= +// CGraph - ShowNodeConnections - draws a line from the given node +// to all connected nodes +//========================================================= +void CGraph :: ShowNodeConnections ( int iNode ) +{ + Vector vecSpot; + CNode *pNode; + CNode *pLinkNode; + int i; + + if ( !m_fGraphPresent || !m_fGraphPointersSet ) + {// protect us in the case that the node graph isn't available or built + ALERT ( at_aiconsole, "Graph not ready!\n" ); + return; + } + + if ( iNode < 0 ) + { + ALERT( at_aiconsole, "Can't show connections for node %d\n", iNode ); + return; + } + + pNode = &m_pNodes[ iNode ]; + + UTIL_ParticleEffect( pNode->m_vecOrigin, g_vecZero, 255, 20 );// show node position + + if ( pNode->m_cNumLinks <= 0 ) + {// no connections! + ALERT ( at_aiconsole, "**No Connections!\n" ); + } + + for ( i = 0 ; i < pNode->m_cNumLinks ; i++ ) + { + + pLinkNode = &Node( NodeLink( iNode, i).m_iDestNode ); + vecSpot = pLinkNode->m_vecOrigin; + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE); + + WRITE_COORD( m_pNodes[ iNode ].m_vecOrigin.x ); + WRITE_COORD( m_pNodes[ iNode ].m_vecOrigin.y ); + WRITE_COORD( m_pNodes[ iNode ].m_vecOrigin.z + NODE_HEIGHT ); + + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + NODE_HEIGHT ); + MESSAGE_END(); + + } +} + +//========================================================= +// CGraph - LinkVisibleNodes - the first, most basic +// function of node graph creation, this connects every +// node to every other node that it can see. Expects a +// pointer to an empty connection pool and a file pointer +// to write progress to. Returns the total number of initial +// links. +// +// If there's a problem with this process, the index +// of the offending node will be written to piBadNode +//========================================================= +int CGraph :: LinkVisibleNodes ( CLink *pLinkPool, FILE *file, int *piBadNode ) +{ + int i,j,z; + edict_t *pTraceEnt; + int cTotalLinks, cLinksThisNode, cMaxInitialLinks; + TraceResult tr; + + // !!!BUGBUG - this function returns 0 if there is a problem in the middle of connecting the graph + // it also returns 0 if none of the nodes in a level can see each other. piBadNode is ALWAYS read + // by BuildNodeGraph() if this function returns a 0, so make sure that it doesn't get some random + // number back. + *piBadNode = 0; + + + if ( m_cNodes <= 0 ) + { + ALERT ( at_aiconsole, "No Nodes!\n" ); + return FALSE; + } + + // if the file pointer is bad, don't blow up, just don't write the + // file. + if ( !file ) + { + ALERT ( at_aiconsole, "**LinkVisibleNodes:\ncan't write to file." ); + } + else + { + fprintf ( file, "----------------------------------------------------------------------------\n" ); + fprintf ( file, "LinkVisibleNodes - Initial Connections\n" ); + fprintf ( file, "----------------------------------------------------------------------------\n" ); + } + + cTotalLinks = 0;// start with no connections + + // to keep track of the maximum number of initial links any node had so far. + // this lets us keep an eye on MAX_NODE_INITIAL_LINKS to ensure that we are + // being generous enough. + cMaxInitialLinks = 0; + + for ( i = 0 ; i < m_cNodes ; i++ ) + { + cLinksThisNode = 0;// reset this count for each node. + + if ( file ) + { + fprintf ( file, "Node #%4d:\n\n", i ); + } + + for ( z = 0 ; z < MAX_NODE_INITIAL_LINKS ; z++ ) + {// clear out the important fields in the link pool for this node + pLinkPool [ cTotalLinks + z ].m_iSrcNode = i;// so each link knows which node it originates from + pLinkPool [ cTotalLinks + z ].m_iDestNode = 0; + pLinkPool [ cTotalLinks + z ].m_pLinkEnt = NULL; + } + + m_pNodes [ i ].m_iFirstLink = cTotalLinks; + + // now build a list of every other node that this node can see + for ( j = 0 ; j < m_cNodes ; j++ ) + { + if ( j == i ) + {// don't connect to self! + continue; + } + +#if 0 + + if ( (m_pNodes[ i ].m_afNodeInfo & bits_NODE_WATER) != (m_pNodes[ j ].m_afNodeInfo & bits_NODE_WATER) ) + { + // don't connect water nodes to air nodes or land nodes. It just wouldn't be prudent at this juncture. + continue; + } +#else + if ( (m_pNodes[ i ].m_afNodeInfo & bits_NODE_GROUP_REALM) != (m_pNodes[ j ].m_afNodeInfo & bits_NODE_GROUP_REALM) ) + { + // don't connect air nodes to water nodes to land nodes. It just wouldn't be prudent at this juncture. + continue; + } +#endif + + tr.pHit = NULL;// clear every time so we don't get stuck with last trace's hit ent + pTraceEnt = 0; + + UTIL_TraceLine ( m_pNodes[ i ].m_vecOrigin, + m_pNodes[ j ].m_vecOrigin, + ignore_monsters, + g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about + &tr ); + + + if ( tr.fStartSolid ) + continue; + + if ( tr.flFraction != 1.0 ) + {// trace hit a brush ent, trace backwards to make sure that this ent is the only thing in the way. + + pTraceEnt = tr.pHit;// store the ent that the trace hit, for comparison + + UTIL_TraceLine ( m_pNodes[ j ].m_vecOrigin, + m_pNodes[ i ].m_vecOrigin, + ignore_monsters, + g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about + &tr ); + + +// there is a solid_bsp ent in the way of these two nodes, so we must record several things about in order to keep +// track of it in the pathfinding code, as well as through save and restore of the node graph. ANY data that is manipulated +// as part of the process of adding a LINKENT to a connection here must also be done in CGraph::SetGraphPointers, where reloaded +// graphs are prepared for use. + if ( tr.pHit == pTraceEnt && !FClassnameIs( tr.pHit, "worldspawn" ) ) + { + // get a pointer + pLinkPool [ cTotalLinks ].m_pLinkEnt = VARS( tr.pHit ); + + // record the modelname, so that we can save/load node trees + memcpy( pLinkPool [ cTotalLinks ].m_szLinkEntModelname, STRING( VARS(tr.pHit)->model ), 4 ); + + // set the flag for this ent that indicates that it is attached to the world graph + // if this ent is removed from the world, it must also be removed from the connections + // that it formerly blocked. + if ( !FBitSet( VARS( tr.pHit )->flags, FL_GRAPHED ) ) + { + VARS( tr.pHit )->flags += FL_GRAPHED; + } + } + else + {// even if the ent wasn't there, these nodes couldn't be connected. Skip. + continue; + } + } + + if ( file ) + { + fprintf ( file, "%4d", j ); + + if ( !FNullEnt( pLinkPool[ cTotalLinks ].m_pLinkEnt ) ) + {// record info about the ent in the way, if any. + fprintf ( file, " Entity on connection: %s, name: %s Model: %s", STRING( VARS( pTraceEnt )->classname ), STRING ( VARS( pTraceEnt )->targetname ), STRING ( VARS(tr.pHit)->model ) ); + } + + fprintf ( file, "\n", j ); + } + + pLinkPool [ cTotalLinks ].m_iDestNode = j; + cLinksThisNode++; + cTotalLinks++; + + // If we hit this, either a level designer is placing too many nodes in the same area, or + // we need to allow for a larger initial link pool. + if ( cLinksThisNode == MAX_NODE_INITIAL_LINKS ) + { + ALERT ( at_aiconsole, "**LinkVisibleNodes:\nNode %d has NodeLinks > MAX_NODE_INITIAL_LINKS", i ); + fprintf ( file, "** NODE %d HAS NodeLinks > MAX_NODE_INITIAL_LINKS **\n", i ); + *piBadNode = i; + return FALSE; + } + else if ( cTotalLinks > MAX_NODE_INITIAL_LINKS * m_cNodes ) + {// this is paranoia + ALERT ( at_aiconsole, "**LinkVisibleNodes:\nTotalLinks > MAX_NODE_INITIAL_LINKS * NUMNODES" ); + *piBadNode = i; + return FALSE; + } + + if ( cLinksThisNode == 0 ) + { + fprintf ( file, "**NO INITIAL LINKS**\n" ); + } + + // record the connection info in the link pool + WorldGraph.m_pNodes [ i ].m_cNumLinks = cLinksThisNode; + + // keep track of the most initial links ANY node had, so we can figure out + // if we have a large enough default link pool + if ( cLinksThisNode > cMaxInitialLinks ) + { + cMaxInitialLinks = cLinksThisNode; + } + } + + + if ( file ) + { + fprintf ( file, "----------------------------------------------------------------------------\n" ); + } + } + + fprintf ( file, "\n%4d Total Initial Connections - %4d Maximum connections for a single node.\n", cTotalLinks, cMaxInitialLinks ); + fprintf ( file, "----------------------------------------------------------------------------\n\n\n" ); + + return cTotalLinks; +} + +//========================================================= +// CGraph - RejectInlineLinks - expects a pointer to a link +// pool, and a pointer to and already-open file ( if you +// want status reports written to disk ). RETURNS the number +// of connections that were rejected +//========================================================= +int CGraph :: RejectInlineLinks ( CLink *pLinkPool, FILE *file ) +{ + int i,j,k; + + int cRejectedLinks; + + BOOL fRestartLoop;// have to restart the J loop if we eliminate a link. + + CNode *pSrcNode; + CNode *pCheckNode;// the node we are testing for (one of pSrcNode's connections) + CNode *pTestNode;// the node we are checking against ( also one of pSrcNode's connections) + + float flDistToTestNode, flDistToCheckNode; + + Vector2D vec2DirToTestNode, vec2DirToCheckNode; + + if ( file ) + { + fprintf ( file, "----------------------------------------------------------------------------\n" ); + fprintf ( file, "InLine Rejection:\n" ); + fprintf ( file, "----------------------------------------------------------------------------\n" ); + } + + cRejectedLinks = 0; + + for ( i = 0 ; i < m_cNodes ; i++ ) + { + pSrcNode = &m_pNodes[ i ]; + + if ( file ) + { + fprintf ( file, "Node %3d:\n", i ); + } + + for ( j = 0 ; j < pSrcNode->m_cNumLinks ; j++ ) + { + pCheckNode = &m_pNodes[ pLinkPool[ pSrcNode->m_iFirstLink + j ].m_iDestNode ]; + + vec2DirToCheckNode = ( pCheckNode->m_vecOrigin - pSrcNode->m_vecOrigin ).Make2D(); + flDistToCheckNode = vec2DirToCheckNode.Length(); + vec2DirToCheckNode = vec2DirToCheckNode.Normalize(); + + pLinkPool[ pSrcNode->m_iFirstLink + j ].m_flWeight = flDistToCheckNode; + + fRestartLoop = FALSE; + for ( k = 0 ; k < pSrcNode->m_cNumLinks && !fRestartLoop ; k++ ) + { + if ( k == j ) + {// don't check against same node + continue; + } + + pTestNode = &m_pNodes [ pLinkPool[ pSrcNode->m_iFirstLink + k ].m_iDestNode ]; + + vec2DirToTestNode = ( pTestNode->m_vecOrigin - pSrcNode->m_vecOrigin ).Make2D(); + + flDistToTestNode = vec2DirToTestNode.Length(); + vec2DirToTestNode = vec2DirToTestNode.Normalize(); + + if ( DotProduct ( vec2DirToCheckNode, vec2DirToTestNode ) >= 0.998 ) + { + // there's a chance that TestNode intersects the line to CheckNode. If so, we should disconnect the link to CheckNode. + if ( flDistToTestNode < flDistToCheckNode ) + { + if ( file ) + { + fprintf ( file, "REJECTED NODE %3d through Node %3d, Dot = %8f\n", pLinkPool[ pSrcNode->m_iFirstLink + j ].m_iDestNode, pLinkPool[ pSrcNode->m_iFirstLink + k ].m_iDestNode, DotProduct ( vec2DirToCheckNode, vec2DirToTestNode ) ); + } + + pLinkPool[ pSrcNode->m_iFirstLink + j ] = pLinkPool[ pSrcNode->m_iFirstLink + ( pSrcNode->m_cNumLinks - 1 ) ]; + pSrcNode->m_cNumLinks--; + j--; + + cRejectedLinks++;// keeping track of how many links are cut, so that we can return that value. + + fRestartLoop = TRUE; + } + } + } + } + + if ( file ) + { + fprintf ( file, "----------------------------------------------------------------------------\n\n" ); + } + } + + return cRejectedLinks; +} + +//========================================================= +// TestHull is a modelless clip hull that verifies reachable +// nodes by walking from every node to each of it's connections +//========================================================= +class CTestHull : public CBaseMonster +{ + +public: + void Spawn( entvars_t *pevMasterNode ); + virtual int ObjectCaps( void ) { return CBaseMonster :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + void EXPORT CallBuildNodeGraph ( void ); + void BuildNodeGraph ( void ); + void EXPORT ShowBadNode ( void ); + void EXPORT DropDelay ( void ); + void EXPORT PathFind ( void ); + + Vector vecBadNodeOrigin; +}; + +LINK_ENTITY_TO_CLASS( testhull, CTestHull ); + +//========================================================= +// CTestHull::Spawn +//========================================================= +void CTestHull :: Spawn( entvars_t *pevMasterNode ) +{ + SET_MODEL(ENT(pev), "models/player.mdl"); + UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + pev->effects = 0; + pev->health = 50; + pev->yaw_speed = 8; + + if ( WorldGraph.m_fGraphPresent ) + {// graph loaded from disk, so we don't need the test hull + SetThink( Remove ); + SetNextThink( 0 ); + } + else + { + SetThink(&CTestHull :: DropDelay ); + SetNextThink( 1 ); + } + + // Make this invisible + // UNDONE: Shouldn't we just use EF_NODRAW? This doesn't need to go to the client. + pev->rendermode = kRenderTransTexture; + pev->renderamt = 0; +} + +//========================================================= +// TestHull::DropDelay - spawns TestHull on top of +// the 0th node and drops it to the ground. +//========================================================= +void CTestHull::DropDelay ( void ) +{ + UTIL_CenterPrintAll( "Node Graph out of Date. Rebuilding..." ); + + UTIL_SetOrigin ( this, WorldGraph.m_pNodes[ 0 ].m_vecOrigin ); + + SetThink(&CTestHull:: CallBuildNodeGraph ); + + SetNextThink( 1 ); +} + +//========================================================= +// nodes start out as ents in the world. As they are spawned, +// the node info is recorded then the ents are discarded. +//========================================================= +void CNodeEnt :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "hinttype")) + { + m_sHintType = (short)atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + + if (FStrEq(pkvd->szKeyName, "activity")) + { + m_sHintActivity = (short)atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else + CBaseEntity::KeyValue( pkvd ); +} + +//========================================================= +//========================================================= +void CNodeEnt :: Spawn( void ) +{ + pev->movetype = MOVETYPE_NONE; + pev->solid = SOLID_NOT;// always solid_not + + if ( WorldGraph.m_fGraphPresent ) + {// graph loaded from disk, so discard all these node ents as soon as they spawn + REMOVE_ENTITY( edict() ); + return; + } + + if ( WorldGraph.m_cNodes == 0 ) + {// this is the first node to spawn, spawn the test hull entity that builds and walks the node tree + CTestHull *pHull = GetClassPtr((CTestHull *)NULL); + pHull->Spawn( pev ); + } + + if ( WorldGraph.m_cNodes >= MAX_NODES ) + { + ALERT ( at_aiconsole, "cNodes > MAX_NODES\n" ); + return; + } + + WorldGraph.m_pNodes[ WorldGraph.m_cNodes ].m_vecOriginPeek = + WorldGraph.m_pNodes[ WorldGraph.m_cNodes ].m_vecOrigin = pev->origin; + WorldGraph.m_pNodes[ WorldGraph.m_cNodes ].m_flHintYaw = pev->angles.y; + WorldGraph.m_pNodes[ WorldGraph.m_cNodes ].m_sHintType = m_sHintType; + WorldGraph.m_pNodes[ WorldGraph.m_cNodes ].m_sHintActivity = m_sHintActivity; + + if (FClassnameIs( pev, "info_node_air" )) + WorldGraph.m_pNodes[ WorldGraph.m_cNodes ].m_afNodeInfo = bits_NODE_AIR; + else + WorldGraph.m_pNodes[ WorldGraph.m_cNodes ].m_afNodeInfo = 0; + + WorldGraph.m_cNodes++; + + REMOVE_ENTITY( edict() ); +} + +//========================================================= +// CTestHull - ShowBadNode - makes a bad node fizzle. When +// there's a problem with node graph generation, the test +// hull will be placed up the bad node's location and will generate +// particles +//========================================================= +void CTestHull :: ShowBadNode( void ) +{ + pev->movetype = MOVETYPE_FLY; + pev->angles.y = pev->angles.y + 4; + + UTIL_MakeVectors ( pev->angles ); + + UTIL_ParticleEffect ( pev->origin, g_vecZero, 255, 25 ); + UTIL_ParticleEffect ( pev->origin + gpGlobals->v_forward * 64, g_vecZero, 255, 25 ); + UTIL_ParticleEffect ( pev->origin - gpGlobals->v_forward * 64, g_vecZero, 255, 25 ); + UTIL_ParticleEffect ( pev->origin + gpGlobals->v_right * 64, g_vecZero, 255, 25 ); + UTIL_ParticleEffect ( pev->origin - gpGlobals->v_right * 64, g_vecZero, 255, 25 ); + + SetNextThink( 0.1 ); +} + +extern BOOL gTouchDisabled; +void CTestHull::CallBuildNodeGraph( void ) +{ + // TOUCH HACK -- Don't allow this entity to call anyone's "touch" function + gTouchDisabled = TRUE; + BuildNodeGraph(); + gTouchDisabled = FALSE; + // Undo TOUCH HACK +} + +//========================================================= +// BuildNodeGraph - think function called by the empty walk +// hull that is spawned by the first node to spawn. This +// function links all nodes that can see each other, then +// eliminates all inline links, then uses a monster-sized +// hull that walks between each node and each of its links +// to ensure that a monster can actually fit through the space +//========================================================= +void CTestHull :: BuildNodeGraph( void ) +{ + TraceResult tr; + FILE *file; + + char szNrpFilename [MAX_PATH];// text node report filename + + CLink *pTempPool; // temporary link pool + + CNode *pSrcNode;// node we're currently working with + CNode *pDestNode;// the other node in comparison operations + + BOOL fSkipRemainingHulls;//if smallest hull can't fit, don't check any others + BOOL fPairsValid;// are all links in the graph evenly paired? + + int i, j, hull; + + int iBadNode;// this is the node that caused graph generation to fail + + int cMaxInitialLinks = 0; + int cMaxValidLinks = 0; + + int iPoolIndex = 0; + int cPoolLinks;// number of links in the pool. + + Vector vecDirToCheckNode; + Vector vecDirToTestNode; + Vector vecStepCheckDir; + Vector vecTraceSpot; + Vector vecSpot; + + Vector2D vec2DirToCheckNode; + Vector2D vec2DirToTestNode; + Vector2D vec2StepCheckDir; + Vector2D vec2TraceSpot; + Vector2D vec2Spot; + + float flYaw;// use this stuff to walk the hull between nodes + float flDist; + int step; + + SetThink( Remove );// no matter what happens, the hull gets rid of itself. + SetNextThink( 0 ); + +// malloc a swollen temporary connection pool that we trim down after we know exactly how many connections there are. + pTempPool = (CLink *)CALLOC ( sizeof ( CLink ) , ( WorldGraph.m_cNodes * MAX_NODE_INITIAL_LINKS ) ); + if ( !pTempPool ) + { + ALERT ( at_aiconsole, "**Could not malloc TempPool!\n" ); + return; + } + + + // make sure directories have been made + GET_GAME_DIR( szNrpFilename ); + strcat( szNrpFilename, "/maps" ); + CreateDirectory( szNrpFilename, NULL ); + strcat( szNrpFilename, "/graphs" ); + CreateDirectory( szNrpFilename, NULL ); + + strcat( szNrpFilename, "/" ); + strcat( szNrpFilename, STRING( gpGlobals->mapname ) ); + strcat( szNrpFilename, ".nrp" ); + + //disable nrp files + file = fopen ( szNrpFilename, "w+" ); + + if ( !file ) + {// file error + ALERT ( at_aiconsole, "Couldn't create %s!\n", szNrpFilename ); + + if ( pTempPool ) + { + FREE( pTempPool ); + } + + return; + } + + fprintf( file, "Node Graph Report for map: %s.bsp\n", STRING(gpGlobals->mapname) ); + fprintf ( file, "%d Total Nodes\n\n", WorldGraph.m_cNodes ); + + for ( i = 0 ; i < WorldGraph.m_cNodes ; i++ ) + {// print all node numbers and their locations to the file. + WorldGraph.m_pNodes[ i ].m_cNumLinks = 0; + WorldGraph.m_pNodes[ i ].m_iFirstLink = 0; + memset(WorldGraph.m_pNodes[ i ].m_pNextBestNode, 0, sizeof(WorldGraph.m_pNodes[ i ].m_pNextBestNode)); + + fprintf ( file, "Node# %4d\n", i ); + fprintf ( file, "Location %4d,%4d,%4d\n",(int)WorldGraph.m_pNodes[ i ].m_vecOrigin.x, (int)WorldGraph.m_pNodes[ i ].m_vecOrigin.y, (int)WorldGraph.m_pNodes[ i ].m_vecOrigin.z ); + fprintf ( file, "HintType: %4d\n", WorldGraph.m_pNodes[ i ].m_sHintType ); + fprintf ( file, "HintActivity: %4d\n", WorldGraph.m_pNodes[ i ].m_sHintActivity ); + fprintf ( file, "HintYaw: %4f\n", WorldGraph.m_pNodes[ i ].m_flHintYaw ); + fprintf ( file, "-------------------------------------------------------------------------------\n" ); + } + fprintf ( file, "\n\n" ); + + + // Automatically recognize WATER nodes and drop the LAND nodes to the floor. + // + for ( i = 0; i < WorldGraph.m_cNodes; i++) + { + if( WorldGraph.m_pNodes[i].m_afNodeInfo & bits_NODE_AIR ) + { + // do nothing + } + else if( UTIL_PointContents( WorldGraph.m_pNodes[i].m_vecOrigin) & MASK_WATER ) + { + WorldGraph.m_pNodes[i].m_afNodeInfo |= bits_NODE_WATER; + } + else + { + WorldGraph.m_pNodes[i].m_afNodeInfo |= bits_NODE_LAND; + + // trace to the ground, then pop up 8 units and place node there to make it + // easier for them to connect (think stairs, chairs, and bumps in the floor). + // After the routing is done, push them back down. + // + TraceResult tr; + + UTIL_TraceLine( WorldGraph.m_pNodes[i].m_vecOrigin, + WorldGraph.m_pNodes[i].m_vecOrigin - Vector ( 0, 0, 384 ), + ignore_monsters, + g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about + &tr ); + + // This trace is ONLY used if we hit an entity flagged with FL_WORLDBRUSH + TraceResult trEnt; + UTIL_TraceLine ( WorldGraph.m_pNodes[i].m_vecOrigin, + WorldGraph.m_pNodes[i].m_vecOrigin - Vector ( 0, 0, 384 ), + dont_ignore_monsters, + g_pBodyQueueHead,//!!!HACKHACK no real ent to supply here, using a global we don't care about + &trEnt ); + + + // Did we hit something closer than the floor? + if ( trEnt.flFraction < tr.flFraction ) + { + // If it was a world brush entity, copy the node location + if ( trEnt.pHit && (trEnt.pHit->v.flags & FL_WORLDBRUSH) ) + tr.vecEndPos = trEnt.vecEndPos; + } + + WorldGraph.m_pNodes[i].m_vecOriginPeek.z = + WorldGraph.m_pNodes[i].m_vecOrigin.z = tr.vecEndPos.z + NODE_HEIGHT; + } + } + + cPoolLinks = WorldGraph.LinkVisibleNodes( pTempPool, file, &iBadNode ); + + if ( !cPoolLinks ) + { + ALERT ( at_aiconsole, "**ConnectVisibleNodes FAILED!\n" ); + + SetThink(&CTestHull :: ShowBadNode );// send the hull off to show the offending node. + //pev->solid = SOLID_NOT; + pev->origin = WorldGraph.m_pNodes[ iBadNode ].m_vecOrigin; + + if ( pTempPool ) + { + FREE( pTempPool ); + } + + if ( file ) + {// close the file + fclose ( file ); + } + + return; + } + +// send the walkhull to all of this node's connections now. We'll do this here since +// so much of it relies on being able to control the test hull. + fprintf ( file, "----------------------------------------------------------------------------\n" ); + fprintf ( file, "Walk Rejection:\n"); + + for ( i = 0 ; i < WorldGraph.m_cNodes ; i++ ) + { + pSrcNode = &WorldGraph.m_pNodes[ i ]; + + fprintf ( file, "-------------------------------------------------------------------------------\n"); + fprintf ( file, "Node %4d:\n\n", i ); + + for ( j = 0 ; j < pSrcNode->m_cNumLinks ; j++ ) + { + // assume that all hulls can walk this link, then eliminate the ones that can't. + pTempPool [ pSrcNode->m_iFirstLink + j ].m_afLinkInfo = bits_LINK_SMALL_HULL | bits_LINK_HUMAN_HULL | bits_LINK_LARGE_HULL | bits_LINK_FLY_HULL; + + + // do a check for each hull size. + + // if we can't fit a tiny hull through a connection, no other hulls with fit either, so we + // should just fall out of the loop. Do so by setting the SkipRemainingHulls flag. + fSkipRemainingHulls = FALSE; + for ( hull = 0 ; hull < MAX_NODE_HULLS; hull++ ) + { + if (fSkipRemainingHulls && (hull == NODE_HUMAN_HULL || hull == NODE_LARGE_HULL)) // skip the remaining walk hulls + continue; + + switch ( hull ) + { + case NODE_SMALL_HULL: + UTIL_SetSize(pev, Vector(-12, -12, 0), Vector(12, 12, 24)); + break; + case NODE_HUMAN_HULL: + UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX ); + break; + case NODE_LARGE_HULL: + UTIL_SetSize(pev, Vector(-32, -32, 0), Vector(32, 32, 64)); + break; + case NODE_FLY_HULL: + UTIL_SetSize(pev, Vector(-32, -32, 0), Vector(32, 32, 64)); + // UTIL_SetSize(pev, Vector(0, 0, 0), Vector(0, 0, 0)); + break; + } + + UTIL_SetOrigin ( this, pSrcNode->m_vecOrigin );// place the hull on the node + + if ( !FBitSet ( pev->flags, FL_ONGROUND ) ) + { + ALERT ( at_aiconsole, "OFFGROUND!\n" ); + } + + // now build a yaw that points to the dest node, and get the distance. + if ( j < 0 ) + { + ALERT ( at_aiconsole, "**** j = %d ****\n", j ); + if ( pTempPool ) + { + FREE( pTempPool ); + } + + if ( file ) + {// close the file + fclose ( file ); + } + return; + } + + pDestNode = &WorldGraph.m_pNodes [ pTempPool[ pSrcNode->m_iFirstLink + j ].m_iDestNode ]; + + vecSpot = pDestNode->m_vecOrigin; + //vecSpot.z = pev->origin.z; + + if (hull < NODE_FLY_HULL) + { + int SaveFlags = pev->flags; + int MoveMode = WALKMOVE_WORLDONLY; + if (pSrcNode->m_afNodeInfo & bits_NODE_WATER) + { + pev->flags |= FL_SWIM; + MoveMode = WALKMOVE_NORMAL; + } + + flYaw = UTIL_VecToYaw ( pDestNode->m_vecOrigin - pev->origin ); + + flDist = ( vecSpot - pev->origin ).Length2D(); + + int fWalkFailed = FALSE; + + // in this loop we take tiny steps from the current node to the nodes that it links to, one at a time. + // pev->angles.y = flYaw; + for ( step = 0 ; step < flDist && !fWalkFailed ; step += HULL_STEP_SIZE ) + { + float stepSize = HULL_STEP_SIZE; + + if ( (step + stepSize) >= (flDist-1) ) + stepSize = (flDist - step) - 1; + + if ( !WALK_MOVE( ENT(pev), flYaw, stepSize, MoveMode ) ) + {// can't take the next step + + fWalkFailed = TRUE; + break; + } + } + + if (!fWalkFailed && (pev->origin - vecSpot).Length() > 64) + { + // ALERT( at_console, "bogus walk\n"); + // we thought we + fWalkFailed = TRUE; + } + + if (fWalkFailed) + { + + //pTempPool[ pSrcNode->m_iFirstLink + j ] = pTempPool [ pSrcNode->m_iFirstLink + ( pSrcNode->m_cNumLinks - 1 ) ]; + + // now me must eliminate the hull that couldn't walk this connection + switch ( hull ) + { + case NODE_SMALL_HULL: // if this hull can't fit, nothing can, so drop the connection + fprintf ( file, "NODE_SMALL_HULL step %f\n", step ); + pTempPool[ pSrcNode->m_iFirstLink + j ].m_afLinkInfo &= ~(bits_LINK_SMALL_HULL | bits_LINK_HUMAN_HULL | bits_LINK_LARGE_HULL); + fSkipRemainingHulls = TRUE;// don't bother checking larger hulls + break; + case NODE_HUMAN_HULL: + fprintf ( file, "NODE_HUMAN_HULL step %f\n", step ); + pTempPool[ pSrcNode->m_iFirstLink + j ].m_afLinkInfo &= ~(bits_LINK_HUMAN_HULL | bits_LINK_LARGE_HULL); + fSkipRemainingHulls = TRUE;// don't bother checking larger hulls + break; + case NODE_LARGE_HULL: + fprintf ( file, "NODE_LARGE_HULL step %f\n", step ); + pTempPool[ pSrcNode->m_iFirstLink + j ].m_afLinkInfo &= ~bits_LINK_LARGE_HULL; + break; + } + } + pev->flags = SaveFlags; + } + else + { + TraceResult tr; + + UTIL_TraceHull( pSrcNode->m_vecOrigin + Vector( 0, 0, 32 ), pDestNode->m_vecOriginPeek + Vector( 0, 0, 32 ), ignore_monsters, large_hull, ENT( pev ), &tr ); + if (tr.fStartSolid || tr.flFraction < 1.0) + { + pTempPool[ pSrcNode->m_iFirstLink + j ].m_afLinkInfo &= ~bits_LINK_FLY_HULL; + } + } + } + + if (pTempPool[ pSrcNode->m_iFirstLink + j ].m_afLinkInfo == 0) + { + fprintf ( file, "Rejected Node %3d - Unreachable by ", pTempPool [ pSrcNode->m_iFirstLink + j ].m_iDestNode ); + pTempPool[ pSrcNode->m_iFirstLink + j ] = pTempPool [ pSrcNode->m_iFirstLink + ( pSrcNode->m_cNumLinks - 1 ) ]; + fprintf ( file, "Any Hull\n" ); + + pSrcNode->m_cNumLinks--; + cPoolLinks--;// we just removed a link, so decrement the total number of links in the pool. + j--; + } + + } + } + fprintf ( file, "-------------------------------------------------------------------------------\n\n\n"); + + cPoolLinks -= WorldGraph.RejectInlineLinks ( pTempPool, file ); + +// now malloc a pool just large enough to hold the links that are actually used + WorldGraph.m_pLinkPool = (CLink *) CALLOC ( sizeof ( CLink ), cPoolLinks ); + + if ( !WorldGraph.m_pLinkPool ) + {// couldn't make the link pool! + ALERT ( at_aiconsole, "Couldn't malloc LinkPool!\n" ); + if ( pTempPool ) + { + FREE( pTempPool ); + } + if ( file ) + {// close the file + fclose ( file ); + } + + return; + } + WorldGraph.m_cLinks = cPoolLinks; + +//copy only the used portions of the TempPool into the graph's link pool + int iFinalPoolIndex = 0; + int iOldFirstLink; + + for ( i = 0 ; i < WorldGraph.m_cNodes ; i++ ) + { + iOldFirstLink = WorldGraph.m_pNodes[ i ].m_iFirstLink;// store this, because we have to re-assign it before entering the copy loop + + WorldGraph.m_pNodes[ i ].m_iFirstLink = iFinalPoolIndex; + + for ( j = 0 ; j < WorldGraph.m_pNodes[ i ].m_cNumLinks ; j++ ) + { + WorldGraph.m_pLinkPool[ iFinalPoolIndex++ ] = pTempPool[ iOldFirstLink + j ]; + } + } + + + // Node sorting numbers linked nodes close to each other + // + WorldGraph.SortNodes(); + + // This is used for HashSearch + // + WorldGraph.BuildLinkLookups(); + + fPairsValid = TRUE; // assume that the connection pairs are all valid to start + + fprintf ( file, "\n\n-------------------------------------------------------------------------------\n"); + fprintf ( file, "Link Pairings:\n"); + +// link integrity check. The idea here is that if Node A links to Node B, node B should +// link to node A. If not, we have a situation that prevents us from using a basic +// optimization in the FindNearestLink function. + for ( i = 0 ; i < WorldGraph.m_cNodes ; i++ ) + { + for ( j = 0 ; j < WorldGraph.m_pNodes[ i ].m_cNumLinks ; j++ ) + { + int iLink; + WorldGraph.HashSearch(WorldGraph.INodeLink(i,j), i, iLink); + if (iLink < 0) + { + fPairsValid = FALSE;// unmatched link pair. + fprintf ( file, "WARNING: Node %3d does not connect back to Node %3d\n", WorldGraph.INodeLink(i, j), i); + } + } + } + + // !!!LATER - if all connections are properly paired, when can enable an optimization in the pathfinding code + // (in the find nearest line function) + if ( fPairsValid ) + { + fprintf ( file, "\nAll Connections are Paired!\n"); + } + + fprintf ( file, "-------------------------------------------------------------------------------\n"); + fprintf ( file, "\n\n-------------------------------------------------------------------------------\n"); + fprintf ( file, "Total Number of Connections in Pool: %d\n", cPoolLinks ); + fprintf ( file, "-------------------------------------------------------------------------------\n"); + fprintf ( file, "Connection Pool: %d bytes\n", sizeof ( CLink ) * cPoolLinks ); + fprintf ( file, "-------------------------------------------------------------------------------\n"); + + + ALERT ( at_aiconsole, "%d Nodes, %d Connections\n", WorldGraph.m_cNodes, cPoolLinks ); + + // This is used for FindNearestNode + // + WorldGraph.BuildRegionTables(); + + + // Push all of the LAND nodes down to the ground now. Leave the water and air nodes alone. + // + for ( i = 0 ; i < WorldGraph.m_cNodes ; i++ ) + { + if ((WorldGraph.m_pNodes[ i ].m_afNodeInfo & bits_NODE_LAND)) + { + WorldGraph.m_pNodes[ i ].m_vecOrigin.z -= NODE_HEIGHT; + } + } + + + if ( pTempPool ) + { + // free the temp pool + FREE( pTempPool ); + } + + if ( file ) + { + fclose ( file ); + } + + // We now have some graphing capabilities. + // + WorldGraph.m_fGraphPresent = TRUE;//graph is in memory. + WorldGraph.m_fGraphPointersSet = TRUE;// since the graph was generated, the pointers are ready + WorldGraph.m_fRoutingComplete = FALSE; // Optimal routes aren't computed, yet. + + // Compute and compress the routing information. + // + WorldGraph.ComputeStaticRoutingTables(); + +// save the node graph for this level + WorldGraph.FSaveGraph( (char *)STRING( gpGlobals->mapname ) ); + ALERT( at_debug, "Done.\n"); +} + + +//========================================================= +// returns a hardcoded path. +//========================================================= +void CTestHull :: PathFind ( void ) +{ + int iPath[ 50 ]; + int iPathSize; + int i; + CNode *pNode, *pNextNode; + + if ( !WorldGraph.m_fGraphPresent || !WorldGraph.m_fGraphPointersSet ) + {// protect us in the case that the node graph isn't available + ALERT ( at_aiconsole, "Graph not ready!\n" ); + return; + } + + iPathSize = WorldGraph.FindShortestPath ( iPath, 0, 19, 0, 0 ); // UNDONE use hull constant + + if ( !iPathSize ) + { + ALERT ( at_aiconsole, "No Path!\n" ); + return; + } + + ALERT ( at_aiconsole, "%d\n", iPathSize ); + + pNode = &WorldGraph.m_pNodes[ iPath [ 0 ] ]; + + for ( i = 0 ; i < iPathSize - 1 ; i++ ) + { + + pNextNode = &WorldGraph.m_pNodes[ iPath [ i + 1 ] ]; + + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SHOWLINE); + + WRITE_COORD( pNode->m_vecOrigin.x ); + WRITE_COORD( pNode->m_vecOrigin.y ); + WRITE_COORD( pNode->m_vecOrigin.z + NODE_HEIGHT ); + + WRITE_COORD( pNextNode->m_vecOrigin.x); + WRITE_COORD( pNextNode->m_vecOrigin.y); + WRITE_COORD( pNextNode->m_vecOrigin.z + NODE_HEIGHT); + MESSAGE_END(); + + pNode = pNextNode; + } + +} + + +//========================================================= +// CStack Constructor +//========================================================= +CStack :: CStack( void ) +{ + m_level = 0; +} + +//========================================================= +// pushes a value onto the stack +//========================================================= +void CStack :: Push( int value ) +{ + if ( m_level >= MAX_STACK_NODES ) + { + printf("Error!\n"); + return; + } + m_stack[m_level] = value; + m_level++; +} + +//========================================================= +// pops a value off of the stack +//========================================================= +int CStack :: Pop( void ) +{ + if ( m_level <= 0 ) + return -1; + + m_level--; + return m_stack[ m_level ]; +} + +//========================================================= +// returns the value on the top of the stack +//========================================================= +int CStack :: Top ( void ) +{ + return m_stack[ m_level - 1 ]; +} + +//========================================================= +// copies every element on the stack into an array LIFO +//========================================================= +void CStack :: CopyToArray ( int *piArray ) +{ + int i; + + for ( i = 0 ; i < m_level ; i++ ) + { + piArray[ i ] = m_stack[ i ]; + } +} + +//========================================================= +// CQueue constructor +//========================================================= +CQueue :: CQueue( void ) +{ + m_cSize = 0; + m_head = 0; + m_tail = -1; +} + +//========================================================= +// inserts a value into the queue +//========================================================= +void CQueue :: Insert ( int iValue, float fPriority ) +{ + + if ( Full() ) + { + printf ( "Queue is full!\n" ); + return; + } + + m_tail++; + + if ( m_tail == MAX_STACK_NODES ) + {//wrap around + m_tail = 0; + } + + m_queue[ m_tail ].Id = iValue; + m_queue[ m_tail ].Priority = fPriority; + m_cSize++; +} + +//========================================================= +// removes a value from the queue (FIFO) +//========================================================= +int CQueue :: Remove ( float &fPriority ) +{ + if ( m_head == MAX_STACK_NODES ) + {// wrap + m_head = 0; + } + + m_cSize--; + fPriority = m_queue[ m_head ].Priority; + return m_queue[ m_head++ ].Id; +} + +//========================================================= +// CQueue constructor +//========================================================= +CQueuePriority :: CQueuePriority( void ) +{ + m_cSize = 0; +} + +//========================================================= +// inserts a value into the priority queue +//========================================================= +void CQueuePriority :: Insert( int iValue, float fPriority ) +{ + + if ( Full() ) + { + printf ( "Queue is full!\n" ); + return; + } + + m_heap[ m_cSize ].Priority = fPriority; + m_heap[ m_cSize ].Id = iValue; + m_cSize++; + Heap_SiftUp(); +} + +//========================================================= +// removes the smallest item from the priority queue +// +//========================================================= +int CQueuePriority :: Remove( float &fPriority ) +{ + int iReturn = m_heap[ 0 ].Id; + fPriority = m_heap[ 0 ].Priority; + + m_cSize--; + + m_heap[ 0 ] = m_heap[ m_cSize ]; + + Heap_SiftDown(0); + return iReturn; +} + +#define HEAP_LEFT_CHILD(x) (2*(x)+1) +#define HEAP_RIGHT_CHILD(x) (2*(x)+2) +#define HEAP_PARENT(x) (((x)-1)/2) + +void CQueuePriority::Heap_SiftDown(int iSubRoot) +{ + int parent = iSubRoot; + int child = HEAP_LEFT_CHILD(parent); + + struct tag_HEAP_NODE Ref = m_heap[ parent ]; + + while (child < m_cSize) + { + int rightchild = HEAP_RIGHT_CHILD(parent); + if (rightchild < m_cSize) + { + if ( m_heap[ rightchild ].Priority < m_heap[ child ].Priority ) + { + child = rightchild; + } + } + if ( Ref.Priority <= m_heap[ child ].Priority ) + break; + + m_heap[ parent ] = m_heap[ child ]; + parent = child; + child = HEAP_LEFT_CHILD(parent); + } + m_heap[ parent ] = Ref; +} + +void CQueuePriority::Heap_SiftUp(void) +{ + int child = m_cSize-1; + while (child) + { + int parent = HEAP_PARENT(child); + if ( m_heap[ parent ].Priority <= m_heap[ child ].Priority ) + break; + + struct tag_HEAP_NODE Tmp; + Tmp = m_heap[ child ]; + m_heap[ child ] = m_heap[ parent ]; + m_heap[ parent ] = Tmp; + + child = parent; + } +} + +//========================================================= +// CGraph - FLoadGraph - attempts to load a node graph from disk. +// if the current level is maps/snar.bsp, maps/graphs/snar.nod +// will be loaded. If file cannot be loaded, the node tree +// will be created and saved to disk. +//========================================================= +int CGraph :: FLoadGraph ( char *szMapName ) +{ + char szFilename[MAX_PATH]; + int iVersion; + int length; + byte *aMemFile; + byte *pMemFile; + + // make sure the directories have been made + char szDirName[MAX_PATH]; + GET_GAME_DIR( szDirName ); + strcat( szDirName, "/maps" ); + CreateDirectory( szDirName, NULL ); + strcat( szDirName, "/graphs" ); + CreateDirectory( szDirName, NULL ); + + strcpy ( szFilename, "maps/graphs/" ); + strcat ( szFilename, szMapName ); + strcat( szFilename, ".nod" ); + + pMemFile = aMemFile = LOAD_FILE(szFilename, &length); + + if ( !aMemFile ) + { + return FALSE; + } + else + { + // Read the graph version number + // + length -= sizeof(int); + if (length < 0) goto ShortFile; + memcpy(&iVersion, pMemFile, sizeof(int)); + pMemFile += sizeof(int); + + if ( iVersion != GRAPH_VERSION ) + { + // This file was written by a different build of the dll! + // + ALERT ( at_aiconsole, "**ERROR** Graph version is %d, expected %d\n",iVersion, GRAPH_VERSION ); + goto ShortFile; + } + + // Read the graph class + // + length -= sizeof(CGraph); + if (length < 0) goto ShortFile; + memcpy(this, pMemFile, sizeof(CGraph)); + pMemFile += sizeof(CGraph); + + // Set the pointers to zero, just in case we run out of memory. + // + m_pNodes = NULL; + m_pLinkPool = NULL; + m_di = NULL; + m_pRouteInfo = NULL; + m_pHashLinks = NULL; + + + // Malloc for the nodes + // + m_pNodes = ( CNode * )CALLOC ( sizeof ( CNode ), m_cNodes ); + + if ( !m_pNodes ) + { + ALERT ( at_aiconsole, "**ERROR**\nCouldn't malloc %d nodes!\n", m_cNodes ); + goto NoMemory; + } + + // Read in all the nodes + // + length -= sizeof(CNode) * m_cNodes; + if (length < 0) goto ShortFile; + memcpy(m_pNodes, pMemFile, sizeof(CNode)*m_cNodes); + pMemFile += sizeof(CNode) * m_cNodes; + + + // Malloc for the link pool + // + m_pLinkPool = ( CLink * )CALLOC ( sizeof ( CLink ), m_cLinks ); + + if ( !m_pLinkPool ) + { + ALERT ( at_aiconsole, "**ERROR**\nCouldn't malloc %d link!\n", m_cLinks ); + goto NoMemory; + } + + // Read in all the links + // + length -= sizeof(CLink)*m_cLinks; + if (length < 0) goto ShortFile; + memcpy(m_pLinkPool, pMemFile, sizeof(CLink)*m_cLinks); + pMemFile += sizeof(CLink)*m_cLinks; + + // Malloc for the sorting info. + // + m_di = (DIST_INFO *)CALLOC( sizeof(DIST_INFO), m_cNodes ); + if ( !m_di ) + { + ALERT ( at_aiconsole, "***ERROR**\nCouldn't malloc %d entries sorting nodes!\n", m_cNodes ); + goto NoMemory; + } + + // Read it in. + // + length -= sizeof(DIST_INFO)*m_cNodes; + if (length < 0) goto ShortFile; + memcpy(m_di, pMemFile, sizeof(DIST_INFO)*m_cNodes); + pMemFile += sizeof(DIST_INFO)*m_cNodes; + + // Malloc for the routing info. + // + m_fRoutingComplete = FALSE; + m_pRouteInfo = (char *)CALLOC( sizeof(char), m_nRouteInfo ); + if ( !m_pRouteInfo ) + { + ALERT ( at_aiconsole, "***ERROR**\nCounldn't malloc %d route bytes!\n", m_nRouteInfo ); + goto NoMemory; + } + m_CheckedCounter = 0; + for (int i = 0; i < m_cNodes; i++) + { + m_di[i].m_CheckedEvent = 0; + } + + // Read in the route information. + // + length -= sizeof(char)*m_nRouteInfo; + if (length < 0) goto ShortFile; + memcpy(m_pRouteInfo, pMemFile, sizeof(char)*m_nRouteInfo); + pMemFile += sizeof(char)*m_nRouteInfo; + m_fRoutingComplete = TRUE;; + + // malloc for the hash links + // + m_pHashLinks = (short *)CALLOC(sizeof(short), m_nHashLinks); + if (!m_pHashLinks) + { + ALERT ( at_aiconsole, "***ERROR**\nCounldn't malloc %d hash link bytes!\n", m_nHashLinks ); + goto NoMemory; + } + + // Read in the hash link information + // + length -= sizeof(short)*m_nHashLinks; + if (length < 0) goto ShortFile; + memcpy(m_pHashLinks, pMemFile, sizeof(short)*m_nHashLinks); + pMemFile += sizeof(short)*m_nHashLinks; + + // Set the graph present flag, clear the pointers set flag + m_fGraphPresent = TRUE; + m_fGraphPointersSet = FALSE; + + FREE_FILE( aMemFile ); + + if( length != 0 ) + { + ALERT ( at_aiconsole, "***WARNING***:Node graph was longer than expected by %d bytes.!\n", length); + } + + return TRUE; + } + +ShortFile: +NoMemory: + FREE_FILE( aMemFile ); + return FALSE; +} + +//========================================================= +// CGraph - FSaveGraph - It's not rocket science. +// this WILL overwrite existing files. +//========================================================= +int CGraph :: FSaveGraph ( char *szMapName ) +{ + + int iVersion = GRAPH_VERSION; + char szFilename[MAX_PATH]; + FILE *file; + + if ( !m_fGraphPresent || !m_fGraphPointersSet ) + {// protect us in the case that the node graph isn't available or built + ALERT ( at_aiconsole, "Graph not ready!\n" ); + return FALSE; + } + + // make sure directories have been made + GET_GAME_DIR( szFilename ); + strcat( szFilename, "/maps" ); + CreateDirectory( szFilename, NULL ); + strcat( szFilename, "/graphs" ); + CreateDirectory( szFilename, NULL ); + + strcat( szFilename, "/" ); + strcat( szFilename, szMapName ); + strcat( szFilename, ".nod" ); + + file = fopen ( szFilename, "wb" ); + + ALERT ( at_aiconsole, "Created: %s\n", szFilename ); + + if ( !file ) + {// couldn't create + ALERT ( at_aiconsole, "Couldn't Create: %s\n", szFilename ); + return FALSE; + } + else + { + // write the version + fwrite ( &iVersion, sizeof ( int ), 1, file ); + + // write the CGraph class + fwrite ( this, sizeof ( CGraph ), 1, file ); + + // write the nodes + fwrite ( m_pNodes, sizeof ( CNode ), m_cNodes, file ); + + // write the links + fwrite ( m_pLinkPool, sizeof ( CLink ), m_cLinks, file ); + + fwrite ( m_di, sizeof(DIST_INFO), m_cNodes, file ); + + // Write the route info. + // + if ( m_pRouteInfo && m_nRouteInfo ) + { + fwrite ( m_pRouteInfo, sizeof( char ), m_nRouteInfo, file ); + } + + if (m_pHashLinks && m_nHashLinks) + { + fwrite(m_pHashLinks, sizeof(short), m_nHashLinks, file); + } + fclose ( file ); + return TRUE; + } +} + +//========================================================= +// CGraph - FSetGraphPointers - Takes the modelnames of +// all of the brush ents that block connections in the node +// graph and resolves them into pointers to those entities. +// this is done after loading the graph from disk, whereupon +// the pointers are not valid. +//========================================================= +int CGraph :: FSetGraphPointers ( void ) +{ + int i; + CBaseEntity *pLinkEnt; + + for ( i = 0 ; i < m_cLinks ; i++ ) + {// go through all of the links + + if ( m_pLinkPool[ i ].m_pLinkEnt != NULL ) + { + char name[5]; + // when graphs are saved, any valid pointers are will be non-zero, signifying that we should + // reset those pointers upon reloading. Any pointers that were NULL when the graph was saved + // will be NULL when reloaded, and will ignored by this function. + + // m_szLinkEntModelname is not necessarily NULL terminated (so we can store it in a more alignment-friendly 4 bytes) + memcpy( name, m_pLinkPool[ i ].m_szLinkEntModelname, 4 ); + name[4] = 0; + pLinkEnt = UTIL_FindEntityByString( NULL, "model", name ); + + if ( !pLinkEnt ) + { + // the ent isn't around anymore? Either there is a major problem, or it was removed from the world + // ( like a func_breakable that's been destroyed or something ). Make sure that LinkEnt is null. + ALERT ( at_aiconsole, "**Could not find model %s\n", name ); + m_pLinkPool[ i ].m_pLinkEnt = NULL; + } + else + { + m_pLinkPool[ i ].m_pLinkEnt = pLinkEnt->pev; + + if ( !FBitSet( m_pLinkPool[ i ].m_pLinkEnt->flags, FL_GRAPHED ) ) + { + m_pLinkPool[ i ].m_pLinkEnt->flags += FL_GRAPHED; + } + } + } + } + + // the pointers are now set. + m_fGraphPointersSet = TRUE; + return TRUE; +} + +//========================================================= +// CGraph - CheckNODFile - this function checks the date of +// the BSP file that was just loaded and the date of the a +// ssociated .NOD file. If the NOD file is not present, or +// is older than the BSP file, we rebuild it. +// +// returns FALSE if the .NOD file doesn't qualify and needs +// to be rebuilt. +// +// !!!BUGBUG - the file times we get back are 20 hours ahead! +// since this happens consistantly, we can still correctly +// determine which of the 2 files is newer. This needs fixed, +// though. ( I now suspect that we are getting GMT back from +// these functions and must compensate for local time ) (sjb) +//========================================================= +int CGraph :: CheckNODFile ( char *szMapName ) +{ + int retValue; + + char szBspFilename[MAX_PATH]; + char szGraphFilename[MAX_PATH]; + + + strcpy ( szBspFilename, "maps/" ); + strcat ( szBspFilename, szMapName ); + strcat ( szBspFilename, ".bsp" ); + + strcpy ( szGraphFilename, "maps/graphs/" ); + strcat ( szGraphFilename, szMapName ); + strcat ( szGraphFilename, ".nod" ); + + retValue = TRUE; + + int iCompare; + if (COMPARE_FILE_TIME(szBspFilename, szGraphFilename, &iCompare)) + { + if ( iCompare > 0 ) + {// BSP file is newer. + ALERT ( at_aiconsole, ".NOD File will be updated\n\n" ); + retValue = FALSE; + } + } + else + { + retValue = FALSE; + } + + return retValue; +} + +#define ENTRY_STATE_EMPTY -1 + +struct tagNodePair +{ + short iSrc; + short iDest; +}; + +void CGraph::HashInsert(int iSrcNode, int iDestNode, int iKey) +{ + struct tagNodePair np; + + np.iSrc = iSrcNode; + np.iDest = iDestNode; + + word usHash; + CRC_INIT( &usHash ); + CRC_PROCESS_BUFFER( &usHash, &np, sizeof( np )); + usHash = CRC_FINAL( usHash ); + + int di = m_HashPrimes[usHash & 15]; + int i = (usHash>>4) % m_nHashLinks; + + while( m_pHashLinks[i] != ENTRY_STATE_EMPTY ) + { + i += di; + if( i >= m_nHashLinks ) + i -= m_nHashLinks; + } + m_pHashLinks[i] = iKey; +} + +void CGraph::HashSearch( int iSrcNode, int iDestNode, int &iKey ) +{ + struct tagNodePair np; + + np.iSrc = iSrcNode; + np.iDest = iDestNode; + + word usHash; + CRC_INIT( &usHash ); + CRC_PROCESS_BUFFER( &usHash, &np, sizeof( np )); + usHash = CRC_FINAL( usHash ); + + int di = m_HashPrimes[usHash & 15]; + int i = (usHash >> 4) % m_nHashLinks; + + while( m_pHashLinks[i] != ENTRY_STATE_EMPTY ) + { + CLink &link = Link(m_pHashLinks[i]); + if( iSrcNode == link.m_iSrcNode && iDestNode == link.m_iDestNode ) + { + break; + } + else + { + i += di; + if( i >= m_nHashLinks ) i -= m_nHashLinks; + } + } + iKey = m_pHashLinks[i]; +} + +#define NUMBER_OF_PRIMES 177 + +int Primes[NUMBER_OF_PRIMES] = +{ 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, +71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, +157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, +241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, +347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, +439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, +547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, +643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, +751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, +859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, +977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 0 }; + +void CGraph::HashChoosePrimes(int TableSize) +{ + int LargestPrime = TableSize/2; + if (LargestPrime > Primes[NUMBER_OF_PRIMES-2]) + { + LargestPrime = Primes[NUMBER_OF_PRIMES-2]; + } + int Spacing = LargestPrime/16; + + // Pick a set primes that are evenly spaced from (0 to LargestPrime) + // We divide this interval into 16 equal sized zones. We want to find + // one prime number that best represents that zone. + // + int iZone = 0, iPrime = 0; + for (iZone = 1, iPrime = 0; iPrime < 16; iZone += Spacing) + { + // Search for a prime number that is less than the target zone + // number given by iZone. + // + int Lower = Primes[0]; + for (int jPrime = 0; Primes[jPrime] != 0; jPrime++) + { + if (jPrime != 0 && TableSize % Primes[jPrime] == 0) continue; + int Upper = Primes[jPrime]; + if (Lower <= iZone && iZone <= Upper) + { + // Choose the closest lower prime number. + // + if (iZone - Lower <= Upper - iZone) + { + m_HashPrimes[iPrime++] = Lower; + } + else + { + m_HashPrimes[iPrime++] = Upper; + } + break; + } + Lower = Upper; + } + } + + // Alternate negative and positive numbers + // + for (iPrime = 0; iPrime < 16; iPrime += 2) + { + m_HashPrimes[iPrime] = TableSize-m_HashPrimes[iPrime]; + } + + // Shuffle the set of primes to reduce correlation with bits in + // hash key. + // + for (iPrime = 0; iPrime < 16-1; iPrime++) + { + int Pick = RANDOM_LONG(0, 15-iPrime); + int Temp = m_HashPrimes[Pick]; + m_HashPrimes[Pick] = m_HashPrimes[15-iPrime]; + m_HashPrimes[15-iPrime] = Temp; + } +} + +// Renumber nodes so that nodes that link together are together. +// +#define UNNUMBERED_NODE -1 +void CGraph::SortNodes(void) +{ + // We are using m_iPreviousNode to be the new node number. + // After assigning new node numbers to everything, we move + // things and patchup the links. + // + int iNodeCnt = 0; + m_pNodes[0].m_iPreviousNode = iNodeCnt++; + int i = 0; + for (i = 1; i < m_cNodes; i++) + { + m_pNodes[i].m_iPreviousNode = UNNUMBERED_NODE; + } + + for (i = 0; i < m_cNodes; i++) + { + // Run through all of this node's neighbors + // + for (int j = 0 ; j < m_pNodes[i].m_cNumLinks; j++ ) + { + int iDestNode = INodeLink(i, j); + if (m_pNodes[iDestNode].m_iPreviousNode == UNNUMBERED_NODE) + { + m_pNodes[iDestNode].m_iPreviousNode = iNodeCnt++; + } + } + } + + // Assign remaining node numbers to unlinked nodes. + // + for (i = 0; i < m_cNodes; i++) + { + if (m_pNodes[i].m_iPreviousNode == UNNUMBERED_NODE) + { + m_pNodes[i].m_iPreviousNode = iNodeCnt++; + } + } + + // Alter links to reflect new node numbers. + // + for (i = 0; i < m_cLinks; i++) + { + m_pLinkPool[i].m_iSrcNode = m_pNodes[m_pLinkPool[i].m_iSrcNode].m_iPreviousNode; + m_pLinkPool[i].m_iDestNode = m_pNodes[m_pLinkPool[i].m_iDestNode].m_iPreviousNode; + } + + // Rearrange nodes to reflect new node numbering. + // + for (i = 0; i < m_cNodes; i++) + { + while (m_pNodes[i].m_iPreviousNode != i) + { + // Move current node off to where it should be, and bring + // that other node back into the current slot. + // + int iDestNode = m_pNodes[i].m_iPreviousNode; + CNode TempNode = m_pNodes[iDestNode]; + m_pNodes[iDestNode] = m_pNodes[i]; + m_pNodes[i] = TempNode; + } + } +} + +void CGraph::BuildLinkLookups(void) +{ + m_nHashLinks = 3*m_cLinks/2 + 3; + + HashChoosePrimes(m_nHashLinks); + m_pHashLinks = (short *)CALLOC(sizeof(short), m_nHashLinks); + if (!m_pHashLinks) + { + ALERT(at_aiconsole, "Couldn't allocated Link Lookup Table.\n"); + return; + } + int i = 0; + for (i = 0; i < m_nHashLinks; i++) + { + m_pHashLinks[i] = ENTRY_STATE_EMPTY; + } + + for (i = 0; i < m_cLinks; i++) + { + CLink &link = Link(i); + HashInsert(link.m_iSrcNode, link.m_iDestNode, i); + } +#if 0 + for (i = 0; i < m_cLinks; i++) + { + CLink &link = Link(i); + int iKey; + HashSearch(link.m_iSrcNode, link.m_iDestNode, iKey); + if (iKey != i) + { + ALERT(at_aiconsole, "HashLinks don't match (%d versus %d)\n", i, iKey); + } + } +#endif +} + +void CGraph::BuildRegionTables(void) +{ + if( m_di ) FREE( m_di ); + + // Go ahead and setup for range searching the nodes for FindNearestNodes + // + m_di = (DIST_INFO *)CALLOC(sizeof(DIST_INFO), m_cNodes); + if (!m_di) + { + ALERT(at_aiconsole, "Couldn't allocated node ordering array.\n"); + return; + } + + // Calculate regions for all the nodes. + // + // + int i = 0; + for (i = 0; i < 3; i++) + { + m_RegionMin[i] = 999999999.0; // just a big number out there; + m_RegionMax[i] = -999999999.0; // just a big number out there; + } + for (i = 0; i < m_cNodes; i++) + { + if (m_pNodes[i].m_vecOrigin.x < m_RegionMin[0]) + m_RegionMin[0] = m_pNodes[i].m_vecOrigin.x; + if (m_pNodes[i].m_vecOrigin.y < m_RegionMin[1]) + m_RegionMin[1] = m_pNodes[i].m_vecOrigin.y; + if (m_pNodes[i].m_vecOrigin.z < m_RegionMin[2]) + m_RegionMin[2] = m_pNodes[i].m_vecOrigin.z; + + if (m_pNodes[i].m_vecOrigin.x > m_RegionMax[0]) + m_RegionMax[0] = m_pNodes[i].m_vecOrigin.x; + if (m_pNodes[i].m_vecOrigin.y > m_RegionMax[1]) + m_RegionMax[1] = m_pNodes[i].m_vecOrigin.y; + if (m_pNodes[i].m_vecOrigin.z > m_RegionMax[2]) + m_RegionMax[2] = m_pNodes[i].m_vecOrigin.z; + } + for (i = 0; i < m_cNodes; i++) + { + m_pNodes[i].m_Region[0] = CALC_RANGE(m_pNodes[i].m_vecOrigin.x, m_RegionMin[0], m_RegionMax[0]); + m_pNodes[i].m_Region[1] = CALC_RANGE(m_pNodes[i].m_vecOrigin.y, m_RegionMin[1], m_RegionMax[1]); + m_pNodes[i].m_Region[2] = CALC_RANGE(m_pNodes[i].m_vecOrigin.z, m_RegionMin[2], m_RegionMax[2]); + } + + for (i = 0; i < 3; i++) + { + int j = 0; + for ( j = 0; j < NUM_RANGES; j++) + { + m_RangeStart[i][j] = 255; + m_RangeEnd[i][j] = 0; + } + for (j = 0; j < m_cNodes; j++) + { + m_di[j].m_SortedBy[i] = j; + } + + for (j = 0; j < m_cNodes - 1; j++) + { + int jNode = m_di[j].m_SortedBy[i]; + int jCodeX = m_pNodes[jNode].m_Region[0]; + int jCodeY = m_pNodes[jNode].m_Region[1]; + int jCodeZ = m_pNodes[jNode].m_Region[2]; + int jCode; + switch (i) + { + case 0: + jCode = (jCodeX << 16) + (jCodeY << 8) + jCodeZ; + break; + case 1: + jCode = (jCodeY << 16) + (jCodeZ << 8) + jCodeX; + break; + case 2: + jCode = (jCodeZ << 16) + (jCodeX << 8) + jCodeY; + break; + } + + for (int k = j+1; k < m_cNodes; k++) + { + int kNode = m_di[k].m_SortedBy[i]; + int kCodeX = m_pNodes[kNode].m_Region[0]; + int kCodeY = m_pNodes[kNode].m_Region[1]; + int kCodeZ = m_pNodes[kNode].m_Region[2]; + int kCode; + switch (i) + { + case 0: + kCode = (kCodeX << 16) + (kCodeY << 8) + kCodeZ; + break; + case 1: + kCode = (kCodeY << 16) + (kCodeZ << 8) + kCodeX; + break; + case 2: + kCode = (kCodeZ << 16) + (kCodeX << 8) + kCodeY; + break; + } + + if (kCode < jCode) + { + // Swap j and k entries. + // + int Tmp = m_di[j].m_SortedBy[i]; + m_di[j].m_SortedBy[i] = m_di[k].m_SortedBy[i]; + m_di[k].m_SortedBy[i] = Tmp; + } + } + } + } + + // Generate lookup tables. + // + for (i = 0; i < m_cNodes; i++) + { + int CodeX = m_pNodes[m_di[i].m_SortedBy[0]].m_Region[0]; + int CodeY = m_pNodes[m_di[i].m_SortedBy[1]].m_Region[1]; + int CodeZ = m_pNodes[m_di[i].m_SortedBy[2]].m_Region[2]; + + if (i < m_RangeStart[0][CodeX]) + { + m_RangeStart[0][CodeX] = i; + } + if (i < m_RangeStart[1][CodeY]) + { + m_RangeStart[1][CodeY] = i; + } + if (i < m_RangeStart[2][CodeZ]) + { + m_RangeStart[2][CodeZ] = i; + } + if (m_RangeEnd[0][CodeX] < i) + { + m_RangeEnd[0][CodeX] = i; + } + if (m_RangeEnd[1][CodeY] < i) + { + m_RangeEnd[1][CodeY] = i; + } + if (m_RangeEnd[2][CodeZ] < i) + { + m_RangeEnd[2][CodeZ] = i; + } + } + + // Initialize the cache. + // + memset(m_Cache, 0, sizeof(m_Cache)); +} + +void CGraph :: ComputeStaticRoutingTables( void ) +{ + int nRoutes = m_cNodes*m_cNodes; +#define FROM_TO(x,y) ((x)*m_cNodes+(y)) + short *Routes = new short[nRoutes]; + + int *pMyPath = new int[m_cNodes]; + unsigned short *BestNextNodes = new unsigned short[m_cNodes]; + char *pRoute = new char[m_cNodes*2]; + + + if (Routes && pMyPath && BestNextNodes && pRoute) + { + int nTotalCompressedSize = 0; + for (int iHull = 0; iHull < MAX_NODE_HULLS; iHull++) + { + for (int iCap = 0; iCap < 2; iCap++) + { + int iCapMask; + switch (iCap) + { + case 0: + iCapMask = 0; + break; + + case 1: + iCapMask = bits_CAP_OPEN_DOORS | bits_CAP_AUTO_DOORS | bits_CAP_USE; + break; + } + + + // Initialize Routing table to uncalculated. + // + int iFrom = 0; + for (iFrom = 0; iFrom < m_cNodes; iFrom++) + { + for (int iTo = 0; iTo < m_cNodes; iTo++) + { + Routes[FROM_TO(iFrom, iTo)] = -1; + } + } + + for (iFrom = 0; iFrom < m_cNodes; iFrom++) + { + for (int iTo = m_cNodes-1; iTo >= 0; iTo--) + { + if (Routes[FROM_TO(iFrom, iTo)] != -1) continue; + + int cPathSize = FindShortestPath(pMyPath, iFrom, iTo, iHull, iCapMask); + + // Use the computed path to update the routing table. + // + if (cPathSize > 1) + { + for (int iNode = 0; iNode < cPathSize-1; iNode++) + { + int iStart = pMyPath[iNode]; + int iNext = pMyPath[iNode+1]; + for (int iNode1 = iNode+1; iNode1 < cPathSize; iNode1++) + { + int iEnd = pMyPath[iNode1]; + Routes[FROM_TO(iStart, iEnd)] = iNext; + } + } +#if 0 + // Well, at first glance, this should work, but actually it's safer + // to be told explictly that you can take a series of node in a + // particular direction. Some links don't appear to have links in + // the opposite direction. + // + for (iNode = cPathSize-1; iNode >= 1; iNode--) + { + int iStart = pMyPath[iNode]; + int iNext = pMyPath[iNode-1]; + for (int iNode1 = iNode-1; iNode1 >= 0; iNode1--) + { + int iEnd = pMyPath[iNode1]; + Routes[FROM_TO(iStart, iEnd)] = iNext; + } + } +#endif + } + else + { + Routes[FROM_TO(iFrom, iTo)] = iFrom; + Routes[FROM_TO(iTo, iFrom)] = iTo; + } + } + } + + for (iFrom = 0; iFrom < m_cNodes; iFrom++) + { + for (int iTo = 0; iTo < m_cNodes; iTo++) + { + BestNextNodes[iTo] = Routes[FROM_TO(iFrom, iTo)]; + } + + // Compress this node's routing table. + // + int iLastNode = 9999999; // just really big. + int cSequence = 0; + int cRepeats = 0; + int CompressedSize = 0; + char *p = pRoute; + for (int i = 0; i < m_cNodes; i++) + { + BOOL CanRepeat = ((BestNextNodes[i] == iLastNode) && cRepeats < 127); + BOOL CanSequence = (BestNextNodes[i] == i && cSequence < 128); + + if (cRepeats) + { + if (CanRepeat) + { + cRepeats++; + } + else + { + // Emit the repeat phrase. + // + CompressedSize += 2; // (count-1, iLastNode-i) + *p++ = cRepeats - 1; + int a = iLastNode - iFrom; + int b = iLastNode - iFrom + m_cNodes; + int c = iLastNode - iFrom - m_cNodes; + if (-128 <= a && a <= 127) + { + *p++ = a; + } + else if (-128 <= b && b <= 127) + { + *p++ = b; + } + else if (-128 <= c && c <= 127) + { + *p++ = c; + } + else + { + ALERT( at_aiconsole, "Nodes need sorting (%d,%d)!\n", iLastNode, iFrom); + } + cRepeats = 0; + + if (CanSequence) + { + // Start a sequence. + // + cSequence++; + } + else + { + // Start another repeat. + // + cRepeats++; + } + } + } + else if (cSequence) + { + if (CanSequence) + { + cSequence++; + } + else + { + // It may be advantageous to combine + // a single-entry sequence phrase with the + // next repeat phrase. + // + if (cSequence == 1 && CanRepeat) + { + // Combine with repeat phrase. + // + cRepeats = 2; + cSequence = 0; + } + else + { + // Emit the sequence phrase. + // + CompressedSize += 1; // (-count) + *p++ = -cSequence; + cSequence = 0; + + // Start a repeat sequence. + // + cRepeats++; + } + } + } + else + { + if (CanSequence) + { + // Start a sequence phrase. + // + cSequence++; + } + else + { + // Start a repeat sequence. + // + cRepeats++; + } + } + iLastNode = BestNextNodes[i]; + } + if (cRepeats) + { + // Emit the repeat phrase. + // + CompressedSize += 2; + *p++ = cRepeats - 1; +#if 0 + iLastNode = iFrom + *pRoute; + if (iLastNode >= m_cNodes) iLastNode -= m_cNodes; + else if (iLastNode < 0) iLastNode += m_cNodes; +#endif + int a = iLastNode - iFrom; + int b = iLastNode - iFrom + m_cNodes; + int c = iLastNode - iFrom - m_cNodes; + if (-128 <= a && a <= 127) + { + *p++ = a; + } + else if (-128 <= b && b <= 127) + { + *p++ = b; + } + else if (-128 <= c && c <= 127) + { + *p++ = c; + } + else + { + ALERT( at_aiconsole, "Nodes need sorting (%d,%d)!\n", iLastNode, iFrom); + } + } + if (cSequence) + { + // Emit the Sequence phrase. + // + CompressedSize += 1; + *p++ = -cSequence; + } + + // Go find a place to store this thing and point to it. + // + int nRoute = p - pRoute; + if (m_pRouteInfo) + { + int i = 0; + for (i = 0; i < m_nRouteInfo - nRoute; i++) + { + if (memcmp(m_pRouteInfo + i, pRoute, nRoute) == 0) + { + break; + } + } + if (i < m_nRouteInfo - nRoute) + { + m_pNodes[ iFrom ].m_pNextBestNode[iHull][iCap] = i; + } + else + { + char *Tmp = (char *)CALLOC(sizeof(char), (m_nRouteInfo + nRoute)); + memcpy(Tmp, m_pRouteInfo, m_nRouteInfo); + FREE( m_pRouteInfo ); + m_pRouteInfo = Tmp; + memcpy(m_pRouteInfo + m_nRouteInfo, pRoute, nRoute); + m_pNodes[ iFrom ].m_pNextBestNode[iHull][iCap] = m_nRouteInfo; + m_nRouteInfo += nRoute; + nTotalCompressedSize += CompressedSize; + } + } + else + { + m_nRouteInfo = nRoute; + m_pRouteInfo = (char *)CALLOC(sizeof(char), nRoute); + memcpy(m_pRouteInfo, pRoute, nRoute); + m_pNodes[ iFrom ].m_pNextBestNode[iHull][iCap] = 0; + nTotalCompressedSize += CompressedSize; + } + } + } + } + ALERT( at_aiconsole, "Size of Routes = %d\n", nTotalCompressedSize); + } + if (Routes) delete Routes; + if (BestNextNodes) delete BestNextNodes; + if (pRoute) delete pRoute; + if (pMyPath) delete pMyPath; + Routes = 0; + BestNextNodes = 0; + pRoute = 0; + pMyPath = 0; + +#if 0 + TestRoutingTables(); +#endif + m_fRoutingComplete = TRUE; +} + +// Test those routing tables. Doesn't really work, yet. +// +void CGraph :: TestRoutingTables( void ) +{ + int *pMyPath = new int[m_cNodes]; + int *pMyPath2 = new int[m_cNodes]; + if (pMyPath && pMyPath2) + { + for (int iHull = 0; iHull < MAX_NODE_HULLS; iHull++) + { + for (int iCap = 0; iCap < 2; iCap++) + { + int iCapMask; + switch (iCap) + { + case 0: + iCapMask = 0; + break; + + case 1: + iCapMask = bits_CAP_OPEN_DOORS | bits_CAP_AUTO_DOORS | bits_CAP_USE; + break; + } + + for (int iFrom = 0; iFrom < m_cNodes; iFrom++) + { + for (int iTo = 0; iTo < m_cNodes; iTo++) + { + m_fRoutingComplete = FALSE; + int cPathSize1 = FindShortestPath(pMyPath, iFrom, iTo, iHull, iCapMask); + m_fRoutingComplete = TRUE; + int cPathSize2 = FindShortestPath(pMyPath2, iFrom, iTo, iHull, iCapMask); + + // Unless we can look at the entire path, we can verify that it's correct. + // + if (cPathSize2 == MAX_PATH_SIZE) continue; + + // Compare distances. + // +#if 1 + float flDistance1 = 0.0; + int i = 0; + for (i = 0; i < cPathSize1-1; i++) + { + // Find the link from pMyPath[i] to pMyPath[i+1] + // + if (pMyPath[i] == pMyPath[i+1]) continue; + int iVisitNode; + BOOL bFound = FALSE; + for (int iLink = 0; iLink < m_pNodes[pMyPath[i]].m_cNumLinks; iLink++) + { + iVisitNode = INodeLink ( pMyPath[i], iLink ); + if (iVisitNode == pMyPath[i+1]) + { + flDistance1 += m_pLinkPool[ m_pNodes[ pMyPath[i] ].m_iFirstLink + iLink].m_flWeight; + bFound = TRUE; + break; + } + } + if (!bFound) + { + ALERT(at_aiconsole, "No link.\n"); + } + } + + float flDistance2 = 0.0; + for (i = 0; i < cPathSize2-1; i++) + { + // Find the link from pMyPath2[i] to pMyPath2[i+1] + // + if (pMyPath2[i] == pMyPath2[i+1]) continue; + int iVisitNode; + BOOL bFound = FALSE; + for (int iLink = 0; iLink < m_pNodes[pMyPath2[i]].m_cNumLinks; iLink++) + { + iVisitNode = INodeLink ( pMyPath2[i], iLink ); + if (iVisitNode == pMyPath2[i+1]) + { + flDistance2 += m_pLinkPool[ m_pNodes[ pMyPath2[i] ].m_iFirstLink + iLink].m_flWeight; + bFound = TRUE; + break; + } + } + if (!bFound) + { + ALERT(at_aiconsole, "No link.\n"); + } + } + if (fabs(flDistance1 - flDistance2) > 0.10) + { +#else + if (cPathSize1 != cPathSize2 || memcmp(pMyPath, pMyPath2, sizeof(int)*cPathSize1) != 0) + { +#endif + ALERT(at_aiconsole, "Routing is inconsistent!!!\n"); + ALERT(at_aiconsole, "(%d to %d |%d/%d)1:", iFrom, iTo, iHull, iCap); + int i = 0; + for (i = 0; i < cPathSize1; i++) + { + ALERT(at_aiconsole, "%d ", pMyPath[i]); + } + ALERT(at_aiconsole, "\n(%d to %d |%d/%d)2:", iFrom, iTo, iHull, iCap); + for (i = 0; i < cPathSize2; i++) + { + ALERT(at_aiconsole, "%d ", pMyPath2[i]); + } + ALERT(at_aiconsole, "\n"); + m_fRoutingComplete = FALSE; + cPathSize1 = FindShortestPath(pMyPath, iFrom, iTo, iHull, iCapMask); + m_fRoutingComplete = TRUE; + cPathSize2 = FindShortestPath(pMyPath2, iFrom, iTo, iHull, iCapMask); + goto EnoughSaid; + } + } + } + } + } + } + +EnoughSaid: + + if (pMyPath) delete pMyPath; + if (pMyPath2) delete pMyPath2; + pMyPath = 0; + pMyPath2 = 0; +} + + + + + + + + + +//========================================================= +// CNodeViewer - Draws a graph of the shorted path from all nodes +// to current location (typically the player). It then draws +// as many connects as it can per frame, trying not to overflow the buffer +//========================================================= +class CNodeViewer : public CBaseEntity +{ +public: + void Spawn( void ); + + int m_iBaseNode; + int m_iDraw; + int m_nVisited; + int m_aFrom[128]; + int m_aTo[128]; + int m_iHull; + int m_afNodeType; + Vector m_vecColor; + + void FindNodeConnections( int iNode ); + void AddNode( int iFrom, int iTo ); + void EXPORT DrawThink( void ); + +}; +LINK_ENTITY_TO_CLASS( node_viewer, CNodeViewer ); +LINK_ENTITY_TO_CLASS( node_viewer_human, CNodeViewer ); +LINK_ENTITY_TO_CLASS( node_viewer_fly, CNodeViewer ); +LINK_ENTITY_TO_CLASS( node_viewer_large, CNodeViewer ); + +void CNodeViewer::Spawn( ) +{ + if ( !WorldGraph.m_fGraphPresent || !WorldGraph.m_fGraphPointersSet ) + {// protect us in the case that the node graph isn't available or built + ALERT ( at_debug, "Graph not ready!\n" ); + UTIL_Remove( this ); + return; + } + + + if (FClassnameIs( pev, "node_viewer_fly")) + { + m_iHull = NODE_FLY_HULL; + m_afNodeType = bits_NODE_AIR; + m_vecColor = Vector( 160, 100, 255 ); + } + else if (FClassnameIs( pev, "node_viewer_large")) + { + m_iHull = NODE_LARGE_HULL; + m_afNodeType = bits_NODE_LAND | bits_NODE_WATER; + m_vecColor = Vector( 100, 255, 160 ); + } + else + { + m_iHull = NODE_HUMAN_HULL; + m_afNodeType = bits_NODE_LAND | bits_NODE_WATER; + m_vecColor = Vector( 255, 160, 100 ); + } + + + m_iBaseNode = WorldGraph.FindNearestNode ( pev->origin, m_afNodeType ); + + if ( m_iBaseNode < 0 ) + { + ALERT( at_debug, "No nearby node\n" ); + return; + } + + m_nVisited = 0; + + ALERT( at_aiconsole, "basenode %d\n", m_iBaseNode ); + + if (WorldGraph.m_cNodes < 128) + { + for (int i = 0; i < WorldGraph.m_cNodes; i++) + { + AddNode( i, WorldGraph.NextNodeInRoute( i, m_iBaseNode, m_iHull, 0 )); + } + } + else + { + // do a depth traversal + FindNodeConnections( m_iBaseNode ); + + int start = 0; + int end; + do { + end = m_nVisited; + // ALERT( at_console, "%d :", m_nVisited ); + for (end = m_nVisited; start < end; start++) + { + FindNodeConnections( m_aFrom[start] ); + FindNodeConnections( m_aTo[start] ); + } + } while (end != m_nVisited); + } + + ALERT( at_aiconsole, "%d nodes\n", m_nVisited ); + + m_iDraw = 0; + SetThink(&CNodeViewer:: DrawThink ); + SetNextThink( 0 ); +} + + +void CNodeViewer :: FindNodeConnections ( int iNode ) +{ + AddNode( iNode, WorldGraph.NextNodeInRoute( iNode, m_iBaseNode, m_iHull, 0 )); + for ( int i = 0 ; i < WorldGraph.m_pNodes[ iNode ].m_cNumLinks ; i++ ) + { + CLink *pToLink = &WorldGraph.NodeLink( iNode, i); + AddNode( pToLink->m_iDestNode, WorldGraph.NextNodeInRoute( pToLink->m_iDestNode, m_iBaseNode, m_iHull, 0 )); + } +} + +void CNodeViewer::AddNode( int iFrom, int iTo ) +{ + if (m_nVisited >= 128) + { + return; + } + else + { + if (iFrom == iTo) + return; + + for (int i = 0; i < m_nVisited; i++) + { + if (m_aFrom[i] == iFrom && m_aTo[i] == iTo) + return; + if (m_aFrom[i] == iTo && m_aTo[i] == iFrom) + return; + } + m_aFrom[m_nVisited] = iFrom; + m_aTo[m_nVisited] = iTo; + m_nVisited++; + } +} + + +void CNodeViewer :: DrawThink( void ) +{ + SetNextThink( 0 ); + + for (int i = 0; i < 10; i++) + { + if (m_iDraw == m_nVisited) + { + UTIL_Remove( this ); + return; + } + + extern short g_sModelIndexLaser; + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_BEAMPOINTS ); + WRITE_COORD( WorldGraph.m_pNodes[ m_aFrom[m_iDraw] ].m_vecOrigin.x ); + WRITE_COORD( WorldGraph.m_pNodes[ m_aFrom[m_iDraw] ].m_vecOrigin.y ); + WRITE_COORD( WorldGraph.m_pNodes[ m_aFrom[m_iDraw] ].m_vecOrigin.z + NODE_HEIGHT ); + + WRITE_COORD( WorldGraph.m_pNodes[ m_aTo[m_iDraw] ].m_vecOrigin.x ); + WRITE_COORD( WorldGraph.m_pNodes[ m_aTo[m_iDraw] ].m_vecOrigin.y ); + WRITE_COORD( WorldGraph.m_pNodes[ m_aTo[m_iDraw] ].m_vecOrigin.z + NODE_HEIGHT ); + WRITE_SHORT( g_sModelIndexLaser ); + WRITE_BYTE( 0 ); // framerate + WRITE_BYTE( 0 ); // framerate + WRITE_BYTE( 250 ); // life + WRITE_BYTE( 40 ); // width + WRITE_BYTE( 0 ); // noise + WRITE_BYTE( m_vecColor.x ); // r, g, b + WRITE_BYTE( m_vecColor.y ); // r, g, b + WRITE_BYTE( m_vecColor.z ); // r, g, b + WRITE_BYTE( 128 ); // brightness + WRITE_BYTE( 0 ); // speed + MESSAGE_END(); + + m_iDraw++; + } +} + + diff --git a/server/monsters/nodes.h b/server/monsters/nodes.h new file mode 100644 index 00000000..269fe420 --- /dev/null +++ b/server/monsters/nodes.h @@ -0,0 +1,374 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// nodes.h +//========================================================= + +//========================================================= +// DEFINE +//========================================================= +#define MAX_STACK_NODES 100 +#define NO_NODE -1 +#define MAX_NODE_HULLS 4 + +#define bits_NODE_LAND ( 1 << 0 ) // Land node, so nudge if necessary. +#define bits_NODE_AIR ( 1 << 1 ) // Air node, don't nudge. +#define bits_NODE_WATER ( 1 << 2 ) // Water node, don't nudge. +#define bits_NODE_GROUP_REALM (bits_NODE_LAND | bits_NODE_AIR | bits_NODE_WATER) + +//========================================================= +// Instance of a node. +//========================================================= +class CNode +{ +public: + Vector m_vecOrigin;// location of this node in space + Vector m_vecOriginPeek; // location of this node (LAND nodes are NODE_HEIGHT higher). + BYTE m_Region[3]; // Which of 256 regions do each of the coordinate belong? + int m_afNodeInfo;// bits that tell us more about this location + + int m_cNumLinks; // how many links this node has + int m_iFirstLink;// index of this node's first link in the link pool. + + // Where to start looking in the compressed routing table (offset into m_pRouteInfo). + // (4 hull sizes -- smallest to largest + fly/swim), and secondly, door capability. + // + int m_pNextBestNode[MAX_NODE_HULLS][2]; + + // Used in finding the shortest path. m_fClosestSoFar is -1 if not visited. + // Then it is the distance to the source. If another path uses this node + // and has a closer distance, then m_iPreviousNode is also updated. + // + float m_flClosestSoFar; // Used in finding the shortest path. + int m_iPreviousNode; + + short m_sHintType;// there is something interesting in the world at this node's position + short m_sHintActivity;// there is something interesting in the world at this node's position + float m_flHintYaw;// monster on this node should face this yaw to face the hint. +}; + +//========================================================= +// CLink - A link between 2 nodes +//========================================================= +#define bits_LINK_SMALL_HULL ( 1 << 0 )// headcrab box can fit through this connection +#define bits_LINK_HUMAN_HULL ( 1 << 1 )// player box can fit through this connection +#define bits_LINK_LARGE_HULL ( 1 << 2 )// big box can fit through this connection +#define bits_LINK_FLY_HULL ( 1 << 3 )// a flying big box can fit through this connection +#define bits_LINK_DISABLED ( 1 << 4 )// link is not valid when the set + +#define NODE_SMALL_HULL 0 +#define NODE_HUMAN_HULL 1 +#define NODE_LARGE_HULL 2 +#define NODE_FLY_HULL 3 + +class CLink +{ +public: + int m_iSrcNode;// the node that 'owns' this link ( keeps us from having to make reverse lookups ) + int m_iDestNode;// the node on the other end of the link. + + entvars_t *m_pLinkEnt;// the entity that blocks this connection (doors, etc) + + // m_szLinkEntModelname is not necessarily NULL terminated (so we can store it in a more alignment-friendly 4 bytes) + char m_szLinkEntModelname[ 4 ];// the unique name of the brush model that blocks the connection (this is kept for save/restore) + + int m_afLinkInfo;// information about this link + float m_flWeight;// length of the link line segment +}; + + +typedef struct +{ + int m_SortedBy[3]; + int m_CheckedEvent; +} DIST_INFO; + +typedef struct +{ + Vector v; + short n; // Nearest node or -1 if no node found. +} CACHE_ENTRY; + +//========================================================= +// CGraph +//========================================================= +#define GRAPH_VERSION (int)16// !!!increment this whever graph/node/link classes change, to obsolesce older disk files. +class CGraph +{ +public: + +// the graph has two flags, and should not be accessed unless both flags are TRUE! + BOOL m_fGraphPresent;// is the graph in memory? + BOOL m_fGraphPointersSet;// are the entity pointers for the graph all set? + BOOL m_fRoutingComplete; // are the optimal routes computed, yet? + + CNode *m_pNodes;// pointer to the memory block that contains all node info + CLink *m_pLinkPool;// big list of all node connections + char *m_pRouteInfo; // compressed routing information the nodes use. + + int m_cNodes;// total number of nodes + int m_cLinks;// total number of links + int m_nRouteInfo; // size of m_pRouteInfo in bytes. + + // Tables for making nearest node lookup faster. SortedBy provided nodes in a + // order of a particular coordinate. Instead of doing a binary search, RangeStart + // and RangeEnd let you get to the part of SortedBy that you are interested in. + // + // Once you have a point of interest, the only way you'll find a closer point is + // if at least one of the coordinates is closer than the ones you have now. So we + // search each range. After the search is exhausted, we know we have the closest + // node. + // +#define CACHE_SIZE 128 +#define NUM_RANGES 256 + DIST_INFO *m_di; // This is m_cNodes long, but the entries don't correspond to CNode entries. + int m_RangeStart[3][NUM_RANGES]; + int m_RangeEnd[3][NUM_RANGES]; + float m_flShortest; + int m_iNearest; + int m_minX, m_minY, m_minZ, m_maxX, m_maxY, m_maxZ; + int m_minBoxX, m_minBoxY, m_minBoxZ, m_maxBoxX, m_maxBoxY, m_maxBoxZ; + int m_CheckedCounter; + float m_RegionMin[3], m_RegionMax[3]; // The range of nodes. + CACHE_ENTRY m_Cache[CACHE_SIZE]; + + + int m_HashPrimes[16]; + short *m_pHashLinks; + int m_nHashLinks; + + + // kinda sleazy. In order to allow variety in active idles for monster groups in a room with more than one node, + // we keep track of the last node we searched from and store it here. Subsequent searches by other monsters will pick + // up where the last search stopped. + int m_iLastActiveIdleSearch; + + // another such system used to track the search for cover nodes, helps greatly with two monsters trying to get to the same node. + int m_iLastCoverSearch; + + // functions to create the graph + int LinkVisibleNodes ( CLink *pLinkPool, FILE *file, int *piBadNode ); + int RejectInlineLinks ( CLink *pLinkPool, FILE *file ); + int FindShortestPath ( int *piPath, int iStart, int iDest, int iHull, int afCapMask); + int FindNearestNode ( const Vector &vecOrigin, CBaseEntity *pEntity ); + int FindNearestNode ( const Vector &vecOrigin, int afNodeTypes ); + //int FindNearestLink ( const Vector &vecTestPoint, int *piNearestLink, BOOL *pfAlongLine ); + float PathLength( int iStart, int iDest, int iHull, int afCapMask ); + int NextNodeInRoute( int iCurrentNode, int iDest, int iHull, int iCap ); + + enum NODEQUERY { NODEGRAPH_DYNAMIC, NODEGRAPH_STATIC }; + // A static query means we're asking about the possiblity of handling this entity at ANY time + // A dynamic query means we're asking about it RIGHT NOW. So we should query the current state + int HandleLinkEnt ( int iNode, entvars_t *pevLinkEnt, int afCapMask, NODEQUERY queryType ); + entvars_t* LinkEntForLink ( CLink *pLink, CNode *pNode ); + void ShowNodeConnections ( int iNode ); + void InitGraph( void ); + int AllocNodes ( void ); + + int CheckNODFile(char *szMapName); + int FLoadGraph(char *szMapName); + int FSaveGraph(char *szMapName); + int FSetGraphPointers(void); + void CheckNode(Vector vecOrigin, int iNode); + + void BuildRegionTables(void); + void ComputeStaticRoutingTables(void); + void TestRoutingTables(void); + + void HashInsert(int iSrcNode, int iDestNode, int iKey); + void HashSearch(int iSrcNode, int iDestNode, int &iKey); + void HashChoosePrimes(int TableSize); + void BuildLinkLookups(void); + + void SortNodes(void); + + int HullIndex( const CBaseEntity *pEntity ); // what hull the monster uses + int NodeType( const CBaseEntity *pEntity ); // what node type the monster uses + inline int CapIndex( int afCapMask ) + { + if (afCapMask & (bits_CAP_OPEN_DOORS | bits_CAP_AUTO_DOORS | bits_CAP_USE)) + return 1; + return 0; + } + + + inline CNode &Node( int i ) + { +#ifdef _DEBUG + if ( !m_pNodes || i < 0 || i > m_cNodes ) + ALERT( at_error, "Bad Node!\n" ); +#endif + return m_pNodes[i]; + } + + inline CLink &Link( int i ) + { +#ifdef _DEBUG + if ( !m_pLinkPool || i < 0 || i > m_cLinks ) + ALERT( at_error, "Bad link!\n" ); +#endif + return m_pLinkPool[i]; + } + + inline CLink &NodeLink( int iNode, int iLink ) + { + return Link( Node( iNode ).m_iFirstLink + iLink ); + } + + inline CLink &NodeLink( const CNode &node, int iLink ) + { + return Link( node.m_iFirstLink + iLink ); + } + + inline int INodeLink ( int iNode, int iLink ) + { + return NodeLink( iNode, iLink ).m_iDestNode; + } + +#if 0 + inline CNode &SourceNode( int iNode, int iLink ) + { + return Node( NodeLink( iNode, iLink ).m_iSrcNode ); + } + + inline CNode &DestNode( int iNode, int iLink ) + { + return Node( NodeLink( iNode, iLink ).m_iDestNode ); + } + + inline CNode *PNodeLink ( int iNode, int iLink ) + { + return &DestNode( iNode, iLink ); + } +#endif +}; + +//========================================================= +// Nodes start out as ents in the level. The node graph +// is built, then these ents are discarded. +//========================================================= +class CNodeEnt : public CBaseEntity +{ + void Spawn( void ); + void KeyValue( KeyValueData *pkvd ); + virtual int ObjectCaps( void ) { return CBaseEntity :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + + short m_sHintType; + short m_sHintActivity; +}; + + +//========================================================= +// CStack - last in, first out. +//========================================================= +class CStack +{ +public: + CStack( void ); + void Push( int value ); + int Pop( void ); + int Top( void ); + int Empty( void ) { return m_level==0; } + int Size( void ) { return m_level; } + void CopyToArray ( int *piArray ); + +private: + int m_stack[ MAX_STACK_NODES ]; + int m_level; +}; + + +//========================================================= +// CQueue - first in, first out. +//========================================================= +class CQueue +{ +public: + + CQueue( void );// constructor + inline int Full ( void ) { return ( m_cSize == MAX_STACK_NODES ); } + inline int Empty ( void ) { return ( m_cSize == 0 ); } + //inline int Tail ( void ) { return ( m_queue[ m_tail ] ); } + inline int Size ( void ) { return ( m_cSize ); } + void Insert( int, float ); + int Remove( float & ); + +private: + int m_cSize; + struct tag_QUEUE_NODE + { + int Id; + float Priority; + } m_queue[ MAX_STACK_NODES ]; + int m_head; + int m_tail; +}; + +//========================================================= +// CQueuePriority - Priority queue (smallest item out first). +// +//========================================================= +class CQueuePriority +{ +public: + + CQueuePriority( void );// constructor + inline int Full ( void ) { return ( m_cSize == MAX_STACK_NODES ); } + inline int Empty ( void ) { return ( m_cSize == 0 ); } + //inline int Tail ( float & ) { return ( m_queue[ m_tail ].Id ); } + inline int Size ( void ) { return ( m_cSize ); } + void Insert( int, float ); + int Remove( float &); + +private: + int m_cSize; + struct tag_HEAP_NODE + { + int Id; + float Priority; + } m_heap[ MAX_STACK_NODES ]; + void Heap_SiftDown(int); + void Heap_SiftUp(void); + +}; + +//========================================================= +// hints - these MUST coincide with the HINTS listed under +// info_node in the FGD file! +//========================================================= +enum +{ + HINT_NONE = 0, + HINT_WORLD_DOOR, + HINT_WORLD_WINDOW, + HINT_WORLD_BUTTON, + HINT_WORLD_MACHINERY, + HINT_WORLD_LEDGE, + HINT_WORLD_LIGHT_SOURCE, + HINT_WORLD_HEAT_SOURCE, + HINT_WORLD_BLINKING_LIGHT, + HINT_WORLD_BRIGHT_COLORS, + HINT_WORLD_HUMAN_BLOOD, + HINT_WORLD_ALIEN_BLOOD, + + HINT_TACTICAL_EXIT = 100, + HINT_TACTICAL_VANTAGE, + HINT_TACTICAL_AMBUSH, + + HINT_STUKA_PERCH = 300, + HINT_STUKA_LANDING, +}; + +extern CGraph WorldGraph; diff --git a/server/monsters/osprey.cpp b/server/monsters/osprey.cpp new file mode 100644 index 00000000..cd297531 --- /dev/null +++ b/server/monsters/osprey.cpp @@ -0,0 +1,822 @@ +/*** +* +* Copyright (c) 1999, 2000 Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "baseweapon.h" +#include "nodes.h" +#include "soundent.h" +#include "basebeams.h" + +typedef struct +{ + int isValid; + EHANDLE hGrunt; + Vector vecOrigin; + Vector vecAngles; +} t_ospreygrunt; + + + +#define SF_WAITFORTRIGGER 0x40 + + +#define MAX_CARRY 24 + +class COsprey : public CBaseMonster +{ +public: + int Save( CSave &save ); + int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + int ObjectCaps( void ) { return CBaseMonster :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + + void Spawn( void ); + void Precache( void ); + int Classify( void ) { return CLASS_MACHINE; }; + int BloodColor( void ) { return DONT_BLEED; } + void Killed( entvars_t *pevAttacker, int iGib ); + + void UpdateGoal( void ); + BOOL HasDead( void ); + void EXPORT FlyThink( void ); + void EXPORT DeployThink( void ); + void Flight( void ); + void EXPORT HitTouch( CBaseEntity *pOther ); + void EXPORT FindAllThink( void ); + void EXPORT HoverThink( void ); + CBaseMonster *MakeGrunt( Vector vecSrc ); + void EXPORT CrashTouch( CBaseEntity *pOther ); + void EXPORT DyingThink( void ); + void EXPORT CommandUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + // int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType ); + void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + void ShowDamage( void ); + + CBaseEntity *m_pGoalEnt; + Vector m_vel1; + Vector m_vel2; + Vector m_pos1; + Vector m_pos2; + Vector m_ang1; + Vector m_ang2; + float m_startTime; + float m_dTime; + + Vector m_velocity; + + float m_flIdealtilt; + float m_flRotortilt; + + float m_flRightHealth; + float m_flLeftHealth; + + int m_iUnits; + EHANDLE m_hGrunt[MAX_CARRY]; + Vector m_vecOrigin[MAX_CARRY]; + EHANDLE m_hRepel[4]; + + int m_iSoundState; + int m_iSpriteTexture; + + int m_iPitch; + + int m_iExplode; + int m_iTailGibs; + int m_iBodyGibs; + int m_iEngineGibs; + + int m_iDoLeftSmokePuff; + int m_iDoRightSmokePuff; +}; + +LINK_ENTITY_TO_CLASS( monster_osprey, COsprey ); + +TYPEDESCRIPTION COsprey::m_SaveData[] = +{ + DEFINE_FIELD( COsprey, m_pGoalEnt, FIELD_CLASSPTR ), + DEFINE_FIELD( COsprey, m_vel1, FIELD_VECTOR ), + DEFINE_FIELD( COsprey, m_vel2, FIELD_VECTOR ), + DEFINE_FIELD( COsprey, m_pos1, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( COsprey, m_pos2, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( COsprey, m_ang1, FIELD_VECTOR ), + DEFINE_FIELD( COsprey, m_ang2, FIELD_VECTOR ), + + DEFINE_FIELD( COsprey, m_startTime, FIELD_TIME ), + DEFINE_FIELD( COsprey, m_dTime, FIELD_FLOAT ), + DEFINE_FIELD( COsprey, m_velocity, FIELD_VECTOR ), + + DEFINE_FIELD( COsprey, m_flIdealtilt, FIELD_FLOAT ), + DEFINE_FIELD( COsprey, m_flRotortilt, FIELD_FLOAT ), + + DEFINE_FIELD( COsprey, m_flRightHealth, FIELD_FLOAT ), + DEFINE_FIELD( COsprey, m_flLeftHealth, FIELD_FLOAT ), + + DEFINE_FIELD( COsprey, m_iUnits, FIELD_INTEGER ), + DEFINE_ARRAY( COsprey, m_hGrunt, FIELD_EHANDLE, MAX_CARRY ), + DEFINE_ARRAY( COsprey, m_vecOrigin, FIELD_POSITION_VECTOR, MAX_CARRY ), + DEFINE_ARRAY( COsprey, m_hRepel, FIELD_EHANDLE, 4 ), + + // DEFINE_FIELD( COsprey, m_iSoundState, FIELD_INTEGER ), + // DEFINE_FIELD( COsprey, m_iSpriteTexture, FIELD_INTEGER ), + // DEFINE_FIELD( COsprey, m_iPitch, FIELD_INTEGER ), + + DEFINE_FIELD( COsprey, m_iDoLeftSmokePuff, FIELD_INTEGER ), + DEFINE_FIELD( COsprey, m_iDoRightSmokePuff, FIELD_INTEGER ), +}; +IMPLEMENT_SAVERESTORE( COsprey, CBaseMonster ); + +void COsprey :: Spawn( void ) +{ + Precache( ); + // motor + pev->movetype = MOVETYPE_FLY; + pev->solid = SOLID_BBOX; + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/osprey.mdl"); + UTIL_SetSize(pev, Vector( -400, -400, -100), Vector(400, 400, 32)); + UTIL_SetOrigin( this, pev->origin ); + + //ALERT(at_console, "Osprey origin %f %f %f\n", pev->origin.x, pev->origin.y, pev->origin.z); + + pev->flags |= FL_MONSTER; + pev->takedamage = DAMAGE_YES; + m_flRightHealth = 200; + m_flLeftHealth = 200; + pev->health = 400; + + pev->speed = 80; //LRC - default speed, in case path corners don't give a speed. + + m_flFieldOfView = 0; // 180 degrees + + pev->sequence = 0; + ResetSequenceInfo( ); + pev->frame = RANDOM_LONG(0,0xFF); + + InitBoneControllers(); + + SetThink(&COsprey :: FindAllThink ); + SetUse(&COsprey :: CommandUse ); + + if (!(pev->spawnflags & SF_WAITFORTRIGGER)) + { + SetNextThink( 1.0 ); + } + + m_pos2 = pev->origin; + m_ang2 = pev->angles; + m_vel2 = pev->velocity; +} + + +void COsprey::Precache( void ) +{ + UTIL_PrecacheEntity( "monster_human_grunt" ); + + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/osprey.mdl"); + PRECACHE_MODEL("models/HVR.mdl"); + + PRECACHE_SOUND("apache/ap_rotor4.wav"); + PRECACHE_SOUND("weapons/mortarhit.wav"); + + m_iSpriteTexture = PRECACHE_MODEL( "sprites/rope.spr" ); + + m_iExplode = PRECACHE_MODEL( "sprites/fexplo.spr" ); + m_iTailGibs = PRECACHE_MODEL( "models/osprey_tailgibs.mdl" ); + m_iBodyGibs = PRECACHE_MODEL( "models/osprey_bodygibs.mdl" ); + m_iEngineGibs = PRECACHE_MODEL( "models/osprey_enginegibs.mdl" ); +} + +void COsprey::CommandUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + SetNextThink( 0.1 ); +} + +void COsprey :: FindAllThink( void ) +{ + CBaseEntity *pEntity = NULL; + + m_iUnits = 0; + while (m_iUnits < MAX_CARRY && (pEntity = UTIL_FindEntityByClassname( pEntity, "monster_human_grunt" )) != NULL) + { + if (pEntity->IsAlive()) + { + m_hGrunt[m_iUnits] = pEntity; + m_vecOrigin[m_iUnits] = pEntity->pev->origin; + m_iUnits++; + } + } + + if (m_iUnits == 0) + { + m_iUnits = 4; //LRC - stop whining, just make the damn grunts... + +// ALERT( at_console, "osprey error: no grunts to resupply\n"); +// UTIL_Remove( this ); +// return; + } + SetThink(&COsprey :: FlyThink ); + SetNextThink( 0.1 ); + m_startTime = gpGlobals->time; +} + + +void COsprey :: DeployThink( void ) +{ + UTIL_MakeAimVectors( pev->angles ); + + Vector vecForward = gpGlobals->v_forward; + Vector vecRight = gpGlobals->v_right; + Vector vecUp = gpGlobals->v_up; + + Vector vecSrc; + + TraceResult tr; + UTIL_TraceLine( pev->origin, pev->origin + Vector( 0, 0, -4096.0), ignore_monsters, ENT(pev), &tr); + CSoundEnt::InsertSound ( bits_SOUND_DANGER, tr.vecEndPos, 400, 0.3 ); + + vecSrc = pev->origin + vecForward * 32 + vecRight * 100 + vecUp * -96; + m_hRepel[0] = MakeGrunt( vecSrc ); + + vecSrc = pev->origin + vecForward * -64 + vecRight * 100 + vecUp * -96; + m_hRepel[1] = MakeGrunt( vecSrc ); + + vecSrc = pev->origin + vecForward * 32 + vecRight * -100 + vecUp * -96; + m_hRepel[2] = MakeGrunt( vecSrc ); + + vecSrc = pev->origin + vecForward * -64 + vecRight * -100 + vecUp * -96; + m_hRepel[3] = MakeGrunt( vecSrc ); + + SetThink(&COsprey :: HoverThink ); + SetNextThink( 0.1 ); +} + + + +BOOL COsprey :: HasDead( ) +{ + for (int i = 0; i < m_iUnits; i++) + { + if (m_hGrunt[i] == NULL || !m_hGrunt[i]->IsAlive()) + { + return TRUE; + } + else + { + m_vecOrigin[i] = m_hGrunt[i]->pev->origin; // send them to where they died + } + } + return FALSE; +} + + +CBaseMonster *COsprey :: MakeGrunt( Vector vecSrc ) +{ + CBaseEntity *pEntity; + CBaseMonster *pGrunt; + + TraceResult tr; + UTIL_TraceLine( vecSrc, vecSrc + Vector( 0, 0, -4096.0), dont_ignore_monsters, ENT(pev), &tr); + if ( tr.pHit && Instance( tr.pHit )->pev->solid != SOLID_BSP) + return NULL; + + for (int i = 0; i < m_iUnits; i++) + { + if (m_hGrunt[i] == NULL || !m_hGrunt[i]->IsAlive()) + { + pEntity = Create( "monster_human_grunt", vecSrc, pev->angles ); + pGrunt = pEntity->MyMonsterPointer( ); + pGrunt->pev->movetype = MOVETYPE_FLY; + pGrunt->pev->velocity = Vector( 0, 0, RANDOM_FLOAT( -196, -128 ) ); + pGrunt->SetActivity( ACT_GLIDE ); + + CBeam *pBeam = CBeam::BeamCreate( "sprites/rope.spr", 10 ); + pBeam->PointEntInit( vecSrc + Vector(0,0,112), pGrunt->entindex() ); + pBeam->SetFlags( BEAM_FSOLID ); + pBeam->SetColor( 255, 255, 255 ); + pBeam->SetThink( Remove ); + pBeam->SetNextThink( -4096.0 * tr.flFraction / pGrunt->pev->velocity.z + 0.5 ); + + // ALERT( at_console, "%d at %.0f %.0f %.0f\n", i, m_vecOrigin[i].x, m_vecOrigin[i].y, m_vecOrigin[i].z ); + pGrunt->m_vecLastPosition = m_vecOrigin[i]; + m_hGrunt[i] = pGrunt; + return pGrunt; + } + } + // ALERT( at_console, "none dead\n"); + return NULL; +} + + +void COsprey :: HoverThink( void ) +{ + int i; + for (i = 0; i < 4; i++) + { + if (m_hRepel[i] != NULL && m_hRepel[i]->pev->health > 0 && !(m_hRepel[i]->pev->flags & FL_ONGROUND)) + { + break; + } + } + + if (i == 4) + { + m_startTime = gpGlobals->time; + SetThink(&COsprey :: FlyThink ); + } + + SetNextThink( 0.1 ); + UTIL_MakeAimVectors( pev->angles ); + ShowDamage( ); +} + + +void COsprey::UpdateGoal( ) +{ + if (m_pGoalEnt) + { + m_pos1 = m_pos2; + m_ang1 = m_ang2; + m_vel1 = m_vel2; + m_pos2 = m_pGoalEnt->pev->origin; + m_ang2 = m_pGoalEnt->pev->angles; + UTIL_MakeAimVectors( Vector( 0, m_ang2.y, 0 ) ); + + //LRC - ugh. we shouldn't require our path corners to specify a speed! + if (m_pGoalEnt->pev->speed) + pev->speed = m_pGoalEnt->pev->speed; + + m_vel2 = gpGlobals->v_forward * pev->speed; //LRC + + m_startTime = m_startTime + m_dTime; + m_dTime = 2.0 * (m_pos1 - m_pos2).Length() / (m_vel1.Length() + pev->speed); + + //ALERT(at_console, "osprey m_dTime = %f / %f + %f\n", (m_pos1 - m_pos2).Length(), m_vel1.Length(), m_pGoalEnt->pev->speed); + + if (m_ang1.y - m_ang2.y < -180) + { + m_ang1.y += 360; + } + else if (m_ang1.y - m_ang2.y > 180) + { + m_ang1.y -= 360; + } + + if (pev->speed < 400) + m_flIdealtilt = 0; + else + m_flIdealtilt = -90; + } + else + { + ALERT( at_debug, "osprey missing target"); + } +} + + +void COsprey::FlyThink( void ) +{ + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + if ( m_pGoalEnt == NULL && !FStringNull(pev->target) )// this monster has a target + { + m_pGoalEnt = UTIL_FindEntityByTargetname( NULL, STRING( pev->target ) ); + UpdateGoal( ); + } + + if (gpGlobals->time > m_startTime + m_dTime) + { + if (m_pGoalEnt->pev->speed == 0) + { + SetThink(&COsprey:: DeployThink ); + } + int loopbreaker = 100; //LRC - don't loop indefinitely! + do { + m_pGoalEnt = UTIL_FindEntityByTargetname( NULL, STRING( m_pGoalEnt->pev->target ) ); + loopbreaker--; //LRC + } while (m_pGoalEnt->pev->speed < 400 && !HasDead() && loopbreaker > 0); + UpdateGoal( ); + } + + Flight( ); + ShowDamage( ); +} + + +void COsprey::Flight( ) +{ + float t = (gpGlobals->time - m_startTime); + float scale = 1.0 / m_dTime; + + float f = UTIL_SplineFraction( t * scale, 1.0 ); + +// ALERT(at_console, "Osprey setorigin m_pos1 %f, m_vel1 %f, m_pos2 %f, m_vel2 %f, m_dTime %f, t %f, f %f\n", m_pos1.x, m_vel1.x, m_pos2.x, m_vel2.x, m_dTime, t, f); + + Vector pos = (m_pos1 + m_vel1 * t) * (1.0 - f) + (m_pos2 - m_vel2 * (m_dTime - t)) * f; + Vector ang = (m_ang1) * (1.0 - f) + (m_ang2) * f; + m_velocity = m_vel1 * (1.0 - f) + m_vel2 * f; + + UTIL_SetOrigin( this, pos ); + pev->angles = ang; + UTIL_MakeAimVectors( pev->angles ); + float flSpeed = DotProduct( gpGlobals->v_forward, m_velocity ); + + // float flSpeed = DotProduct( gpGlobals->v_forward, pev->velocity ); + + float m_flIdealtilt = (160 - flSpeed) / 10.0; + + // ALERT( at_console, "%f %f\n", flSpeed, flIdealtilt ); + if (m_flRotortilt < m_flIdealtilt) + { + m_flRotortilt += 0.5; + if (m_flRotortilt > 0) + m_flRotortilt = 0; + } + if (m_flRotortilt > m_flIdealtilt) + { + m_flRotortilt -= 0.5; + if (m_flRotortilt < -90) + m_flRotortilt = -90; + } + SetBoneController( 0, m_flRotortilt ); + + + if (m_iSoundState == 0) + { + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_rotor4.wav", 1.0, 0.15, 0, 110 ); + // EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_whine1.wav", 0.5, 0.2, 0, 110 ); + + m_iSoundState = SND_CHANGE_PITCH; // hack for going through level transitions + } + else + { + CBaseEntity *pPlayer = NULL; + + pPlayer = UTIL_FindEntityByClassname( NULL, "player" ); + // UNDONE: this needs to send different sounds to every player for multiplayer. + if (pPlayer) + { + float pitch = DotProduct( m_velocity - pPlayer->pev->velocity, (pPlayer->pev->origin - pev->origin).Normalize() ); + + pitch = (int)(100 + pitch / 75.0); + + if (pitch > 250) + pitch = 250; + if (pitch < 50) + pitch = 50; + + if (pitch == 100) + pitch = 101; + + if (pitch != m_iPitch) + { + m_iPitch = pitch; + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_rotor4.wav", 1.0, 0.15, SND_CHANGE_PITCH | SND_CHANGE_VOL, pitch); + // ALERT( at_console, "%.0f\n", pitch ); + } + } + // EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "apache/ap_whine1.wav", flVol, 0.2, SND_CHANGE_PITCH | SND_CHANGE_VOL, pitch); + } + +} + + +void COsprey::HitTouch( CBaseEntity *pOther ) +{ + SetNextThink( 2.0 ); +} + + +/* +int COsprey::TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + if (m_flRotortilt <= -90) + { + m_flRotortilt = 0; + } + else + { + m_flRotortilt -= 45; + } + SetBoneController( 0, m_flRotortilt ); + return 0; +} +*/ + + + +void COsprey :: Killed( entvars_t *pevAttacker, int iGib ) +{ + pev->movetype = MOVETYPE_TOSS; + pev->gravity = 0.3; + pev->velocity = m_velocity; + pev->avelocity = Vector( RANDOM_FLOAT( -20, 20 ), 0, RANDOM_FLOAT( -50, 50 ) ); + STOP_SOUND( ENT(pev), CHAN_STATIC, "apache/ap_rotor4.wav" ); + + UTIL_SetSize( pev, Vector( -32, -32, -64), Vector( 32, 32, 0) ); + SetThink(&COsprey :: DyingThink ); + SetTouch(&COsprey :: CrashTouch ); + SetNextThink( 0.1 ); + pev->health = 0; + pev->takedamage = DAMAGE_NO; + + m_startTime = gpGlobals->time + 4.0; +} + +void COsprey::CrashTouch( CBaseEntity *pOther ) +{ + // only crash if we hit something solid + if ( pOther->pev->solid == SOLID_BSP) + { + SetTouch( NULL ); + m_startTime = gpGlobals->time; + SetNextThink( 0 ); + m_velocity = pev->velocity; + } +} + + +void COsprey :: DyingThink( void ) +{ + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + pev->avelocity = pev->avelocity * 1.02; + + // still falling? + if (m_startTime > gpGlobals->time ) + { + UTIL_MakeAimVectors( pev->angles ); + ShowDamage( ); + + Vector vecSpot = pev->origin + pev->velocity * 0.2; + + // random explosions + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_EXPLOSION); // This just makes a dynamic light now + WRITE_COORD( vecSpot.x + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( vecSpot.y + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( vecSpot.z + RANDOM_FLOAT( -150, -50 )); + WRITE_SHORT( g_sModelIndexFireball ); + WRITE_BYTE( RANDOM_LONG(0,29) + 30 ); // scale * 10 + WRITE_BYTE( 12 ); // framerate + WRITE_BYTE( TE_EXPLFLAG_NONE ); + MESSAGE_END(); + + // lots of smoke + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( vecSpot.x + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( vecSpot.y + RANDOM_FLOAT( -150, 150 )); + WRITE_COORD( vecSpot.z + RANDOM_FLOAT( -150, -50 )); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( 100 ); // scale * 10 + WRITE_BYTE( 10 ); // framerate + MESSAGE_END(); + + + vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_BREAKMODEL); + + // position + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z ); + + // size + WRITE_COORD( 800 ); + WRITE_COORD( 800 ); + WRITE_COORD( 132 ); + + // velocity + WRITE_COORD( pev->velocity.x ); + WRITE_COORD( pev->velocity.y ); + WRITE_COORD( pev->velocity.z ); + + // randomization + WRITE_BYTE( 50 ); + + // Model + WRITE_SHORT( m_iTailGibs ); //model id# + + // # of shards + WRITE_BYTE( 8 ); // let client decide + + // duration + WRITE_BYTE( 200 );// 10.0 seconds + + // flags + + WRITE_BYTE( BREAK_METAL ); + MESSAGE_END(); + + + + // don't stop it we touch a entity + pev->flags &= ~FL_ONGROUND; + SetNextThink( 0.2 ); + return; + } + else + { + Vector vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + + /* + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_EXPLOSION); // This just makes a dynamic light now + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 512 ); + WRITE_SHORT( m_iExplode ); + WRITE_BYTE( 250 ); // scale * 10 + WRITE_BYTE( 10 ); // framerate + MESSAGE_END(); + */ + + // gibs + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_SPRITE ); + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 512 ); + WRITE_SHORT( m_iExplode ); + WRITE_BYTE( 250 ); // scale * 10 + WRITE_BYTE( 255 ); // brightness + MESSAGE_END(); + + /* + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 300 ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( 250 ); // scale * 10 + WRITE_BYTE( 6 ); // framerate + MESSAGE_END(); + */ + + // blast circle + MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, pev->origin ); + WRITE_BYTE( TE_BEAMCYLINDER ); + WRITE_COORD( pev->origin.x); + WRITE_COORD( pev->origin.y); + WRITE_COORD( pev->origin.z); + WRITE_COORD( pev->origin.x); + WRITE_COORD( pev->origin.y); + WRITE_COORD( pev->origin.z + 2000 ); // reach damage radius over .2 seconds + WRITE_SHORT( m_iSpriteTexture ); + WRITE_BYTE( 0 ); // startframe + WRITE_BYTE( 0 ); // framerate + WRITE_BYTE( 4 ); // life + WRITE_BYTE( 32 ); // width + WRITE_BYTE( 0 ); // noise + WRITE_BYTE( 255 ); // r, g, b + WRITE_BYTE( 255 ); // r, g, b + WRITE_BYTE( 192 ); // r, g, b + WRITE_BYTE( 128 ); // brightness + WRITE_BYTE( 0 ); // speed + MESSAGE_END(); + + EMIT_SOUND(ENT(pev), CHAN_STATIC, "weapons/mortarhit.wav", 1.0, 0.3); + + RadiusDamage( pev->origin, pev, pev, 300, CLASS_NONE, DMG_BLAST ); + + // gibs + vecSpot = pev->origin + (pev->mins + pev->maxs) * 0.5; + MESSAGE_BEGIN( MSG_PAS, SVC_TEMPENTITY, vecSpot ); + WRITE_BYTE( TE_BREAKMODEL); + + // position + WRITE_COORD( vecSpot.x ); + WRITE_COORD( vecSpot.y ); + WRITE_COORD( vecSpot.z + 64); + + // size + WRITE_COORD( 800 ); + WRITE_COORD( 800 ); + WRITE_COORD( 128 ); + + // velocity + WRITE_COORD( m_velocity.x ); + WRITE_COORD( m_velocity.y ); + WRITE_COORD( fabs( m_velocity.z ) * 0.25 ); + + // randomization + WRITE_BYTE( 40 ); + + // Model + WRITE_SHORT( m_iBodyGibs ); //model id# + + // # of shards + WRITE_BYTE( 128 ); + + // duration + WRITE_BYTE( 200 );// 10.0 seconds + + // flags + + WRITE_BYTE( BREAK_METAL ); + MESSAGE_END(); + + UTIL_Remove( this ); + } +} + + +void COsprey :: ShowDamage( void ) +{ + if (m_iDoLeftSmokePuff > 0 || RANDOM_LONG(0,99) > m_flLeftHealth) + { + Vector vecSrc = pev->origin + gpGlobals->v_right * -340; + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSrc ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( vecSrc.x ); + WRITE_COORD( vecSrc.y ); + WRITE_COORD( vecSrc.z ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( RANDOM_LONG(0,9) + 20 ); // scale * 10 + WRITE_BYTE( 12 ); // framerate + MESSAGE_END(); + if (m_iDoLeftSmokePuff > 0) + m_iDoLeftSmokePuff--; + } + if (m_iDoRightSmokePuff > 0 || RANDOM_LONG(0,99) > m_flRightHealth) + { + Vector vecSrc = pev->origin + gpGlobals->v_right * 340; + MESSAGE_BEGIN( MSG_PVS, SVC_TEMPENTITY, vecSrc ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( vecSrc.x ); + WRITE_COORD( vecSrc.y ); + WRITE_COORD( vecSrc.z ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( RANDOM_LONG(0,9) + 20 ); // scale * 10 + WRITE_BYTE( 12 ); // framerate + MESSAGE_END(); + if (m_iDoRightSmokePuff > 0) + m_iDoRightSmokePuff--; + } +} + + +void COsprey::TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + // ALERT( at_console, "%d %.0f\n", ptr->iHitgroup, flDamage ); + + // only so much per engine + if (ptr->iHitgroup == 3) + { + if (m_flRightHealth < 0) + return; + else + m_flRightHealth -= flDamage; + m_iDoLeftSmokePuff = 3 + (flDamage / 5.0); + } + + if (ptr->iHitgroup == 2) + { + if (m_flLeftHealth < 0) + return; + else + m_flLeftHealth -= flDamage; + m_iDoRightSmokePuff = 3 + (flDamage / 5.0); + } + + // hit hard, hits cockpit, hits engines + if (flDamage > 50 || ptr->iHitgroup == 1 || ptr->iHitgroup == 2 || ptr->iHitgroup == 3) + { + // ALERT( at_console, "%.0f\n", flDamage ); + AddMultiDamage( pevAttacker, this, flDamage, bitsDamageType ); + } + else + { + UTIL_Sparks( ptr->vecEndPos ); + } +} + + + + + diff --git a/server/monsters/player.cpp b/server/monsters/player.cpp new file mode 100644 index 00000000..076747ae --- /dev/null +++ b/server/monsters/player.cpp @@ -0,0 +1,5176 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +/* + +===== player.cpp ======================================================== + + functions dealing with the player + +*/ + +#include "extdll.h" +#include "utils.h" + +#include "cbase.h" +#include "client.h" +#include "player.h" +#include "nodes.h" +#include "baseweapon.h" +#include "soundent.h" +#include "monsters.h" +#include "shake.h" +#include "decals.h" +#include "gamerules.h" +#include "game.h" +#include "basebeams.h" +#include "defaults.h" + + +extern DLL_GLOBAL ULONG g_ulModelIndexPlayer; +extern DLL_GLOBAL BOOL g_fGameOver; +extern DLL_GLOBAL BOOL g_fDrawLines; +float g_flWeaponCheat; +int gEvilImpulse101; +BOOL GiveOnlyAmmo; +BOOL g_markFrameBounds = 0; //LRC +BOOL gInitHUD = TRUE; + +int MapTextureTypeStepType(char chTextureType); +extern void CopyToBodyQue(entvars_t* pev); +extern void respawn(entvars_t *pev, BOOL fCopyCorpse); +extern Vector VecBModelOrigin(entvars_t *pevBModel ); +extern edict_t *EntSelectSpawnPoint( CBaseEntity *pPlayer ); +extern void LinkUserMessages( void ); + +// the world node graph +extern CGraph WorldGraph; + +#define PLAYER_WALLJUMP_SPEED 300 // how fast we can spring off walls +#define PLAYER_LONGJUMP_SPEED 350 // how fast we longjump + +#define TRAIN_ACTIVE 0x80 +#define TRAIN_NEW 0xc0 +#define TRAIN_OFF 0x00 +#define TRAIN_NEUTRAL 0x01 +#define TRAIN_SLOW 0x02 +#define TRAIN_MEDIUM 0x03 +#define TRAIN_FAST 0x04 +#define TRAIN_BACK 0x05 + +#define FLASH_DRAIN_TIME 1.3 //100 units/3 minutes +#define FLASH_CHARGE_TIME 0.2 // 100 units/20 seconds (seconds per unit) + +// Global Savedata for player +TYPEDESCRIPTION CBasePlayer::m_playerSaveData[] = +{ + DEFINE_FIELD( CBasePlayer, m_flFlashLightTime, FIELD_TIME ), + DEFINE_FIELD( CBasePlayer, m_iFlashBattery, FIELD_INTEGER ), + + DEFINE_FIELD( CBasePlayer, m_afButtonLast, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_afButtonPressed, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_afButtonReleased, FIELD_INTEGER ), + + DEFINE_ARRAY( CBasePlayer, m_rgItems, FIELD_INTEGER, MAX_ITEMS ), + DEFINE_FIELD( CBasePlayer, m_afPhysicsFlags, FIELD_INTEGER ), + + DEFINE_FIELD( CBasePlayer, m_flTimeStepSound, FIELD_TIME ), + DEFINE_FIELD( CBasePlayer, m_flTimeWeaponIdle, FIELD_TIME ), + DEFINE_FIELD( CBasePlayer, m_flSwimTime, FIELD_TIME ), + DEFINE_FIELD( CBasePlayer, m_flDuckTime, FIELD_TIME ), + DEFINE_FIELD( CBasePlayer, m_flWallJumpTime, FIELD_TIME ), + + DEFINE_FIELD( CBasePlayer, m_flSuitUpdate, FIELD_TIME ), + DEFINE_ARRAY( CBasePlayer, m_rgSuitPlayList, FIELD_INTEGER, CSUITPLAYLIST ), + DEFINE_FIELD( CBasePlayer, m_iSuitPlayNext, FIELD_INTEGER ), + DEFINE_ARRAY( CBasePlayer, m_rgiSuitNoRepeat, FIELD_INTEGER, CSUITNOREPEAT ), + DEFINE_ARRAY( CBasePlayer, m_rgflSuitNoRepeatTime, FIELD_TIME, CSUITNOREPEAT ), + DEFINE_ARRAY( CBasePlayer, m_szAnimExtention, FIELD_CHARACTER, 32), + DEFINE_FIELD( CBasePlayer, m_lastDamageAmount, FIELD_INTEGER ), + + DEFINE_ARRAY( CBasePlayer, m_rgpPlayerItems, FIELD_CLASSPTR, MAX_ITEM_TYPES ), + DEFINE_FIELD( CBasePlayer, m_pActiveItem, FIELD_CLASSPTR ), + DEFINE_FIELD( CBasePlayer, m_pLastItem, FIELD_CLASSPTR ), + DEFINE_FIELD( CBasePlayer, m_pNextItem, FIELD_CLASSPTR ), + + DEFINE_ARRAY( CBasePlayer, m_rgAmmo, FIELD_INTEGER, MAX_AMMO_SLOTS ), + DEFINE_FIELD( CBasePlayer, m_idrowndmg, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_idrownrestored, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_tSneaking, FIELD_TIME ), + + DEFINE_FIELD( CBasePlayer, m_iTrain, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_bitsHUDDamage, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_flFallVelocity, FIELD_FLOAT ), + DEFINE_FIELD( CBasePlayer, m_iTargetVolume, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iWeaponVolume, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iExtraSoundTypes, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iWeaponFlash, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_fLongJump, FIELD_BOOLEAN ), + DEFINE_FIELD( CBasePlayer, m_fInitHUD, FIELD_BOOLEAN ), + DEFINE_FIELD( CBasePlayer, m_tbdPrev, FIELD_TIME ), + + DEFINE_FIELD( CBasePlayer, m_pTank, FIELD_EHANDLE ), // NB: this points to a CFuncTank*Controls* now. --LRC + DEFINE_FIELD( CBasePlayer, m_pMonitor, FIELD_EHANDLE ), + DEFINE_FIELD( CBasePlayer, m_iHideHUD, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iFOV, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, pViewEnt, FIELD_CLASSPTR), + DEFINE_FIELD( CBasePlayer, viewFlags, FIELD_INTEGER), + DEFINE_FIELD( CBasePlayer, m_iSndRoomtype, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iFogStartDist, FIELD_INTEGER), + DEFINE_FIELD( CBasePlayer, m_iFogEndDist, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iFogFinalEndDist, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_FogColor, FIELD_VECTOR ), + DEFINE_FIELD( CBasePlayer, m_FogFadeTime, FIELD_INTEGER), + DEFINE_FIELD( CBasePlayer, m_iWarHUD, FIELD_INTEGER), + + DEFINE_FIELD( CBasePlayer, m_FadeColor, FIELD_VECTOR ), + DEFINE_FIELD( CBasePlayer, m_FadeAlpha, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iFadeFlags, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, m_iFadeHold, FIELD_INTEGER ), + + DEFINE_FIELD( CBasePlayer, m_flStartTime, FIELD_TIME ), + + DEFINE_FIELD( CBasePlayer, Rain_dripsPerSecond, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, Rain_windX, FIELD_FLOAT ), + DEFINE_FIELD( CBasePlayer, Rain_windY, FIELD_FLOAT ), + DEFINE_FIELD( CBasePlayer, Rain_randX, FIELD_FLOAT ), + DEFINE_FIELD( CBasePlayer, Rain_randY, FIELD_FLOAT ), + + DEFINE_FIELD( CBasePlayer, Rain_ideal_dripsPerSecond, FIELD_INTEGER ), + DEFINE_FIELD( CBasePlayer, Rain_ideal_windX, FIELD_FLOAT ), + DEFINE_FIELD( CBasePlayer, Rain_ideal_windY, FIELD_FLOAT ), + DEFINE_FIELD( CBasePlayer, Rain_ideal_randX, FIELD_FLOAT ), + DEFINE_FIELD( CBasePlayer, Rain_ideal_randY, FIELD_FLOAT ), + + DEFINE_FIELD( CBasePlayer, Rain_endFade, FIELD_TIME ), + DEFINE_FIELD( CBasePlayer, Rain_nextFadeUpdate, FIELD_TIME ), + + //LRC + //DEFINE_FIELD( CBasePlayer, m_iFogStartDist, FIELD_INTEGER ), + //DEFINE_FIELD( CBasePlayer, m_iFogEndDist, FIELD_INTEGER ), + //DEFINE_FIELD( CBasePlayer, m_vecFogColor, FIELD_VECTOR ), + + //DEFINE_FIELD( CBasePlayer, m_fDeadTime, FIELD_FLOAT ), // only used in multiplayer games + //DEFINE_FIELD( CBasePlayer, m_fGameHUDInitialized, FIELD_INTEGER ), // only used in multiplayer games + //DEFINE_FIELD( CBasePlayer, m_flStopExtraSoundTime, FIELD_TIME ), + //DEFINE_FIELD( CBasePlayer, m_fKnownItem, FIELD_INTEGER ), // reset to zero on load + //DEFINE_FIELD( CBasePlayer, m_iPlayerSound, FIELD_INTEGER ), // Don't restore, set in Precache() + //DEFINE_FIELD( CBasePlayer, m_pentSndLast, FIELD_EDICT ), // Don't restore, client needs reset + //DEFINE_FIELD( CBasePlayer, m_flSndRange, FIELD_FLOAT ), // Don't restore, client needs reset + //DEFINE_FIELD( CBasePlayer, m_fNewAmmo, FIELD_INTEGER ), // Don't restore, client needs reset + //DEFINE_FIELD( CBasePlayer, m_flgeigerRange, FIELD_FLOAT ), // Don't restore, reset in Precache() + //DEFINE_FIELD( CBasePlayer, m_flgeigerDelay, FIELD_FLOAT ), // Don't restore, reset in Precache() + //DEFINE_FIELD( CBasePlayer, m_igeigerRangePrev, FIELD_FLOAT ), // Don't restore, reset in Precache() + //DEFINE_FIELD( CBasePlayer, m_iStepLeft, FIELD_INTEGER ), // Don't need to restore + //DEFINE_ARRAY( CBasePlayer, m_szTextureName, FIELD_CHARACTER, CBTEXTURENAMEMAX ), // Don't need to restore + //DEFINE_FIELD( CBasePlayer, m_chTextureType, FIELD_CHARACTER ), // Don't need to restore + //DEFINE_FIELD( CBasePlayer, m_fNoPlayerSound, FIELD_BOOLEAN ), // Don't need to restore, debug + //DEFINE_FIELD( CBasePlayer, m_iUpdateTime, FIELD_INTEGER ), // Don't need to restore + //DEFINE_FIELD( CBasePlayer, m_iClientHealth, FIELD_INTEGER ), // Don't restore, client needs reset + //DEFINE_FIELD( CBasePlayer, m_iClientBattery, FIELD_INTEGER ), // Don't restore, client needs reset + //DEFINE_FIELD( CBasePlayer, m_iClientHideHUD, FIELD_INTEGER ), // Don't restore, client needs reset + //DEFINE_FIELD( CBasePlayer, m_fWeapon, FIELD_BOOLEAN ), // Don't restore, client needs reset + //DEFINE_FIELD( CBasePlayer, m_nCustomSprayFrames, FIELD_INTEGER ), // Don't restore, depends on server message after spawning and only matters in multiplayer + //DEFINE_FIELD( CBasePlayer, m_vecAutoAim, FIELD_VECTOR ), // Don't save/restore - this is recomputed + //DEFINE_ARRAY( CBasePlayer, m_rgAmmoLast, FIELD_INTEGER, MAX_AMMO_SLOTS ), // Don't need to restore + //DEFINE_FIELD( CBasePlayer, m_fOnTarget, FIELD_BOOLEAN ), // Don't need to restore + //DEFINE_FIELD( CBasePlayer, m_nCustomSprayFrames, FIELD_INTEGER ), // Don't need to restore + +}; +LINK_ENTITY_TO_CLASS( player, CBasePlayer ); + + + +void CBasePlayer :: Pain( void ) +{ + float flRndSound;//sound randomizer + + flRndSound = RANDOM_FLOAT ( 0 , 1 ); + + if ( flRndSound <= 0.33 ) + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain5.wav", 1, ATTN_NORM); + else if ( flRndSound <= 0.66 ) + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain6.wav", 1, ATTN_NORM); + else + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain7.wav", 1, ATTN_NORM); +} + +/* + * + */ +Vector VecVelocityForDamage(float flDamage) +{ + Vector vec(RANDOM_FLOAT(-100,100), RANDOM_FLOAT(-100,100), RANDOM_FLOAT(200,300)); + + if (flDamage > -50) + vec = vec * 0.7; + else if (flDamage > -200) + vec = vec * 2; + else + vec = vec * 10; + + return vec; +} + +#if 0 /* +static void ThrowGib(entvars_t *pev, char *szGibModel, float flDamage) +{ + edict_t *pentNew = CREATE_ENTITY(); + entvars_t *pevNew = VARS(pentNew); + + pevNew->origin = pev->origin; + SET_MODEL(ENT(pevNew), szGibModel); + UTIL_SetSize(pevNew, g_vecZero, g_vecZero); + + pevNew->velocity = VecVelocityForDamage(flDamage); + pevNew->movetype = MOVETYPE_BOUNCE; + pevNew->solid = SOLID_NOT; + pevNew->avelocity.x = RANDOM_FLOAT(0,600); + pevNew->avelocity.y = RANDOM_FLOAT(0,600); + pevNew->avelocity.z = RANDOM_FLOAT(0,600); + CHANGE_METHOD(ENT(pevNew), em_think, Remove); + pevNew->ltime = gpGlobals->time; + pevNew->nextthink = gpGlobals->time + RANDOM_FLOAT(10,20); + pevNew->frame = 0; + pevNew->flags = 0; +} + + +static void ThrowHead(entvars_t *pev, char *szGibModel, floatflDamage) +{ + SET_MODEL(ENT(pev), szGibModel); + pev->frame = 0; + pev->nextthink = -1; + pev->movetype = MOVETYPE_BOUNCE; + pev->takedamage = DAMAGE_NO; + pev->solid = SOLID_NOT; + pev->view_ofs = Vector(0,0,8); + UTIL_SetSize(pev, Vector(-16,-16,0), Vector(16,16,56)); + pev->velocity = VecVelocityForDamage(flDamage); + pev->avelocity = RANDOM_FLOAT(-1,1) * Vector(0,600,0); + pev->origin.z -= 24; + ClearBits(pev->flags, FL_ONGROUND); +} + + +*/ +#endif + +int TrainSpeed(int iSpeed, int iMax) +{ + float fSpeed, fMax; + int iRet = 0; + + fMax = (float)iMax; + fSpeed = iSpeed; + + fSpeed = fSpeed/fMax; + + if (iSpeed < 0) + iRet = TRAIN_BACK; + else if (iSpeed == 0) + iRet = TRAIN_NEUTRAL; + else if (fSpeed < 0.33) + iRet = TRAIN_SLOW; + else if (fSpeed < 0.66) + iRet = TRAIN_MEDIUM; + else + iRet = TRAIN_FAST; + + return iRet; +} + +void CBasePlayer :: DeathSound( void ) +{ + // water death sounds + /* + if (pev->waterlevel == 3) + { + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/h2odeath.wav", 1, ATTN_NONE); + return; + } + */ + + // temporarily using pain sounds for death sounds + switch (RANDOM_LONG(1,5)) + { + case 1: + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain5.wav", 1, ATTN_NORM); + break; + case 2: + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain6.wav", 1, ATTN_NORM); + break; + case 3: + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain7.wav", 1, ATTN_NORM); + break; + } + + // play one of the suit death alarms + //LRC- if no suit, then no flatline sound. (unless it's a deathmatch.) + if (!(m_iHideHUD & ITEM_SUIT) && !g_pGameRules->IsDeathmatch() )return; + EMIT_GROUPNAME_SUIT(ENT(pev), "HEV_DEAD"); +} + +// override takehealth +// bitsDamageType indicates type of damage healed. + +int CBasePlayer :: TakeHealth( float flHealth, int bitsDamageType ) +{ + return CBaseMonster::TakeHealth (flHealth, bitsDamageType); +} + +int CBasePlayer :: TakeArmor( float flArmor, int suit ) +{ + if(!(m_iHideHUD & ITEM_SUIT)) return 0; + + if(CBaseMonster::TakeArmor(flArmor )) + { + if(!suit) return 1; //silent take armor + int pct; + char szcharge[64]; + + // Suit reports new power level + pct = (int)( (float)(pev->armorvalue * 100.0) * (1.0/MAX_NORMAL_BATTERY) + 0.5); + pct = (pct / 5); + if (pct > 0) pct--; + + sprintf( szcharge,"!HEV_%1dP", pct ); + SetSuitUpdate(szcharge, FALSE, SUIT_NEXT_IN_30SEC); + + return 1; + } + return 0; +} + +int CBasePlayer :: TakeItem( int iItem ) +{ + if ( m_iHideHUD & iItem ) + return 0; + return 1; +} + +Vector CBasePlayer :: GetGunPosition( ) +{ + return pev->origin + pev->view_ofs; +} + +//========================================================= +// TraceAttack +//========================================================= +void CBasePlayer :: TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + if ( pev->takedamage ) + { + m_LastHitGroup = ptr->iHitgroup; + + switch ( ptr->iHitgroup ) + { + case HITGROUP_GENERIC: + break; + case HITGROUP_HEAD: + flDamage *= DMG_HEAD; + break; + case HITGROUP_CHEST: + flDamage *= DMG_CHEST; + break; + case HITGROUP_STOMACH: + flDamage *= DMG_STOMACH; + break; + case HITGROUP_LEFTARM: + case HITGROUP_RIGHTARM: + flDamage *= DMG_ARM; + break; + case HITGROUP_LEFTLEG: + case HITGROUP_RIGHTLEG: + flDamage *= DMG_LEG; + break; + default: + break; + } + + if(bitsDamageType & DMG_NUCLEAR) + { + m_FadeColor = Vector(255, 255, 255); + m_FadeAlpha = 240; + m_iFadeFlags = 0; + m_iFadeTime = 25; + fadeNeedsUpdate = TRUE; + } + else SpawnBlood(ptr->vecEndPos, BloodColor(), flDamage);// a little surface blood. + TraceBleed( flDamage, vecDir, ptr, bitsDamageType ); + AddMultiDamage( pevAttacker, this, flDamage, bitsDamageType ); + } +} + +/* + Take some damage. + NOTE: each call to TakeDamage with bitsDamageType set to a time-based damage + type will cause the damage time countdown to be reset. Thus the ongoing effects of poison, radiation + etc are implemented with subsequent calls to TakeDamage using DMG_GENERIC. +*/ + +#define ARMOR_RATIO 0.2 // Armor Takes 80% of the damage +#define ARMOR_BONUS 0.5 // Each Point of Armor is work 1/x points of health + +int CBasePlayer :: TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + // have suit diagnose the problem - ie: report damage type + int bitsDamage = bitsDamageType; + int ffound = TRUE; + int fmajor; + int fcritical; + int fTookDamage; + int ftrivial; + float flRatio; + float flBonus; + float flHealthPrev = pev->health; + + flBonus = ARMOR_BONUS; + flRatio = ARMOR_RATIO; + + if ( ( bitsDamageType & DMG_BLAST ) && g_pGameRules->IsMultiplayer() ) + { + // blasts damage armor more. + flBonus *= 2; + } + + // Already dead + if ( !IsAlive() ) + return 0; + // go take the damage first + + + CBaseEntity *pAttacker = CBaseEntity::Instance(pevAttacker); + + if ( !g_pGameRules->FPlayerCanTakeDamage( this, pAttacker ) ) + { + // Refuse the damage + return 0; + } + + // keep track of amount of damage last sustained + m_lastDamageAmount = flDamage; + + // Armor. + if (pev->armorvalue && !(bitsDamageType & (DMG_FALL | DMG_DROWN)) )// armor doesn't protect against fall or drown damage! + { + float flNew = flDamage * flRatio; + + float flArmor; + + flArmor = (flDamage - flNew) * flBonus; + + // Does this use more armor than we have? + if (flArmor > pev->armorvalue) + { + flArmor = pev->armorvalue; + flArmor *= (1/flBonus); + flNew = flDamage - flArmor; + pev->armorvalue = 0; + } + else + pev->armorvalue -= flArmor; + + flDamage = flNew; + } + + // this cast to INT is critical!!! If a player ends up with 0.5 health, the engine will get that + // as an int (zero) and think the player is dead! (this will incite a clientside screentilt, etc) + fTookDamage = CBaseMonster::TakeDamage(pevInflictor, pevAttacker, (int)flDamage, bitsDamageType); + + // reset damage time countdown for each type of time based damage player just sustained + + { + for (int i = 0; i < CDMG_TIMEBASED; i++) + if (bitsDamageType & (DMG_PARALYZE << i)) + m_rgbTimeBasedDamage[i] = 0; + } + + + // how bad is it, doc? + + ftrivial = (pev->health > 75 || m_lastDamageAmount < 5); + fmajor = (m_lastDamageAmount > 25); + fcritical = (pev->health < 30); + + // handle all bits set in this damage message, + // let the suit give player the diagnosis + + // UNDONE: add sounds for types of damage sustained (ie: burn, shock, slash ) + + // UNDONE: still need to record damage and heal messages for the following types + + // DMG_BURN + // DMG_FREEZE + // DMG_BLAST + // DMG_SHOCK + + m_bitsDamageType |= bitsDamage; // Save this so we can report it to the client + m_bitsHUDDamage = -1; // make sure the damage bits get resent + + while (fTookDamage && (!ftrivial || (bitsDamage & DMG_TIMEBASED)) && ffound && bitsDamage) + { + ffound = FALSE; + + if (bitsDamage & DMG_CLUB) + { + if (fmajor) + SetSuitUpdate("!HEV_DMG4", FALSE, SUIT_NEXT_IN_30SEC); // minor fracture + bitsDamage &= ~DMG_CLUB; + ffound = TRUE; + } + if (bitsDamage & (DMG_FALL | DMG_CRUSH)) + { + if (fmajor) + SetSuitUpdate("!HEV_DMG5", FALSE, SUIT_NEXT_IN_30SEC); // major fracture + else + SetSuitUpdate("!HEV_DMG4", FALSE, SUIT_NEXT_IN_30SEC); // minor fracture + + bitsDamage &= ~(DMG_FALL | DMG_CRUSH); + ffound = TRUE; + } + + if (bitsDamage & DMG_BULLET) + { + if (m_lastDamageAmount > 5) + SetSuitUpdate("!HEV_DMG6", FALSE, SUIT_NEXT_IN_30SEC); // blood loss detected + //else + // SetSuitUpdate("!HEV_DMG0", FALSE, SUIT_NEXT_IN_30SEC); // minor laceration + + bitsDamage &= ~DMG_BULLET; + ffound = TRUE; + } + + if (bitsDamage & DMG_SLASH) + { + if (fmajor) + SetSuitUpdate("!HEV_DMG1", FALSE, SUIT_NEXT_IN_30SEC); // major laceration + else + SetSuitUpdate("!HEV_DMG0", FALSE, SUIT_NEXT_IN_30SEC); // minor laceration + + bitsDamage &= ~DMG_SLASH; + ffound = TRUE; + } + if (bitsDamage & DMG_SONIC) + { + if (fmajor) + SetSuitUpdate("!HEV_DMG2", FALSE, SUIT_NEXT_IN_1MIN); // internal bleeding + bitsDamage &= ~DMG_SONIC; + ffound = TRUE; + } + + if (bitsDamage & (DMG_POISON | DMG_PARALYZE)) + { + SetSuitUpdate("!HEV_DMG3", FALSE, SUIT_NEXT_IN_1MIN); // blood toxins detected + bitsDamage &= ~(DMG_POISON | DMG_PARALYZE); + ffound = TRUE; + } + + if (bitsDamage & DMG_ACID) + { + SetSuitUpdate("!HEV_DET1", FALSE, SUIT_NEXT_IN_1MIN); // hazardous chemicals detected + bitsDamage &= ~DMG_ACID; + ffound = TRUE; + } + + if (bitsDamage & DMG_NERVEGAS) + { + SetSuitUpdate("!HEV_DET0", FALSE, SUIT_NEXT_IN_1MIN); // biohazard detected + bitsDamage &= ~DMG_NERVEGAS; + ffound = TRUE; + } + + if (bitsDamage & DMG_RADIATION) + { + SetSuitUpdate("!HEV_DET2", FALSE, SUIT_NEXT_IN_1MIN); // radiation detected + bitsDamage &= ~DMG_RADIATION; + ffound = TRUE; + } + if (bitsDamage & DMG_NUCLEAR) + { + SetSuitUpdate("!HEV_DET2", FALSE, SUIT_NEXT_IN_1MIN); // radiation detected + bitsDamage &= ~DMG_NUCLEAR; + ffound = TRUE; + } + if (bitsDamage & DMG_SHOCK) + { + bitsDamage &= ~DMG_SHOCK; + ffound = TRUE; + } + } + + pev->punchangle.x = -2; + + if (fTookDamage && !ftrivial && fmajor && flHealthPrev >= 75) + { + // first time we take major damage... + // turn automedic on if not on + SetSuitUpdate("!HEV_MED1", FALSE, SUIT_NEXT_IN_30MIN); // automedic on + + // give morphine shot if not given recently + SetSuitUpdate("!HEV_HEAL7", FALSE, SUIT_NEXT_IN_30MIN); // morphine shot + } + + if (fTookDamage && !ftrivial && fcritical && flHealthPrev < 75) + { + + // already took major damage, now it's critical... + if (pev->health < 6) + SetSuitUpdate("!HEV_HLTH3", FALSE, SUIT_NEXT_IN_10MIN); // near death + else if (pev->health < 20) + SetSuitUpdate("!HEV_HLTH2", FALSE, SUIT_NEXT_IN_10MIN); // health critical + + // give critical health warnings + if (!RANDOM_LONG(0,3) && flHealthPrev < 50) + SetSuitUpdate("!HEV_DMG7", FALSE, SUIT_NEXT_IN_5MIN); //seek medical attention + } + + // if we're taking time based damage, warn about its continuing effects + if (fTookDamage && (bitsDamageType & DMG_TIMEBASED) && flHealthPrev < 75) + { + if (flHealthPrev < 50) + { + if (!RANDOM_LONG(0,3)) + SetSuitUpdate("!HEV_DMG7", FALSE, SUIT_NEXT_IN_5MIN); //seek medical attention + } + else + SetSuitUpdate("!HEV_HLTH1", FALSE, SUIT_NEXT_IN_10MIN); // health dropping + } + + return fTookDamage; +} + +//========================================================= +// PackDeadPlayerItems - call this when a player dies to +// pack up the appropriate weapons and ammo items, and to +// destroy anything that shouldn't be packed. +// +// This is pretty brute force :( +//========================================================= +void CBasePlayer::PackDeadPlayerItems( void ) +{ + int iWeaponRules; + int iAmmoRules; + int i; + CBasePlayerWeapon *rgpPackWeapons[ 20 ];// 20 hardcoded for now. How to determine exactly how many weapons we have? + int iPackAmmo[ MAX_AMMO_SLOTS + 1]; + int iPW = 0;// index into packweapons array + int iPA = 0;// index into packammo array + + memset(rgpPackWeapons, NULL, sizeof(rgpPackWeapons) ); + memset(iPackAmmo, -1, sizeof(iPackAmmo) ); + + // get the game rules + iWeaponRules = g_pGameRules->DeadPlayerWeapons( this ); + iAmmoRules = g_pGameRules->DeadPlayerAmmo( this ); + + if ( iWeaponRules == GR_PLR_DROP_GUN_NO && iAmmoRules == GR_PLR_DROP_AMMO_NO ) + { + // nothing to pack. Remove the weapons and return. Don't call create on the box! + RemoveAllItems( TRUE ); + return; + } + +// go through all of the weapons and make a list of the ones to pack + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + if ( m_rgpPlayerItems[ i ] ) + { + // there's a weapon here. Should I pack it? + CBasePlayerWeapon *pPlayerItem = m_rgpPlayerItems[ i ]; + + while ( pPlayerItem ) + { + switch( iWeaponRules ) + { + case GR_PLR_DROP_GUN_ACTIVE: + if ( m_pActiveItem && pPlayerItem == m_pActiveItem ) + { + // this is the active item. Pack it. + rgpPackWeapons[ iPW++ ] = (CBasePlayerWeapon *)pPlayerItem; + } + break; + + case GR_PLR_DROP_GUN_ALL: + rgpPackWeapons[ iPW++ ] = (CBasePlayerWeapon *)pPlayerItem; + break; + + default: + break; + } + + pPlayerItem = pPlayerItem->m_pNext; + } + } + } + +// now go through ammo and make a list of which types to pack. + if ( iAmmoRules != GR_PLR_DROP_AMMO_NO ) + { + for ( i = 0 ; i < MAX_AMMO_SLOTS ; i++ ) + { + if ( m_rgAmmo[ i ] > 0 ) + { + // player has some ammo of this type. + switch ( iAmmoRules ) + { + case GR_PLR_DROP_AMMO_ALL: + iPackAmmo[ iPA++ ] = i; + break; + + case GR_PLR_DROP_AMMO_ACTIVE: + if ( m_pActiveItem && i == m_pActiveItem->m_iPrimaryAmmoType ) + { + // this is the primary ammo type for the active weapon + iPackAmmo[ iPA++ ] = i; + } + else if ( m_pActiveItem && i == m_pActiveItem->m_iSecondaryAmmoType ) + { + // this is the secondary ammo type for the active weapon + iPackAmmo[ iPA++ ] = i; + } + break; + + default: + break; + } + } + } + } + +// create a box to pack the stuff into. + CWeaponBox *pWeaponBox = (CWeaponBox *)CBaseEntity::Create( "item_weaponbox", pev->origin, pev->angles, edict() ); + + pWeaponBox->pev->angles.x = 0;// don't let weaponbox tilt. + pWeaponBox->pev->angles.z = 0; + + pWeaponBox->SetThink(& CWeaponBox::Kill ); + pWeaponBox->SetNextThink( 120 ); + +// back these two lists up to their first elements + iPA = 0; + iPW = 0; + +// pack the ammo + while ( iPackAmmo[ iPA ] != -1 ) + { + pWeaponBox->PackAmmo( MAKE_STRING( CBasePlayerWeapon::AmmoInfoArray[ iPackAmmo[ iPA ] ].pszName ), m_rgAmmo[ iPackAmmo[ iPA ] ] ); + iPA++; + } + +// now pack all of the items in the lists + while ( rgpPackWeapons[ iPW ] ) + { + // weapon unhooked from the player. Pack it into der box. + pWeaponBox->PackWeapon( rgpPackWeapons[ iPW ] ); + + iPW++; + } + + pWeaponBox->pev->velocity = pev->velocity * 1.2;// weaponbox has player's velocity, then some. + + RemoveAllItems( TRUE );// now strip off everything that wasn't handled by the code above. +} + +void CBasePlayer::RemoveAllItems( BOOL removeSuit ) +{ + if (m_pActiveItem) + { + ResetAutoaim( ); + m_pActiveItem->Holster( ); + m_pActiveItem = NULL; + } + + m_pLastItem = NULL; + + int i; + CBasePlayerWeapon *pPendingItem; + for (i = 0; i < MAX_ITEM_TYPES; i++) + { + m_pActiveItem = m_rgpPlayerItems[i]; + while (m_pActiveItem) + { + pPendingItem = m_pActiveItem->m_pNext; + m_pActiveItem->Drop( ); + m_pActiveItem = pPendingItem; + } + m_rgpPlayerItems[i] = NULL; + } + m_pActiveItem = NULL; + + pev->viewmodel = 0; + pev->weaponmodel = 0; + pev->weapons = 0; + + if ( removeSuit ) m_iHideHUD &= ~ITEM_SUIT; + + for ( i = 0; i < MAX_AMMO_SLOTS; i++) + m_rgAmmo[i] = 0; + + UpdateClientData(); + + // send Selected Weapon Message to our client + MESSAGE_BEGIN( MSG_ONE, gmsgCurWeapon, NULL, pev ); + WRITE_BYTE(0); + WRITE_BYTE(0); + WRITE_BYTE(0); + MESSAGE_END(); +} + +/* + * GLOBALS ASSUMED SET: g_ulModelIndexPlayer + * + * ENTITY_METHOD(PlayerDie) + */ +entvars_t *g_pevLastInflictor; // Set in combat.cpp. Used to pass the damage inflictor for death messages. + // Better solution: Add as parameter to all Killed() functions. + +void CBasePlayer::Killed( entvars_t *pevAttacker, int iGib ) +{ + CSound *pSound; + + // Holster weapon immediately, to allow it to cleanup + if ( m_pActiveItem ) + m_pActiveItem->Holster( ); + + m_pNextItem = NULL; + + g_pGameRules->PlayerKilled( this, pevAttacker, g_pevLastInflictor ); + + if ( m_pTank != NULL ) + { + m_pTank->Use( this, this, USE_OFF, 0 ); + m_pTank = NULL; + } + + if( m_pMonitor != NULL ) + { + m_pMonitor->Use( this, this, USE_SET, 1 ); + m_pMonitor = NULL; + } + // this client isn't going to be thinking for a while, so reset the sound until they respawn + pSound = CSoundEnt::SoundPointerForIndex( CSoundEnt::ClientSoundIndex( edict() ) ); + { + if ( pSound ) + { + pSound->Reset(); + } + } + + SetAnimation( PLAYER_DIE ); + + m_iRespawnFrames = 0; + + pev->modelindex = g_ulModelIndexPlayer; // don't use eyes + + pev->deadflag = DEAD_DYING; + pev->movetype = MOVETYPE_TOSS; + ClearBits( pev->flags, FL_ONGROUND ); + if (pev->velocity.z < 10) + pev->velocity.z += RANDOM_FLOAT(0,300); + + // clear out the suit message cache so we don't keep chattering + SetSuitUpdate(NULL, FALSE, 0); + + // send "health" update message to zero + m_iClientHealth = 0; + MESSAGE_BEGIN( MSG_ONE, gmsgHealth, NULL, pev ); + WRITE_BYTE( m_iClientHealth ); + MESSAGE_END(); + + // Tell Ammo Hud that the player is dead + MESSAGE_BEGIN( MSG_ONE, gmsgCurWeapon, NULL, pev ); + WRITE_BYTE(0); + WRITE_BYTE(0XFF); + WRITE_BYTE(0xFF); + MESSAGE_END(); + + // reset FOV + m_iFOV = m_iClientFOV = 90; + + pViewEnt = 0; + viewFlags = 0; + viewNeedsUpdate = 1; + + MESSAGE_BEGIN( MSG_ONE, gmsgSetFOV, NULL, pev ); + WRITE_BYTE(0); + MESSAGE_END(); + + //death fading + m_FadeColor = Vector(128, 0, 0); + m_FadeAlpha = 240; + m_iFadeFlags = FFADE_OUT | FFADE_MODULATE | FFADE_STAYOUT; + m_iFadeTime = 6; + fadeNeedsUpdate = TRUE; + + if ( ( pev->health < -40 && iGib != GIB_NEVER ) || iGib == GIB_ALWAYS ) + { + pev->solid = SOLID_NOT; + GibMonster(); // This clears pev->model + pev->effects |= EF_NODRAW; + return; + } + + DeathSound(); + + pev->angles.x = 0; + pev->angles.z = 0; + + SetThink(PlayerDeathThink); + SetNextThink( 0.1 ); +} + + +// Set the activity based on an event or current state +void CBasePlayer::SetAnimation( PLAYER_ANIM playerAnim ) +{ + int animDesired; + float speed; + char szAnim[64]; + + speed = pev->velocity.Length2D(); + + if( pev->flags & FL_FROZEN ) + { + speed = 0; + playerAnim = PLAYER_IDLE; + } + + switch( playerAnim ) + { + case PLAYER_JUMP: + m_IdealActivity = ACT_HOP; + break; + + case PLAYER_SUPERJUMP: + m_IdealActivity = ACT_LEAP; + break; + + case PLAYER_DIE: + m_IdealActivity = ACT_DIESIMPLE; + m_IdealActivity = GetDeathActivity( ); + break; + + case PLAYER_ATTACK1: + switch( m_Activity ) + { + case ACT_HOVER: + case ACT_SWIM: + case ACT_HOP: + case ACT_LEAP: + case ACT_DIESIMPLE: + m_IdealActivity = m_Activity; + break; + default: + m_IdealActivity = ACT_RANGE_ATTACK1; + break; + } + break; + case PLAYER_IDLE: + case PLAYER_WALK: + if ( !FBitSet( pev->flags, FL_ONGROUND ) && (m_Activity == ACT_HOP || m_Activity == ACT_LEAP) ) // Still jumping + { + m_IdealActivity = m_Activity; + } + else if ( pev->waterlevel > 1 && !FBitSet( pev->watertype, CONTENTS_FOG )) + { + if ( speed == 0 ) + m_IdealActivity = ACT_HOVER; + else + m_IdealActivity = ACT_SWIM; + } + else + { + m_IdealActivity = ACT_WALK; + } + break; + } + + switch (m_IdealActivity) + { + case ACT_HOVER: + case ACT_LEAP: + case ACT_SWIM: + case ACT_HOP: + case ACT_DIESIMPLE: + default: + if ( m_Activity == m_IdealActivity) + return; + m_Activity = m_IdealActivity; + + animDesired = LookupActivity( m_Activity ); + // Already using the desired animation? + if (pev->sequence == animDesired) + return; + + pev->gaitsequence = 0; + pev->sequence = animDesired; + pev->frame = 0; + ResetSequenceInfo( ); + return; + + case ACT_RANGE_ATTACK1: + if ( FBitSet( pev->flags, FL_DUCKING ) ) // crouching + strcpy( szAnim, "crouch_shoot_" ); + else + strcpy( szAnim, "ref_shoot_" ); + strcat( szAnim, m_szAnimExtention ); + animDesired = LookupSequence( szAnim ); + if (animDesired == -1) + animDesired = 0; + + if ( pev->sequence != animDesired || !m_fSequenceLoops ) + { + pev->frame = 0; + } + + if (!m_fSequenceLoops) + { + pev->effects |= EF_NOINTERP; + } + + m_Activity = m_IdealActivity; + + pev->sequence = animDesired; + ResetSequenceInfo( ); + break; + + case ACT_WALK: + if (m_Activity != ACT_RANGE_ATTACK1 || m_fSequenceFinished) + { + if ( FBitSet( pev->flags, FL_DUCKING ) ) // crouching + strcpy( szAnim, "crouch_aim_" ); + else + strcpy( szAnim, "ref_aim_" ); + strcat( szAnim, m_szAnimExtention ); + animDesired = LookupSequence( szAnim ); + if (animDesired == -1) + animDesired = 0; + m_Activity = ACT_WALK; + } + else + { + animDesired = pev->sequence; + } + } + + if ( FBitSet( pev->flags, FL_DUCKING ) ) + { + if ( speed == 0) + { + pev->gaitsequence = LookupActivity( ACT_CROUCHIDLE ); + // pev->gaitsequence = LookupActivity( ACT_CROUCH ); + } + else + { + pev->gaitsequence = LookupActivity( ACT_CROUCH ); + } + } + else if ( speed > 220 ) + { + pev->gaitsequence = LookupActivity( ACT_RUN ); + } + else if (speed > 0) + { + pev->gaitsequence = LookupActivity( ACT_WALK ); + } + else + { + // pev->gaitsequence = LookupActivity( ACT_WALK ); + pev->gaitsequence = LookupSequence( "deep_idle" ); + } + + + // Already using the desired animation? + if (pev->sequence == animDesired) + return; + + //ALERT( at_console, "Set animation to %d\n", animDesired ); + // Reset to first frame of desired animation + pev->sequence = animDesired; + pev->frame = 0; + ResetSequenceInfo( ); +} + + +/* +=========== +WaterMove +============ +*/ +#define AIRTIME 12 // lung full of air lasts this many seconds + +void CBasePlayer::WaterMove() +{ + int air; + + if (pev->movetype == MOVETYPE_NOCLIP) + return; + + if (pev->health < 0) + return; + + // waterlevel 0 - not in water + // waterlevel 1 - feet in water + // waterlevel 2 - waist in water + // waterlevel 3 - head in water + + if (pev->waterlevel != 3 || pev->watertype & MASK_WATER ) + { + // not underwater + + // play 'up for air' sound + if (pev->air_finished < gpGlobals->time) + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_wade1.wav", 1, ATTN_NORM); + else if (pev->air_finished < gpGlobals->time + 9) + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_wade2.wav", 1, ATTN_NORM); + + pev->air_finished = gpGlobals->time + AIRTIME; + pev->dmg = 2; + + // if we took drowning damage, give it back slowly + if (m_idrowndmg > m_idrownrestored) + { + // set drowning damage bit. hack - dmg_drownrecover actually + // makes the time based damage code 'give back' health over time. + // make sure counter is cleared so we start count correctly. + + // NOTE: this actually causes the count to continue restarting + // until all drowning damage is healed. + + m_bitsDamageType |= DMG_DROWNRECOVER; + m_bitsDamageType &= ~DMG_DROWN; + m_rgbTimeBasedDamage[itbd_DrownRecover] = 0; + } + + } + else if (pev->watertype & MASK_WATER ) // FLYFIELD, FLYFIELD_GRAVITY & FOG aren't really water... + { // fully under water + // stop restoring damage while underwater + m_bitsDamageType &= ~DMG_DROWNRECOVER; + m_rgbTimeBasedDamage[itbd_DrownRecover] = 0; + + if (pev->air_finished < gpGlobals->time) // drown! + { + if (pev->pain_finished < gpGlobals->time) + { + // take drowning damage + pev->dmg += 1; + if (pev->dmg > 5) + pev->dmg = 5; + TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), pev->dmg, DMG_DROWN); + pev->pain_finished = gpGlobals->time + 1; + + // track drowning damage, give it back when + // player finally takes a breath + + m_idrowndmg += pev->dmg; + } + } + else + { + m_bitsDamageType &= ~DMG_DROWN; + } + } + + if (!pev->waterlevel || pev->watertype & MASK_WATER ) + { + if (FBitSet(pev->flags, FL_INWATER)) + { + // play leave water sound + switch (RANDOM_LONG(0,3)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade1.wav", 1, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade2.wav", 1, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade3.wav", 1, ATTN_NORM); break; + case 3: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade4.wav", 1, ATTN_NORM); break; + } + + ClearBits(pev->flags, FL_INWATER); + } + return; + } + + // make bubbles + + air = (int)(pev->air_finished - gpGlobals->time); + if (!RANDOM_LONG(0,0x1f) && RANDOM_LONG(0,AIRTIME-1) >= air) + { + switch (RANDOM_LONG(0,3)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_swim1.wav", 0.8, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_swim2.wav", 0.8, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_swim3.wav", 0.8, ATTN_NORM); break; + case 3: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_swim4.wav", 0.8, ATTN_NORM); break; + } + } + + if( FBitSet( pev->watertype, CONTENTS_LAVA )) // do damage + { + if (pev->dmgtime < gpGlobals->time) + TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), 10 * pev->waterlevel, DMG_BURN); + } + else if( FBitSet( pev->watertype, CONTENTS_SLIME )) // do damage + { + pev->dmgtime = gpGlobals->time + 1; + TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), 4 * pev->waterlevel, DMG_ACID); + } + + if (!FBitSet( pev->flags, FL_INWATER )) + { + // player enter water sound + if( FBitSet( pev->watertype, CONTENTS_WATER )) + { + switch (RANDOM_LONG(0,3)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade1.wav", 1, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade2.wav", 1, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade3.wav", 1, ATTN_NORM); break; + case 3: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade4.wav", 1, ATTN_NORM); break; + } + } + + SetBits( pev->flags, FL_INWATER ); + pev->dmgtime = 0; + } + + if( !FBitSet( pev->flags, FL_WATERJUMP )) + pev->velocity = pev->velocity - 0.8 * pev->waterlevel * gpGlobals->frametime * pev->velocity; +} + +// TRUE if the player is attached to a ladder +BOOL CBasePlayer::IsOnLadder( void ) +{ + return ( pev->movetype == MOVETYPE_FLY ); +} + +// +// check for a jump-out-of-water +// +void CBasePlayer::CheckWaterJump( ) +{ + if ( IsOnLadder() ) // Don't water jump if on a ladders? (This prevents the bug where + return; // you bounce off water when you are facing it on a ladder). + + Vector vecStart = pev->origin; + vecStart.z += 8; + + UTIL_MakeVectors(pev->angles); + gpGlobals->v_forward.z = 0; + gpGlobals->v_forward = gpGlobals->v_forward.Normalize(); + + Vector vecEnd = vecStart + gpGlobals->v_forward * 24; + + TraceResult tr; + UTIL_TraceLine(vecStart, vecEnd, ignore_monsters, ENT(pev)/*pentIgnore*/, &tr); + if (tr.flFraction < 1) // solid at waist + { + vecStart.z += pev->maxs.z - 8; + vecEnd = vecStart + gpGlobals->v_forward * 24; + pev->movedir = tr.vecPlaneNormal * -50; + UTIL_TraceLine(vecStart, vecEnd, ignore_monsters, ENT(pev)/*pentIgnore*/, &tr); + if (tr.flFraction == 1) // open at eye level + { + SetBits( pev->flags, FL_WATERJUMP ); + pev->velocity.z = 225; + pev->teleport_time = gpGlobals->time + 2; // safety net + } + } +} + + +void CBasePlayer::PlayerDeathThink(void) +{ + float flForward; + + if (FBitSet(pev->flags, FL_ONGROUND)) + { + flForward = pev->velocity.Length() - 20; + if (flForward <= 0) + pev->velocity = g_vecZero; + else + pev->velocity = flForward * pev->velocity.Normalize(); + } + + if ( HasWeapons() ) + { + // we drop the guns here because weapons that have an area effect and can kill their user + // will sometimes crash coming back from CBasePlayer::Killed() if they kill their owner because the + // player class sometimes is freed. It's safer to manipulate the weapons once we know + // we aren't calling into any of their code anymore through the player pointer. + PackDeadPlayerItems(); + } + + //disable redeemer HUD + MESSAGE_BEGIN( MSG_ONE, gmsgWarHUD, NULL, pev ); + WRITE_BYTE( 0 ); + MESSAGE_END(); + + if (pev->modelindex && (!m_fSequenceFinished) && (pev->deadflag == DEAD_DYING)) + { + StudioFrameAdvance( ); + + m_iRespawnFrames++; // Note, these aren't necessarily real "frames", so behavior is dependent on # of client movement commands + if ( m_iRespawnFrames < 120 ) // Animations should be no longer than this + return; + } + + // once we're done animating our death and we're on the ground, we want to set movetype to None so our dead body won't do collisions and stuff anymore + // this prevents a bug where the dead body would go to a player's head if he walked over it while the dead player was clicking their button to respawn + if ( pev->movetype != MOVETYPE_NONE && FBitSet(pev->flags, FL_ONGROUND) ) + pev->movetype = MOVETYPE_NONE; + + if (pev->deadflag == DEAD_DYING) + pev->deadflag = DEAD_DEAD; + + StopAnimation(); + + pev->effects |= EF_NOINTERP; + pev->framerate = 0.0; + + BOOL fAnyButtonDown = (pev->button & ~IN_SCORE ); + + // wait for all buttons released + if (pev->deadflag == DEAD_DEAD) + { + if (fAnyButtonDown) + return; + + if ( g_pGameRules->FPlayerCanRespawn( this ) ) + { + m_fDeadTime = gpGlobals->time; + pev->deadflag = DEAD_RESPAWNABLE; + } + + return; + } + +// if the player has been dead for one second longer than allowed by forcerespawn, +// forcerespawn isn't on. Send the player off to an intermission camera until they +// choose to respawn. + if ( g_pGameRules->IsMultiplayer() && ( gpGlobals->time > (m_fDeadTime + 6) ) && !(m_afPhysicsFlags & PFLAG_OBSERVER) ) + { + // go to dead camera. + StartDeathCam(); + } + +// wait for any button down, or mp_forcerespawn is set and the respawn time is up + if (!fAnyButtonDown + && !( g_pGameRules->IsMultiplayer() && CVAR_GET_FLOAT( "mp_forcerespawn" ) > 0 && (gpGlobals->time > (m_fDeadTime + 5))) ) + return; + + pev->button = 0; + m_iRespawnFrames = 0; + + //ALERT(at_console, "Respawn\n"); + + respawn(pev, !(m_afPhysicsFlags & PFLAG_OBSERVER) );// don't copy a corpse if we're in deathcam. + DontThink(); +} + +//========================================================= +// StartDeathCam - find an intermission spot and send the +// player off into observer mode +//========================================================= +void CBasePlayer::StartDeathCam( void ) +{ + CBaseEntity *pSpot, *pNewSpot; + int iRand; + + if ( pev->view_ofs == g_vecZero ) + { + // don't accept subsequent attempts to StartDeathCam() + return; + } + + pSpot = UTIL_FindEntityByClassname( NULL, "info_intermission"); + + if ( pSpot ) + { + // at least one intermission spot in the world. + iRand = RANDOM_LONG( 0, 3 ); + + while ( iRand > 0 ) + { + pNewSpot = UTIL_FindEntityByTargetname( pSpot, "info_intermission"); + + if ( pNewSpot ) + { + pSpot = pNewSpot; + } + + iRand--; + } + + CopyToBodyQue( pev ); + StartObserver( pSpot->pev->origin, pSpot->pev->v_angle ); + } + else + { + // no intermission spot. Push them up in the air, looking down at their corpse + TraceResult tr; + CopyToBodyQue( pev ); + UTIL_TraceLine( pev->origin, pev->origin + Vector( 0, 0, 128 ), ignore_monsters, edict(), &tr ); + StartObserver( tr.vecEndPos, UTIL_VecToAngles( tr.vecEndPos - pev->origin ) ); + return; + } +} + +void CBasePlayer::StartObserver( Vector vecPosition, Vector vecViewAngle ) +{ + m_afPhysicsFlags |= PFLAG_OBSERVER; + + pev->view_ofs = g_vecZero; + pev->angles = pev->v_angle = vecViewAngle; + pev->fixangle = TRUE; + pev->solid = SOLID_NOT; + pev->takedamage = DAMAGE_NO; + pev->movetype = MOVETYPE_NONE; + pev->modelindex = 0; + UTIL_SetOrigin( this, vecPosition ); +} + +// +// PlayerUse - handles USE keypress +// +#define PLAYER_SEARCH_RADIUS (float)64 + +void CBasePlayer::PlayerUse ( void ) +{ + // Was use pressed or released? + if ( ! ((pev->button | m_afButtonPressed | m_afButtonReleased) & IN_USE) ) + return; + + // Hit Use on a train? + if ( m_afButtonPressed & IN_USE ) + { + if ( m_pTank != NULL ) + { + // Stop controlling the tank + // TODO: Send HUD Update + m_pTank->Use( this, this, USE_OFF, 0 ); + m_pTank = NULL; + return; + } + else if (m_pMonitor != NULL ) + { + m_pMonitor->Use( this, this, USE_SET, 1 ); + m_pMonitor = NULL; + return; + } + else + { + if ( m_afPhysicsFlags & PFLAG_ONTRAIN ) + { + m_afPhysicsFlags &= ~PFLAG_ONTRAIN; + m_iTrain = TRAIN_NEW|TRAIN_OFF; + return; + } + else + { // Start controlling the train! + CBaseEntity *pTrain = CBaseEntity::Instance( pev->groundentity ); + + if ( pTrain && !(pev->button & IN_JUMP) && FBitSet(pev->flags, FL_ONGROUND) && (pTrain->ObjectCaps() & FCAP_DIRECTIONAL_USE) && pTrain->OnControls(pev) ) + { + m_afPhysicsFlags |= PFLAG_ONTRAIN; + m_iTrain = TrainSpeed(pTrain->pev->speed, pTrain->pev->impulse); + m_iTrain |= TRAIN_NEW; + EMIT_SOUND( ENT(pev), CHAN_ITEM, "plats/train_use1.wav", 0.8, ATTN_NORM); + return; + } + } + } + } + + CBaseEntity *pObject = NULL; + CBaseEntity *pClosest = NULL; + Vector vecLOS; + float flMaxDot = VIEW_FIELD_NARROW; + float flDot; + TraceResult tr; + int caps; + + UTIL_MakeVectors ( pev->v_angle );// so we know which way we are facing + + //LRC- try to get an exact entity to use. + // (is this causing "use-buttons-through-walls" problems? Surely not!) + UTIL_TraceLine( pev->origin + pev->view_ofs, pev->origin + pev->view_ofs + (gpGlobals->v_forward * PLAYER_SEARCH_RADIUS), dont_ignore_monsters, ENT(pev), &tr ); + if (tr.pHit) + { + pObject = CBaseEntity::Instance(tr.pHit); + if (!pObject || !(pObject->ObjectCaps() & (FCAP_IMPULSE_USE | FCAP_CONTINUOUS_USE | FCAP_ONOFF_USE))) + { + pObject = NULL; + } + } + + if (!pObject) //LRC- couldn't find a direct solid object to use, try the normal method + { + while ((pObject = UTIL_FindEntityInSphere( pObject, pev->origin, PLAYER_SEARCH_RADIUS )) != NULL) + { + caps = pObject->ObjectCaps(); + if (caps & (FCAP_IMPULSE_USE | FCAP_CONTINUOUS_USE | FCAP_ONOFF_USE) && !(caps & FCAP_ONLYDIRECT_USE)) //LRC - we can't see 'direct use' entities in this section + { + // !!!PERFORMANCE- should this check be done on a per case basis AFTER we've determined that + // this object is actually usable? This dot is being done for every object within PLAYER_SEARCH_RADIUS + // when player hits the use key. How many objects can be in that area, anyway? (sjb) + vecLOS = (VecBModelOrigin( pObject->pev ) - (pev->origin + pev->view_ofs)); + +// ALERT(at_console, "absmin %f %f %f, absmax %f %f %f, mins %f %f %f, maxs %f %f %f, size %f %f %f\n", pObject->pev->absmin.x, pObject->pev->absmin.y, pObject->pev->absmin.z, pObject->pev->absmax.x, pObject->pev->absmax.y, pObject->pev->absmax.z, pObject->pev->mins.x, pObject->pev->mins.y, pObject->pev->mins.z, pObject->pev->maxs.x, pObject->pev->maxs.y, pObject->pev->maxs.z, pObject->pev->size.x, pObject->pev->size.y, pObject->pev->size.z);//LRCTEMP + // This essentially moves the origin of the target to the corner nearest the player to test to see + // if it's "hull" is in the view cone + vecLOS = UTIL_ClampVectorToBox( vecLOS, pObject->pev->size * 0.5 ); + + flDot = DotProduct (vecLOS , gpGlobals->v_forward); + if (flDot > flMaxDot || vecLOS == g_vecZero ) // LRC - if the player is standing inside this entity, it's also ok to use it. + {// only if the item is in front of the user + pClosest = pObject; + flMaxDot = flDot; +// ALERT( at_console, "%s : %f\n", STRING( pObject->pev->classname ), flDot ); + } +// ALERT( at_console, "%s : %f\n", STRING( pObject->pev->classname ), flDot ); + } + } + pObject = pClosest; + } + + // Found an object + if (pObject ) + { + //!!!UNDONE: traceline here to prevent USEing buttons through walls + caps = pObject->ObjectCaps(); + + if ( m_afButtonPressed & IN_USE ) + EMIT_SOUND( ENT(pev), CHAN_ITEM, "common/wpn_select.wav", 0.4, ATTN_NORM); + + if ( ( (pev->button & IN_USE) && (caps & FCAP_CONTINUOUS_USE) ) || + ( (m_afButtonPressed & IN_USE) && (caps & (FCAP_IMPULSE_USE|FCAP_ONOFF_USE)) ) ) + { + if ( caps & FCAP_CONTINUOUS_USE ) + m_afPhysicsFlags |= PFLAG_USING; + + pObject->Use( this, this, USE_SET, 1 ); + } + // UNDONE: Send different USE codes for ON/OFF. Cache last ONOFF_USE object to send 'off' if you turn away + // (actually, nothing uses on/off. They're either continuous - rechargers and momentary + // buttons - or they're impulse - buttons, doors, tanks, trains, etc.) --LRC + else if ( (m_afButtonReleased & IN_USE) && (pObject->ObjectCaps() & FCAP_ONOFF_USE) ) // BUGBUG This is an "off" use + { + pObject->Use( this, this, USE_SET, 0 ); + } + } + else + { + if ( m_afButtonPressed & IN_USE ) + EMIT_SOUND( ENT(pev), CHAN_ITEM, "common/wpn_denyselect.wav", 0.4, ATTN_NORM); + } +} + + + +void CBasePlayer :: Jump( void ) +{ + float flScale; + Vector vecWallCheckDir;// direction we're tracing a line to find a wall when walljumping + Vector vecAdjustedVelocity; + Vector vecSpot; + TraceResult tr; + + if( FBitSet( pev->flags, FL_WATERJUMP )) + return; + + if( pev->waterlevel >= 2 && !FBitSet( pev->watertype, CONTENTS_FOG )) + { + if( pev->watertype & CONTENTS_WATER ) + flScale = 100; + else if( pev->watertype & CONTENTS_SLIME ) + flScale = 80; + else flScale = 50; + pev->velocity.z = flScale; + + // play swiming sound + if (m_flSwimTime < gpGlobals->time) + { + m_flSwimTime = gpGlobals->time + 1; + switch (RANDOM_LONG(0,3)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade1.wav", 1, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade2.wav", 1, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade3.wav", 1, ATTN_NORM); break; + case 3: EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade4.wav", 1, ATTN_NORM); break; + } + } + return; + } + + // jump velocity is sqrt( height * gravity * 2) + + // If this isn't the first frame pressing the jump button, break out. + if ( !FBitSet( m_afButtonPressed, IN_JUMP ) ) + return; // don't pogo stick + + if ( !(pev->flags & FL_ONGROUND) || !pev->groundentity ) + { + return; + } + + // many features in this function use v_forward, so makevectors now. + UTIL_MakeVectors (pev->angles); + + ClearBits(pev->flags, FL_ONGROUND);// don't stairwalk + + SetAnimation( PLAYER_JUMP ); + + // play step sound for current texture at full volume + + PlayStepSound(MapTextureTypeStepType(m_chTextureType), 1.0); + + if ( FBitSet( pev->flags, FL_DUCKING ) || FBitSet(m_afPhysicsFlags, PFLAG_DUCKING) ) + { + if ( m_fLongJump && (pev->button & IN_DUCK) && ( gpGlobals->time - m_flDuckTime < 1 ) && pev->velocity.Length() > 50 ) + { + Msg( "LongJump!\n" ); + + // play longjump 'servo' sound + if ( RANDOM_LONG(0,1) ) EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain2.wav", 1, ATTN_NORM); + else EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_pain4.wav", 1, ATTN_NORM); + + pev->punchangle.x = -5; + pev->velocity = gpGlobals->v_forward * (PLAYER_LONGJUMP_SPEED * 1.6); + pev->velocity.z = sqrt( 2 * 800 * 56.0 ); // jump 56 units + SetAnimation( PLAYER_SUPERJUMP ); + } + else + { // ducking jump + pev->velocity.z = sqrt( 2 * 800 * 45.0 ); // jump 45 units + } + } + else + { + pev->velocity.z = sqrt( 2 * 800 * 45.0 ); // jump 45 units + } +} + + + +// This is a glorious hack to find free space when you've crouched into some solid space +// Our crouching collisions do not work correctly for some reason and this is easier +// than fixing the problem :( +void FixPlayerCrouchStuck( edict_t *pPlayer ) +{ + TraceResult trace; + + // Move up as many as 18 pixels if the player is stuck. + for ( int i = 0; i < 18; i++ ) + { + UTIL_TraceHull( pPlayer->v.origin, pPlayer->v.origin, dont_ignore_monsters, head_hull, pPlayer, &trace ); + if ( trace.fStartSolid ) + pPlayer->v.origin.z ++; + else + break; + } +} + +// It takes 0.4 seconds to duck +#define TIME_TO_DUCK 0.4 + +// This is a little more complicated now than it used to be, but I think for the better - +// The player's origin no longer moves when ducking. If you start ducking while standing on ground, +// your view offset is non-linearly blended to the ducking position and then your hull is changed. +// In air, duck works just like it used to - you pick up your feet (more like tucking). +// PFLAG_DUCKING was added to track the state where the player is in the process of ducking, but hasn't +// reached the crouched position yet. +// +// A few of nice side effects of this are: +// a) On ground status does not change when ducking (good on trains, etc) +// b) origin stays constant +// c) Superjump can begin while still in run animation (nice when looking at others in multiplayer) +// +void CBasePlayer::Duck( ) +{ + float time, duckFraction; + + if (pev->button & IN_DUCK) + { + if(( m_afButtonPressed & IN_DUCK ) && !(pev->flags & FL_DUCKING) ) + { + m_flDuckTime = gpGlobals->time; + SetBits(m_afPhysicsFlags,PFLAG_DUCKING); // We are in the process of ducking + } + time = (gpGlobals->time - m_flDuckTime); + + // if we're not already done ducking + if ( FBitSet( m_afPhysicsFlags, PFLAG_DUCKING ) ) + { + // Finish ducking immediately if duck time is over or not on ground + if ( time >= TIME_TO_DUCK || !(pev->flags & FL_ONGROUND) ) + { + // HACKHACK - Fudge for collision bug - no time to fix this properly + if ( pev->flags & FL_ONGROUND ) + { + pev->origin = pev->origin - (VEC_DUCK_HULL_MIN - VEC_HULL_MIN); + FixPlayerCrouchStuck( edict() ); + } + + + UTIL_SetOrigin( this, pev->origin ); + + UTIL_SetSize(pev, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX); + pev->view_ofs = VEC_DUCK_VIEW; + SetBits( pev->flags, FL_DUCKING ); // Hull is duck hull + ClearBits( m_afPhysicsFlags, PFLAG_DUCKING ); // Done ducking (don't ease-in when the player hits the ground) + } + else + { + // Calc parametric time + duckFraction = UTIL_SplineFraction( time, (1.0/TIME_TO_DUCK) ); + pev->view_ofs = ((VEC_DUCK_VIEW - (VEC_DUCK_HULL_MIN - VEC_HULL_MIN)) * duckFraction) + (VEC_VIEW * (1-duckFraction)); + } + } + SetAnimation( PLAYER_WALK ); + } + else + { + TraceResult trace; + Vector newOrigin = pev->origin; + + if ( pev->flags & FL_ONGROUND ) + newOrigin = newOrigin + (VEC_DUCK_HULL_MIN - VEC_HULL_MIN); + + UTIL_TraceHull( newOrigin, newOrigin, dont_ignore_monsters, human_hull, ENT(pev), &trace ); + + if ( !trace.fStartSolid ) + { + ClearBits( pev->flags, FL_DUCKING ); + ClearBits( m_afPhysicsFlags, PFLAG_DUCKING ); + pev->view_ofs = VEC_VIEW; + UTIL_SetSize(pev, VEC_HULL_MIN, VEC_HULL_MAX); + pev->origin = newOrigin; + } + } +} + + +// +// ID's player as such. +// +int CBasePlayer::Classify ( void ) +{ + return CLASS_PLAYER; +} + + +void CBasePlayer::AddPoints( int score, BOOL bAllowNegativeScore ) +{ + // Positive score always adds + if ( score < 0 ) + { + if ( !bAllowNegativeScore ) + { + if ( pev->frags < 0 ) // Can't go more negative + return; + + if ( -score > pev->frags ) // Will this go negative? + { + score = -pev->frags; // Sum will be 0 + } + } + } + + pev->frags += score; + + MESSAGE_BEGIN( MSG_ALL, gmsgScoreInfo ); + WRITE_BYTE( ENTINDEX(edict()) ); + WRITE_SHORT( pev->frags ); + WRITE_SHORT( m_iDeaths ); + WRITE_SHORT( 0 ); + WRITE_SHORT( g_pGameRules->GetTeamIndex( m_szTeamName ) + 1 ); + MESSAGE_END(); +} + + +void CBasePlayer::AddPointsToTeam( int score, BOOL bAllowNegativeScore ) +{ + int index = entindex(); + + for ( int i = 1; i <= gpGlobals->maxClients; i++ ) + { + CBaseEntity *pPlayer = UTIL_PlayerByIndex( i ); + + if ( pPlayer && i != index ) + { + if ( g_pGameRules->PlayerRelationship( this, pPlayer ) == GR_TEAMMATE ) + { + pPlayer->AddPoints( score, bAllowNegativeScore ); + } + } + } +} + +//Player ID +void CBasePlayer::InitStatusBar() +{ + m_flStatusBarDisappearDelay = 0; + m_SbarString1[0] = m_SbarString0[0] = 0; +} + +void CBasePlayer::UpdateStatusBar() +{ + int newSBarState[ SBAR_END ]; + char sbuf0[ SBAR_STRING_SIZE ]; + char sbuf1[ SBAR_STRING_SIZE ]; + + memset( newSBarState, 0, sizeof(newSBarState) ); + strcpy( sbuf0, m_SbarString0 ); + strcpy( sbuf1, m_SbarString1 ); + + // Find an ID Target + TraceResult tr; + UTIL_MakeVectors( pev->v_angle + pev->punchangle ); + Vector vecSrc = EyePosition(); + Vector vecEnd = vecSrc + (gpGlobals->v_forward * MAX_ID_RANGE); + UTIL_TraceLine( vecSrc, vecEnd, dont_ignore_monsters, edict(), &tr); + + if (tr.flFraction != 1.0) + { + if ( !FNullEnt( tr.pHit ) ) + { + CBaseEntity *pEntity = CBaseEntity::Instance( tr.pHit ); + if (pEntity->Classify() == CLASS_PLAYER ) + { + newSBarState[ SBAR_ID_TARGETNAME ] = ENTINDEX( pEntity->edict() ); + strcpy( sbuf1, "1 %p1\n2 Health: %i2%%\n3 Armor: %i3%%" ); + + // allies and medics get to see the targets health + if ( g_pGameRules->PlayerRelationship( this, pEntity ) == GR_TEAMMATE ) + { + newSBarState[ SBAR_ID_TARGETHEALTH ] = 100 * (pEntity->pev->health / pEntity->pev->max_health); + newSBarState[ SBAR_ID_TARGETARMOR ] = pEntity->pev->armorvalue; //No need to get it % based since 100 it's the max. + } + + m_flStatusBarDisappearDelay = gpGlobals->time + 1.0; + } + } + else if ( m_flStatusBarDisappearDelay > gpGlobals->time ) + { + // hold the values for a short amount of time after viewing the object + newSBarState[ SBAR_ID_TARGETNAME ] = m_izSBarState[ SBAR_ID_TARGETNAME ]; + newSBarState[ SBAR_ID_TARGETHEALTH ] = m_izSBarState[ SBAR_ID_TARGETHEALTH ]; + newSBarState[ SBAR_ID_TARGETARMOR ] = m_izSBarState[ SBAR_ID_TARGETARMOR ]; + } + } + + BOOL bForceResend = FALSE; + + if ( strcmp( sbuf0, m_SbarString0 ) ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgStatusText, NULL, pev ); + WRITE_BYTE( 0 ); + WRITE_STRING( sbuf0 ); + MESSAGE_END(); + + strcpy( m_SbarString0, sbuf0 ); + + // make sure everything's resent + bForceResend = TRUE; + } + + if ( strcmp( sbuf1, m_SbarString1 ) ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgStatusText, NULL, pev ); + WRITE_BYTE( 1 ); + WRITE_STRING( sbuf1 ); + MESSAGE_END(); + + strcpy( m_SbarString1, sbuf1 ); + + // make sure everything's resent + bForceResend = TRUE; + } + + // Check values and send if they don't match + for (int i = 1; i < SBAR_END; i++) + { + if ( newSBarState[i] != m_izSBarState[i] || bForceResend ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgStatusValue, NULL, pev ); + WRITE_BYTE( i ); + WRITE_SHORT( newSBarState[i] ); + MESSAGE_END(); + + m_izSBarState[i] = newSBarState[i]; + } + } +} + + + + + + +// play a footstep if it's time - this will eventually be frame-based. not time based. + +#define STEP_CONCRETE 0 // default step sound +#define STEP_METAL 1 // metal floor +#define STEP_DIRT 2 // dirt, sand, rock +#define STEP_VENT 3 // ventillation duct +#define STEP_GRATE 4 // metal grating +#define STEP_TILE 5 // floor tiles +#define STEP_SLOSH 6 // shallow liquid puddle +#define STEP_WADE 7 // wading in liquid +#define STEP_LADDER 8 // climbing ladder + +// Play correct step sound for material we're on or in + +void CBasePlayer :: PlayStepSound(int step, float fvol) +{ + static int iSkipStep = 0; + + if ( !g_pGameRules->PlayFootstepSounds( this, fvol ) ) + return; + + // irand - 0,1 for right foot, 2,3 for left foot + // used to alternate left and right foot + int irand = RANDOM_LONG(0,1) + (m_iStepLeft * 2); + + m_iStepLeft = !m_iStepLeft; + + switch (step) + { + default: + case STEP_CONCRETE: + switch (irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_step4.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_METAL: + switch(irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_metal1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_metal3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_metal2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_metal4.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_DIRT: + switch(irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_dirt1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_dirt3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_dirt2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_dirt4.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_VENT: + switch(irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_duct1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_duct3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_duct2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_duct4.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_GRATE: + switch(irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_grate1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_grate3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_grate2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_grate4.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_TILE: + if (!RANDOM_LONG(0,4)) + irand = 4; + switch(irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_tile1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_tile3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_tile2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_tile4.wav", fvol, ATTN_NORM); break; + case 4: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_tile5.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_SLOSH: + switch(irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_slosh1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_slosh3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_slosh2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_slosh4.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_WADE: + if ( iSkipStep == 0 ) + { + iSkipStep++; + break; + } + + if ( iSkipStep++ == 3 ) + { + iSkipStep = 0; + } + + switch (irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_wade1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_wade2.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_wade3.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_wade4.wav", fvol, ATTN_NORM); break; + } + break; + case STEP_LADDER: + switch(irand) + { + // right foot + case 0: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_ladder1.wav", fvol, ATTN_NORM); break; + case 1: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_ladder3.wav", fvol, ATTN_NORM); break; + // left foot + case 2: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_ladder2.wav", fvol, ATTN_NORM); break; + case 3: EMIT_SOUND( ENT(pev), CHAN_BODY, "player/pl_ladder4.wav", fvol, ATTN_NORM); break; + } + break; + } +} + +// Simple mapping from texture type character to step type + +int MapTextureTypeStepType(char chTextureType) +{ + switch (chTextureType) + { + default: + case CHAR_TEX_CONCRETE: return STEP_CONCRETE; + case CHAR_TEX_METAL: return STEP_METAL; + case CHAR_TEX_DIRT: return STEP_DIRT; + case CHAR_TEX_VENT: return STEP_VENT; + case CHAR_TEX_GRATE: return STEP_GRATE; + case CHAR_TEX_TILE: return STEP_TILE; + case CHAR_TEX_SLOSH: return STEP_SLOSH; + } +} + +// Play left or right footstep based on material player is on or in + +void CBasePlayer :: UpdateStepSound( void ) +{ + int fWalking; + float fvol; + char szbuffer[64]; + const char *pTextureName; + Vector start, end; + float rgfl1[3]; + float rgfl2[3]; + Vector knee; + Vector feet; + Vector center; + float height; + float speed; + float velrun; + float velwalk; + float flduck; + int fLadder; + int step; + + if (gpGlobals->time <= m_flTimeStepSound) + return; + + if( pev->flags & FL_FROZEN ) + return; + + speed = pev->velocity.Length(); + + // determine if we are on a ladder + fLadder = IsOnLadder(); + + // UNDONE: need defined numbers for run, walk, crouch, crouch run velocities!!!! + if( FBitSet( pev->flags, FL_DUCKING ) || fLadder ) + { + velwalk = 60; // These constants should be based on cl_movespeedkey * cl_forwardspeed somehow + velrun = 80; // UNDONE: Move walking to server + flduck = 0.1; + } + else + { + velwalk = 120; + velrun = 210; + flduck = 0.0; + } + + // ALERT (at_console, "vel: %f\n", vecVel.Length()); + + // if we're on a ladder or on the ground, and we're moving fast enough, + // play step sound. Also, if m_flTimeStepSound is zero, get the new + // sound right away - we just started moving in new level. + + if ((fLadder || FBitSet (pev->flags, FL_ONGROUND)) && pev->velocity != g_vecZero + && (speed >= velwalk || !m_flTimeStepSound)) + { + SetAnimation( PLAYER_WALK ); + + fWalking = speed < velrun; + + center = knee = feet = (pev->absmin + pev->absmax) * 0.5; + height = pev->absmax.z - pev->absmin.z; + + knee.z = pev->absmin.z + height * 0.2; + feet.z = pev->absmin.z; + + // find out what we're stepping in or on... + if( fLadder ) + { + step = STEP_LADDER; + fvol = 0.35; + m_flTimeStepSound = gpGlobals->time + 0.35; + } + else if( UTIL_PointContents( knee ) & MASK_WATER ) + { + step = STEP_WADE; + fvol = 0.65; + m_flTimeStepSound = gpGlobals->time + 0.6; + } + else if( UTIL_PointContents( feet ) & MASK_WATER ) + { + step = STEP_SLOSH; + fvol = fWalking ? 0.2 : 0.5; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + } + else + { + // find texture under player, if different from current texture, + // get material type + + start = end = center; // center point of player BB + start.z = end.z = pev->absmin.z; // copy zmin + start.z += 4.0; // extend start up + end.z -= 24.0; // extend end down + + start.CopyToArray(rgfl1); + end.CopyToArray(rgfl2); + + pTextureName = TRACE_TEXTURE( ENT( pev->groundentity), rgfl1, rgfl2 ); + if ( pTextureName ) + { + // strip leading '-0' or '{' or '!' + if (*pTextureName == '-') + pTextureName += 2; + if (*pTextureName == '{' || *pTextureName == '!') + pTextureName++; + + if (_strnicmp(pTextureName, m_szTextureName, CBTEXTURENAMEMAX-1)) + { + // current texture is different from texture player is on... + // set current texture + strcpy(szbuffer, pTextureName); + szbuffer[CBTEXTURENAMEMAX - 1] = 0; + strcpy(m_szTextureName, szbuffer); + + // ALERT ( at_aiconsole, "texture: %s\n", m_szTextureName ); + + // get texture type + m_chTextureType = TEXTURETYPE_Find(m_szTextureName); + } + } + + step = MapTextureTypeStepType(m_chTextureType); + + switch (m_chTextureType) + { + default: + case CHAR_TEX_CONCRETE: + fvol = fWalking ? 0.2 : 0.5; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + break; + + case CHAR_TEX_METAL: + fvol = fWalking ? 0.2 : 0.5; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + break; + + case CHAR_TEX_DIRT: + fvol = fWalking ? 0.25 : 0.55; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + break; + + case CHAR_TEX_VENT: + fvol = fWalking ? 0.4 : 0.7; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + break; + + case CHAR_TEX_GRATE: + fvol = fWalking ? 0.2 : 0.5; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + break; + + case CHAR_TEX_TILE: + fvol = fWalking ? 0.2 : 0.5; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + break; + + case CHAR_TEX_SLOSH: + fvol = fWalking ? 0.2 : 0.5; + m_flTimeStepSound = fWalking ? gpGlobals->time + 0.4 : gpGlobals->time + 0.3; + break; + } + } + + m_flTimeStepSound += flduck; // slower step time if ducking + + // play the sound + + // 35% volume if ducking + if ( pev->flags & FL_DUCKING ) + fvol *= 0.35; + + PlayStepSound(step, fvol); + } +} + + +#define CLIMB_SHAKE_FREQUENCY 22 // how many frames in between screen shakes when climbing +#define MAX_CLIMB_SPEED 200 // fastest vertical climbing speed possible +#define CLIMB_SPEED_DEC 15 // climbing deceleration rate +#define CLIMB_PUNCH_X -7 // how far to 'punch' client X axis when climbing +#define CLIMB_PUNCH_Z 7 // how far to 'punch' client Z axis when climbing + +void CBasePlayer::PreThink(void) +{ + int buttonsChanged = (m_afButtonLast ^ pev->button); // These buttons have changed this frame + + // Debounced button codes for pressed/released + // UNDONE: Do we need auto-repeat? + m_afButtonPressed = buttonsChanged & pev->button; // The ones that changed and are now down are "pressed" + m_afButtonReleased = buttonsChanged & (~pev->button); // The ones that changed and aren't down are "released" + + g_pGameRules->PlayerThink( this ); + + if ( g_fGameOver ) + return; // intermission or finale + + UTIL_MakeVectors(pev->v_angle); // is this still used? + + ItemPreFrame( ); + WaterMove(); + + if ( g_pGameRules && g_pGameRules->FAllowFlashlight() ) + m_iHideHUD &= ~HIDEHUD_FLASHLIGHT; + else + m_iHideHUD |= HIDEHUD_FLASHLIGHT; + + // JOHN: checks if new client data (for HUD and view control) needs to be sent to the client + UpdateClientData(); + + CheckTimeBasedDamage(); + + CheckSuitUpdate(); + + if (pev->waterlevel == 2) + CheckWaterJump(); + else if ( pev->waterlevel == 0 ) + { + pev->flags &= ~FL_WATERJUMP; // Clear this if out of water + } + + if (pev->deadflag >= DEAD_DYING) + { + PlayerDeathThink(); + return; + } + + // So the correct flags get sent to client asap. + // + if ( m_afPhysicsFlags & PFLAG_ONTRAIN ) + pev->flags |= FL_ONTRAIN; + else + pev->flags &= ~FL_ONTRAIN; + + // Train speed control + if ( m_afPhysicsFlags & PFLAG_ONTRAIN ) + { + CBaseEntity *pTrain = CBaseEntity::Instance( pev->groundentity ); + float vel; + + if ( !pTrain ) + { + TraceResult trainTrace; + // Maybe this is on the other side of a level transition + UTIL_TraceLine( pev->origin, pev->origin + Vector(0,0,-38), ignore_monsters, ENT(pev), &trainTrace ); + + // HACKHACK - Just look for the func_tracktrain classname + if ( trainTrace.flFraction != 1.0 && trainTrace.pHit ) + pTrain = CBaseEntity::Instance( trainTrace.pHit ); + + + if ( !pTrain || !(pTrain->ObjectCaps() & FCAP_DIRECTIONAL_USE) || !pTrain->OnControls(pev) ) + { + //ALERT( at_error, "In train mode with no train!\n" ); + m_afPhysicsFlags &= ~PFLAG_ONTRAIN; + m_iTrain = TRAIN_NEW|TRAIN_OFF; + return; + } + } + else if ( !FBitSet( pev->flags, FL_ONGROUND ) || FBitSet( pTrain->pev->spawnflags, 0x2 ) || (pev->button & (IN_MOVELEFT|IN_MOVERIGHT) ) ) + { + // Turn off the train if you jump, strafe, or the train controls go dead + m_afPhysicsFlags &= ~PFLAG_ONTRAIN; + m_iTrain = TRAIN_NEW|TRAIN_OFF; + return; + } + + pev->velocity = g_vecZero; + vel = 0; + if ( m_afButtonPressed & IN_FORWARD ) + { + vel = 1; + pTrain->Use( this, this, USE_SET, (float)vel ); + } + else if ( m_afButtonPressed & IN_BACK ) + { + vel = -1; + pTrain->Use( this, this, USE_SET, (float)vel ); + } + + if (vel) + { + m_iTrain = TrainSpeed(pTrain->pev->speed, pTrain->pev->impulse); + m_iTrain |= TRAIN_ACTIVE|TRAIN_NEW; + } + + } else if (m_iTrain & TRAIN_ACTIVE) + m_iTrain = TRAIN_NEW; // turn off train + + if( pev->button & IN_JUMP ) + { + // If on a ladder, jump off the ladder + // else Jump + Jump(); + } + + // If trying to duck, already ducked, or in the process of ducking + if ((pev->button & IN_DUCK) || FBitSet( pev->flags, FL_DUCKING) || (m_afPhysicsFlags & PFLAG_DUCKING) ) + Duck(); + + // play a footstep if it's time - this will eventually be frame-based. not time based. + + UpdateStepSound(); + + if ( !FBitSet ( pev->flags, FL_ONGROUND ) ) + { + m_flFallVelocity = -pev->velocity.z; + } + + // StudioFrameAdvance( );//!!!HACKHACK!!! Can't be hit by traceline when not animating? + + // Clear out ladder pointer + m_hEnemy = NULL; + + if ( m_afPhysicsFlags & PFLAG_ONBARNACLE ) + { + pev->velocity = g_vecZero; + } +} +/* Time based Damage works as follows: + 1) There are several types of timebased damage: + + #define DMG_PARALYZE (1 << 14) // slows affected creature down + #define DMG_NERVEGAS (1 << 15) // nerve toxins, very bad + #define DMG_POISON (1 << 16) // blood poisioning + #define DMG_RADIATION (1 << 17) // radiation exposure + #define DMG_DROWNRECOVER (1 << 18) // drown recovery + #define DMG_ACID (1 << 19) // toxic chemicals or acid burns + #define DMG_SLOWBURN (1 << 20) // in an oven + #define DMG_SLOWFREEZE (1 << 21) // in a subzero freezer + #define DMG_NUCLEAR (1 << 24) + + 2) A new hit inflicting tbd restarts the tbd counter - each monster has an 8bit counter, + per damage type. The counter is decremented every second, so the maximum time + an effect will last is 255/60 = 4.25 minutes. Of course, staying within the radius + of a damaging effect like fire, nervegas, radiation will continually reset the counter to max. + + 3) Every second that a tbd counter is running, the player takes damage. The damage + is determined by the type of tdb. + Paralyze - 1/2 movement rate, 30 second duration. + Nervegas - 5 points per second, 16 second duration = 80 points max dose. + Poison - 2 points per second, 25 second duration = 50 points max dose. + Radiation - 1 point per second, 50 second duration = 50 points max dose. + Drown - 5 points per second, 2 second duration. + Acid/Chemical - 5 points per second, 10 second duration = 50 points max. + Burn - 10 points per second, 2 second duration. + Freeze - 3 points per second, 10 second duration = 30 points max. + + 4) Certain actions or countermeasures counteract the damaging effects of tbds: + + Armor/Heater/Cooler - Chemical(acid),burn, freeze all do damage to armor power, then to body + - recharged by suit recharger + Air In Lungs - drowning damage is done to air in lungs first, then to body + - recharged by poking head out of water + - 10 seconds if swiming fast + Air In SCUBA - drowning damage is done to air in tanks first, then to body + - 2 minutes in tanks. Need new tank once empty. + Radiation Syringe - Each syringe full provides protection vs one radiation dosage + Antitoxin Syringe - Each syringe full provides protection vs one poisoning (nervegas or poison). + Health kit - Immediate stop to acid/chemical, fire or freeze damage. + Radiation Shower - Immediate stop to radiation damage, acid/chemical or fire damage. + + +*/ + +// If player is taking time based damage, continue doing damage to player - +// this simulates the effect of being poisoned, gassed, dosed with radiation etc - +// anything that continues to do damage even after the initial contact stops. +// Update all time based damage counters, and shut off any that are done. + +// The m_bitsDamageType bit MUST be set if any damage is to be taken. +// This routine will detect the initial on value of the m_bitsDamageType +// and init the appropriate counter. Only processes damage every second. + +//#define PARALYZE_DURATION 30 // number of 2 second intervals to take damage +//#define PARALYZE_DAMAGE 0.0 // damage to take each 2 second interval + +//#define NERVEGAS_DURATION 16 +//#define NERVEGAS_DAMAGE 5.0 + +//#define POISON_DURATION 25 +//#define POISON_DAMAGE 2.0 + +//#define RADIATION_DURATION 50 +//#define RADIATION_DAMAGE 1.0 + +//#define ACID_DURATION 10 +//#define ACID_DAMAGE 5.0 + +//#define SLOWBURN_DURATION 2 +//#define SLOWBURN_DAMAGE 1.0 + +//#define SLOWFREEZE_DURATION 1.0 +//#define SLOWFREEZE_DAMAGE 3.0 + +/* */ + + +void CBasePlayer::CheckTimeBasedDamage() +{ + int i; + BYTE bDuration = 0; + + static float gtbdPrev = 0.0; + + if (!(m_bitsDamageType & DMG_TIMEBASED)) + return; + + // only check for time based damage approx. every 2 seconds + if (abs(gpGlobals->time - m_tbdPrev) < 2.0) + return; + + m_tbdPrev = gpGlobals->time; + + for (i = 0; i < CDMG_TIMEBASED; i++) + { + // make sure bit is set for damage type + if (m_bitsDamageType & (DMG_PARALYZE << i)) + { + switch (i) + { + case itbd_Paralyze: + // UNDONE - flag movement as half-speed + bDuration = PARALYZE_DURATION; + break; + case itbd_NerveGas: +// TakeDamage(pev, pev, NERVEGAS_DAMAGE, DMG_GENERIC); + bDuration = NERVEGAS_DURATION; + break; + case itbd_Poison: + TakeDamage(pev, pev, POISON_DAMAGE, DMG_GENERIC); + bDuration = POISON_DURATION; + break; + case itbd_Radiation: +// TakeDamage(pev, pev, RADIATION_DAMAGE, DMG_GENERIC); + bDuration = RADIATION_DURATION; + break; + case itbd_DrownRecover: + // NOTE: this hack is actually used to RESTORE health + // after the player has been drowning and finally takes a breath + if (m_idrowndmg > m_idrownrestored) + { + int idif = min(m_idrowndmg - m_idrownrestored, 10); + + TakeHealth(idif, DMG_GENERIC); + m_idrownrestored += idif; + } + bDuration = 4; // get up to 5*10 = 50 points back + break; + case itbd_Acid: +// TakeDamage(pev, pev, ACID_DAMAGE, DMG_GENERIC); + bDuration = ACID_DURATION; + break; + case itbd_SlowBurn: +// TakeDamage(pev, pev, SLOWBURN_DAMAGE, DMG_GENERIC); + bDuration = SLOWBURN_DURATION; + break; + case itbd_SlowFreeze: +// TakeDamage(pev, pev, SLOWFREEZE_DAMAGE, DMG_GENERIC); + bDuration = SLOWFREEZE_DURATION; + break; + default: + bDuration = 0; + } + + if (m_rgbTimeBasedDamage[i]) + { + // decrement damage duration, detect when done. + if (!m_rgbTimeBasedDamage[i] || --m_rgbTimeBasedDamage[i] == 0) + { + m_rgbTimeBasedDamage[i] = 0; + // if we're done, clear damage bits + m_bitsDamageType &= ~(DMG_PARALYZE << i); + } + } + else + // first time taking this damage type - init damage duration + m_rgbTimeBasedDamage[i] = bDuration; + } + } +} + +/* +THE POWER SUIT + +The Suit provides 3 main functions: Protection, Notification and Augmentation. +Some functions are automatic, some require power. +The player gets the suit shortly after getting off the train in C1A0 and it stays +with him for the entire game. + +Protection + + Heat/Cold + When the player enters a hot/cold area, the heating/cooling indicator on the suit + will come on and the battery will drain while the player stays in the area. + After the battery is dead, the player starts to take damage. + This feature is built into the suit and is automatically engaged. + Radiation Syringe + This will cause the player to be immune from the effects of radiation for N seconds. Single use item. + Anti-Toxin Syringe + This will cure the player from being poisoned. Single use item. + Health + Small (1st aid kits, food, etc.) + Large (boxes on walls) + Armor + The armor works using energy to create a protective field that deflects a + percentage of damage projectile and explosive attacks. After the armor has been deployed, + it will attempt to recharge itself to full capacity with the energy reserves from the battery. + It takes the armor N seconds to fully charge. + +Notification (via the HUD) + +x Health +x Ammo +x Automatic Health Care + Notifies the player when automatic healing has been engaged. +x Geiger counter + Classic Geiger counter sound and status bar at top of HUD + alerts player to dangerous levels of radiation. This is not visible when radiation levels are normal. +x Poison + Armor + Displays the current level of armor. + +Augmentation + + Reanimation (w/adrenaline) + Causes the player to come back to life after he has been dead for 3 seconds. + Will not work if player was gibbed. Single use. + Long Jump + Used by hitting the ??? key(s). Caused the player to further than normal. + SCUBA + Used automatically after picked up and after player enters the water. + Works for N seconds. Single use. + +Things powered by the battery + + Armor + Uses N watts for every M units of damage. + Heat/Cool + Uses N watts for every second in hot/cold area. + Long Jump + Uses N watts for every jump. + Alien Cloak + Uses N watts for each use. Each use lasts M seconds. + Alien Shield + Augments armor. Reduces Armor drain by one half + +*/ + +// if in range of radiation source, ping geiger counter + +#define GEIGERDELAY 0.25 + +void CBasePlayer :: UpdateGeigerCounter( void ) +{ + BYTE range; + + // delay per update ie: don't flood net with these msgs + if (gpGlobals->time < m_flgeigerDelay) + return; + + m_flgeigerDelay = gpGlobals->time + GEIGERDELAY; + + // send range to radition source to client + + range = (BYTE) (m_flgeigerRange / 4); + + if (range != m_igeigerRangePrev) + { + m_igeigerRangePrev = range; + + MESSAGE_BEGIN( MSG_ONE, gmsgGeigerRange, NULL, pev ); + WRITE_BYTE( range ); + MESSAGE_END(); + } + + // reset counter and semaphore + if (!RANDOM_LONG(0,3)) + m_flgeigerRange = 1000; + +} + +/* +================ +CheckSuitUpdate + +Play suit update if it's time +================ +*/ + +#define SUITUPDATETIME 3.5 +#define SUITFIRSTUPDATETIME 0.1 + +void CBasePlayer::CheckSuitUpdate() +{ + int i; + int isentence = 0; + int isearch = m_iSuitPlayNext; + + // Ignore suit updates if no suit + if (!(m_iHideHUD & ITEM_SUIT)) + return; + + // if in range of radiation source, ping geiger counter + UpdateGeigerCounter(); + + if ( g_pGameRules->IsMultiplayer() ) + { + // don't bother updating HEV voice in multiplayer. + return; + } + + if ( gpGlobals->time >= m_flSuitUpdate && m_flSuitUpdate > 0) + { + // play a sentence off of the end of the queue + for (i = 0; i < CSUITPLAYLIST; i++) + { + if (isentence = m_rgSuitPlayList[isearch]) + break; + + if (++isearch == CSUITPLAYLIST) + isearch = 0; + } + + if (isentence) + { + m_rgSuitPlayList[isearch] = 0; + if (isentence > 0) + { + // play sentence number + + char sentence[CBSENTENCENAME_MAX+1]; + strcpy(sentence, "!"); + strcat(sentence, gszallsentencenames[isentence]); + EMIT_SOUND_SUIT(ENT(pev), sentence); + } + else + { + // play sentence group + EMIT_GROUPID_SUIT(ENT(pev), -isentence); + } + m_flSuitUpdate = gpGlobals->time + SUITUPDATETIME; + } + else + // queue is empty, don't check + m_flSuitUpdate = 0; + } +} + +// add sentence to suit playlist queue. if fgroup is true, then +// name is a sentence group (HEV_AA), otherwise name is a specific +// sentence name ie: !HEV_AA0. If iNoRepeat is specified in +// seconds, then we won't repeat playback of this word or sentence +// for at least that number of seconds. + +void CBasePlayer::SetSuitUpdate(char *name, int fgroup, int iNoRepeatTime) +{ + int i; + int isentence; + int iempty = -1; + + + // Ignore suit updates if no suit + if (!(m_iHideHUD & ITEM_SUIT)) + return; + + if ( g_pGameRules->IsMultiplayer() ) + { + // due to static channel design, etc. We don't play HEV sounds in multiplayer right now. + return; + } + + // if name == NULL, then clear out the queue + + if (!name) + { + for (i = 0; i < CSUITPLAYLIST; i++) + m_rgSuitPlayList[i] = 0; + return; + } + // get sentence or group number + if (!fgroup) + { + isentence = SENTENCEG_Lookup(name, NULL); + if (isentence < 0) + { + ALERT(at_debug,"HEV couldn't find sentence %s\n",name); + return; + } + } + else + // mark group number as negative + isentence = -SENTENCEG_GetIndex(name); + + // check norepeat list - this list lets us cancel + // the playback of words or sentences that have already + // been played within a certain time. + + for (i = 0; i < CSUITNOREPEAT; i++) + { + if (isentence == m_rgiSuitNoRepeat[i]) + { + // this sentence or group is already in + // the norepeat list + + if (m_rgflSuitNoRepeatTime[i] < gpGlobals->time) + { + // norepeat time has expired, clear it out + m_rgiSuitNoRepeat[i] = 0; + m_rgflSuitNoRepeatTime[i] = 0.0; + iempty = i; + break; + } + else + { + // don't play, still marked as norepeat + return; + } + } + // keep track of empty slot + if (!m_rgiSuitNoRepeat[i]) + iempty = i; + } + + // sentence is not in norepeat list, save if norepeat time was given + + if (iNoRepeatTime) + { + if (iempty < 0) + iempty = RANDOM_LONG(0, CSUITNOREPEAT-1); // pick random slot to take over + m_rgiSuitNoRepeat[iempty] = isentence; + m_rgflSuitNoRepeatTime[iempty] = iNoRepeatTime + gpGlobals->time; + } + + // find empty spot in queue, or overwrite last spot + + m_rgSuitPlayList[m_iSuitPlayNext++] = isentence; + if (m_iSuitPlayNext == CSUITPLAYLIST) + m_iSuitPlayNext = 0; + + if (m_flSuitUpdate <= gpGlobals->time) + { + if (m_flSuitUpdate == 0) + // play queue is empty, don't delay too long before playback + m_flSuitUpdate = gpGlobals->time + SUITFIRSTUPDATETIME; + else + m_flSuitUpdate = gpGlobals->time + SUITUPDATETIME; + } + +} + +/* +================ +CheckPowerups + +Check for turning off powerups + +GLOBALS ASSUMED SET: g_ulModelIndexPlayer +================ +*/ + static void +CheckPowerups(entvars_t *pev) +{ + if (pev->health <= 0) + return; + + pev->modelindex = g_ulModelIndexPlayer; // don't use eyes +} + + +//========================================================= +// UpdatePlayerSound - updates the position of the player's +// reserved sound slot in the sound list. +//========================================================= +void CBasePlayer :: UpdatePlayerSound ( void ) +{ + int iBodyVolume; + int iVolume; + CSound *pSound; + + pSound = CSoundEnt::SoundPointerForIndex( CSoundEnt :: ClientSoundIndex( edict() ) ); + + if ( !pSound ) + { + ALERT ( at_debug, "Client lost reserved sound!\n" ); + return; + } + + pSound->m_iType = bits_SOUND_NONE; + + // now calculate the best target volume for the sound. If the player's weapon + // is louder than his body/movement, use the weapon volume, else, use the body volume. + + if ( FBitSet ( pev->flags, FL_ONGROUND ) ) + { + iBodyVolume = pev->velocity.Length(); + + // clamp the noise that can be made by the body, in case a push trigger, + // weapon recoil, or anything shoves the player abnormally fast. + if ( iBodyVolume > 512 ) + { + iBodyVolume = 512; + } + } + else + { + iBodyVolume = 0; + } + + if ( pev->button & IN_JUMP ) + { + iBodyVolume += 100; + } + +// convert player move speed and actions into sound audible by monsters. + if ( m_iWeaponVolume > iBodyVolume ) + { + m_iTargetVolume = m_iWeaponVolume; + + // OR in the bits for COMBAT sound if the weapon is being louder than the player. + pSound->m_iType |= bits_SOUND_COMBAT; + } + else + { + m_iTargetVolume = iBodyVolume; + } + + // decay weapon volume over time so bits_SOUND_COMBAT stays set for a while + m_iWeaponVolume -= 250 * gpGlobals->frametime; + if ( m_iWeaponVolume < 0 ) + { + iVolume = 0; + } + + + // if target volume is greater than the player sound's current volume, we paste the new volume in + // immediately. If target is less than the current volume, current volume is not set immediately to the + // lower volume, rather works itself towards target volume over time. This gives monsters a much better chance + // to hear a sound, especially if they don't listen every frame. + iVolume = pSound->m_iVolume; + + if ( m_iTargetVolume > iVolume ) + { + iVolume = m_iTargetVolume; + } + else if ( iVolume > m_iTargetVolume ) + { + iVolume -= 250 * gpGlobals->frametime; + + if ( iVolume < m_iTargetVolume ) + { + iVolume = 0; + } + } + + if ( m_fNoPlayerSound ) + { + // debugging flag, lets players move around and shoot without monsters hearing. + iVolume = 0; + } + + if ( gpGlobals->time > m_flStopExtraSoundTime ) + { + // since the extra sound that a weapon emits only lasts for one client frame, we keep that sound around for a server frame or two + // after actual emission to make sure it gets heard. + m_iExtraSoundTypes = 0; + } + + if ( pSound ) + { + pSound->m_vecOrigin = pev->origin; + pSound->m_iType |= ( bits_SOUND_PLAYER | m_iExtraSoundTypes ); + pSound->m_iVolume = iVolume; + } + + // keep track of virtual muzzle flash + m_iWeaponFlash -= 256 * gpGlobals->frametime; + if (m_iWeaponFlash < 0) + m_iWeaponFlash = 0; + + //UTIL_MakeVectors ( pev->angles ); + //gpGlobals->v_forward.z = 0; + + // Below are a couple of useful little bits that make it easier to determine just how much noise the + // player is making. + // UTIL_ParticleEffect ( pev->origin + gpGlobals->v_forward * iVolume, g_vecZero, 255, 25 ); + //ALERT ( at_console, "%d/%d\n", iVolume, m_iTargetVolume ); +} + + +void CBasePlayer::PostThink() +{ + if ( g_fGameOver ) return; // intermission or finale + if ( !IsAlive () ) return; + + if ( m_pTank != NULL ) + { + if ( m_pTank->OnControls( pev ) && !pev->weaponmodel ); + else + { // they've moved off the platform + m_pTank->Use( this, this, USE_OFF, 0 ); + m_pTank = NULL; + } + } + + if ( m_pMonitor != NULL ) + { + if ( !m_pMonitor->OnControls( pev ) ) + { // they've moved off the platform + m_pMonitor->Use( this, this, USE_SET, 1 ); + m_pMonitor = NULL; + } + } + + // do weapon stuff + ItemPostFrame( ); + +// check to see if player landed hard enough to make a sound +// falling farther than half of the maximum safe distance, but not as far a max safe distance will +// play a bootscrape sound, and no damage will be inflicted. Fallling a distance shorter than half +// of maximum safe distance will make no sound. Falling farther than max safe distance will play a +// fallpain sound, and damage will be inflicted based on how far the player fell + + if ( (FBitSet(pev->flags, FL_ONGROUND)) && (pev->health > 0) && m_flFallVelocity >= PLAYER_FALL_PUNCH_THRESHHOLD ) + { + float fvol = 0.5; + + // ALERT ( at_console, "%f\n", m_flFallVelocity ); + + if( FBitSet( pev->watertype, CONTENTS_WATER )) + { + // Did he hit the world or a non-moving entity? + // BUG - this happens all the time in water, especially when + // BUG - water has current force + // if ( !pev->groundentity || VARS(pev->groundentity)->velocity.z == 0 ) + // EMIT_SOUND(ENT(pev), CHAN_BODY, "player/pl_wade1.wav", 1, ATTN_NORM); + } + else if ( m_flFallVelocity > PLAYER_MAX_SAFE_FALL_SPEED ) + { + // after this point, we start doing damage + switch ( RANDOM_LONG(0,1) ) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_fallpain2.wav", 1, ATTN_NORM); + case 1: EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_fallpain3.wav", 1, ATTN_NORM); + } + + float flFallDamage = g_pGameRules->FlPlayerFallDamage( this ); + + if ( flFallDamage > pev->health ) + { + // splat + // NOTE: play on item channel because we play footstep landing on body channel + EMIT_SOUND(ENT(pev), CHAN_ITEM, "common/bodysplat.wav", 1, ATTN_NORM); + } + + if ( flFallDamage > 0 ) + { + TakeDamage(VARS(eoNullEntity), VARS(eoNullEntity), flFallDamage, DMG_FALL ); + } + + fvol = 1.0; + } + else if ( m_flFallVelocity > PLAYER_MAX_SAFE_FALL_SPEED / 2 ) + { + // EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/pl_jumpland2.wav", 1, ATTN_NORM); + fvol = 0.85; + } + else if ( m_flFallVelocity < PLAYER_MIN_BOUNCE_SPEED ) + { + fvol = 0; + } + + if ( fvol > 0.0 ) + { + // get current texture under player right away + m_flTimeStepSound = 0; + UpdateStepSound(); + + // play step sound for current texture + PlayStepSound(MapTextureTypeStepType(m_chTextureType), fvol); + + // knock the screen around a little bit, temporary effect + pev->punchangle.x = m_flFallVelocity * 0.018; // punch x axis + + if ( pev->punchangle.x > 8 ) + { + pev->punchangle.x = 8; + } + + if ( RANDOM_LONG(0,1) ) + { + pev->punchangle.z = m_flFallVelocity * 0.009; + } + + } + + if ( IsAlive() ) + { + SetAnimation( PLAYER_WALK ); + } + } + + if (FBitSet(pev->flags, FL_ONGROUND)) + { + if (m_flFallVelocity > 64 && !g_pGameRules->IsMultiplayer()) + { + CSoundEnt::InsertSound ( bits_SOUND_PLAYER, pev->origin, m_flFallVelocity, 0.2 ); + // ALERT( at_console, "fall %f\n", m_flFallVelocity ); + } + m_flFallVelocity = 0; + } + + // select the proper animation for the player character + if ( IsAlive() ) + { + if (!pev->velocity.x && !pev->velocity.y) + SetAnimation( PLAYER_IDLE ); + else if ((pev->velocity.x || pev->velocity.y) && (FBitSet(pev->flags, FL_ONGROUND))) + SetAnimation( PLAYER_WALK ); + else if (pev->waterlevel > 1) + SetAnimation( PLAYER_WALK ); + } + + StudioFrameAdvance( ); + CheckPowerups(pev); + + UpdatePlayerSound(); + + // Track button info so we can detect 'pressed' and 'released' buttons next frame + m_afButtonLast = pev->button; +} + + +// checks if the spot is clear of players +BOOL IsSpawnPointValid( CBaseEntity *pPlayer, CBaseEntity *pSpot ) +{ + CBaseEntity *ent = NULL; + + if ( pSpot->GetState( pPlayer ) != STATE_ON ) + { + return FALSE; + } + + while ( (ent = UTIL_FindEntityInSphere( ent, pSpot->pev->origin, 128 )) != NULL ) + { + // if ent is a client, don't spawn on 'em + if ( ent->IsPlayer() && ent != pPlayer ) + return FALSE; + } + + return TRUE; +} + + +DLL_GLOBAL CBaseEntity *g_pLastSpawn; + +/* +============ +EntSelectSpawnPoint + +Returns the entity to spawn at + +USES AND SETS GLOBAL g_pLastSpawn +============ +*/ +edict_t *EntSelectSpawnPoint( CBaseEntity *pPlayer ) +{ + CBaseEntity *pSpot; + edict_t *player; + + player = pPlayer->edict(); + +// choose a info_player_deathmatch point + if (g_pGameRules->IsCoOp()) + { + pSpot = UTIL_FindEntityByClassname( g_pLastSpawn, "info_player_coop"); + if ( !FNullEnt(pSpot) ) + goto ReturnSpot; + pSpot = UTIL_FindEntityByClassname( g_pLastSpawn, "info_player_start"); + if ( !FNullEnt(pSpot) ) + goto ReturnSpot; + } + else if ( g_pGameRules->IsDeathmatch() ) + { + pSpot = g_pLastSpawn; + // Randomize the start spot + for ( int i = RANDOM_LONG(1,5); i > 0; i-- ) + pSpot = UTIL_FindEntityByClassname( pSpot, "info_player_deathmatch" ); + if ( FNullEnt( pSpot ) ) // skip over the null point + pSpot = UTIL_FindEntityByClassname( pSpot, "info_player_deathmatch" ); + + CBaseEntity *pFirstSpot = pSpot; + + do + { + if ( pSpot ) + { + // check if pSpot is valid + if ( IsSpawnPointValid( pPlayer, pSpot ) ) + { + if ( pSpot->pev->origin == Vector( 0, 0, 0 ) ) + { + pSpot = UTIL_FindEntityByClassname( pSpot, "info_player_deathmatch" ); + continue; + } + + // if so, go to pSpot + goto ReturnSpot; + } + } + // increment pSpot + pSpot = UTIL_FindEntityByClassname( pSpot, "info_player_deathmatch" ); + } while ( pSpot != pFirstSpot ); // loop if we're not back to the start + + // we haven't found a place to spawn yet, so kill any guy at the first spawn point and spawn there + if ( !FNullEnt( pSpot ) ) + { + CBaseEntity *ent = NULL; + while ( (ent = UTIL_FindEntityInSphere( ent, pSpot->pev->origin, 128 )) != NULL ) + { + // if ent is a client, kill em (unless they are ourselves) + if ( ent->IsPlayer() && !(ent->edict() == player) ) + ent->TakeDamage( VARS(INDEXENT(0)), VARS(INDEXENT(0)), 300, DMG_GENERIC ); + } + goto ReturnSpot; + } + } + + // If startspot is set, (re)spawn there. + if ( FStringNull( gpGlobals->startspot ) || !strlen(STRING(gpGlobals->startspot))) + { + pSpot = UTIL_FindEntityByClassname(NULL, "info_player_start"); + if ( !FNullEnt(pSpot) ) + goto ReturnSpot; + } + else + { + pSpot = UTIL_FindEntityByTargetname( NULL, STRING(gpGlobals->startspot) ); + if ( !FNullEnt(pSpot) ) + goto ReturnSpot; + } + +ReturnSpot: + if ( FNullEnt( pSpot ) ) + { + ALERT(at_error, "PutClientInServer: no info_player_start on level"); + return INDEXENT(0); + } + + g_pLastSpawn = pSpot; + return pSpot->edict(); +} + +void CBasePlayer::Spawn( void ) +{ +// ALERT(at_console, "PLAYER spawns at time %f\n", gpGlobals->time); + + pev->classname = MAKE_STRING( "player" ); + pev->health = 100; + pev->armorvalue = 0; + pev->takedamage = DAMAGE_AIM; + pev->solid = SOLID_BBOX; + pev->movetype = MOVETYPE_WALK; + pev->max_health = pev->health; + pev->flags = FL_CLIENT; + pev->air_finished = gpGlobals->time + 12; + pev->dmg = 2; // initial water damage + pev->effects = 0; + pev->deadflag = DEAD_NO; + pev->dmg_take = 0; + pev->dmg_save = 0; + pev->friction = 1.0; + pev->gravity = 1.0; + pev->renderfx = 0; + pev->rendercolor = g_vecZero; + pev->mass = 90; // lbs + m_bitsHUDDamage = -1; + m_bitsDamageType = 0; + m_afPhysicsFlags = 0; + m_fLongJump = 1; // no longjump module. + Rain_dripsPerSecond = 0; + Rain_windX = 0; + Rain_windY = 0; + Rain_randX = 0; + Rain_randY = 0; + Rain_ideal_dripsPerSecond = 0; + Rain_ideal_windX = 0; + Rain_ideal_windY = 0; + Rain_ideal_randX = 0; + Rain_ideal_randY = 0; + Rain_endFade = 0; + Rain_nextFadeUpdate = 0; + GiveOnlyAmmo = FALSE; + + m_iFOV = 90;// init field of view. + m_iClientFOV = -1; // make sure fov reset is sent + //m_iAcessLevel = 2; + + m_flNextDecalTime = 0; // let this player decal as soon as he spawns. + + m_flgeigerDelay = gpGlobals->time + 2.0; // wait a few seconds until user-defined message registrations + // are recieved by all clients + + m_flTimeStepSound = 0; + m_iStepLeft = 0; + m_flFieldOfView = 0.5;// some monsters use this to determine whether or not the player is looking at them. + + m_bloodColor = BLOOD_COLOR_RED; + m_flNextAttack = UTIL_WeaponTimeBase(); + StartSneaking(); + + m_iFlashBattery = 99; + m_flFlashLightTime = 1; // force first message + +// dont let uninitialized value here hurt the player + m_flFallVelocity = 0; + + g_pGameRules->SetDefaultPlayerTeam( this ); + g_pGameRules->GetPlayerSpawnSpot( this ); + + UTIL_SetModel(ENT(pev), "models/player.mdl"); + g_ulModelIndexPlayer = pev->modelindex; + pev->sequence = LookupActivity( ACT_IDLE ); + + if( FBitSet( pev->flags, FL_DUCKING )) + UTIL_SetSize( pev, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX ); + else UTIL_SetSize( pev, VEC_HULL_MIN, VEC_HULL_MAX ); + + pev->view_ofs = VEC_VIEW; + pViewEnt = 0; + viewFlags = 0; + Precache(); + m_HackedGunPos = Vector( 0, 32, 0 ); + + if ( m_iPlayerSound == SOUNDLIST_EMPTY ) + { + ALERT ( at_debug, "Couldn't alloc player sound slot!\n" ); + } + + m_fNoPlayerSound = FALSE;// normal sound behavior. + + m_pLastItem = NULL; + m_fInitHUD = TRUE; + m_iClientHideHUD = -1; // force this to be recalculated + m_fWeapon = FALSE; + m_pClientActiveItem = NULL; + m_iClientBattery = -1; + + // reset all ammo values to 0 + for ( int i = 0; i < MAX_AMMO_SLOTS; i++ ) + { + m_rgAmmo[i] = 0; + m_rgAmmoLast[i] = 0; // client ammo values also have to be reset (the death hud clear messages does on the client side) + } + + m_lastx = m_lasty = 0; + + m_flNextChatTime = gpGlobals->time; + + g_pGameRules->PlayerSpawn( this ); +} + + +void CBasePlayer :: Precache( void ) +{ + // in the event that the player JUST spawned, and the level node graph + // was loaded, fix all of the node graph pointers before the game starts. + + // SOUNDS / MODELS ARE PRECACHED in ClientPrecache() (game specific) + // because they need to precache before any clients have connected + + // init geiger counter vars during spawn and each time + // we cross a level transition + + m_flgeigerRange = 1000; + m_igeigerRangePrev = 1000; + + m_bitsDamageType = 0; + m_bitsHUDDamage = -1; + GiveOnlyAmmo = FALSE; + + m_iClientBattery = -1; + + m_iTrain = TRAIN_NEW; + + // Make sure any necessary user messages have been registered + LinkUserMessages(); + viewNeedsUpdate = 1; + hearNeedsUpdate = 1; + fogNeedsUpdate = 1; + fadeNeedsUpdate = 1; + m_iClientWarHUD = -1; + + m_iUpdateTime = 5; // won't update for 1/2 a second + + if ( gInitHUD ) m_fInitHUD = TRUE; + rainNeedsUpdate = 1; + + //clear fade effects + if(IsMultiplayer()) + { + m_FadeColor = Vector(255, 255, 255); + m_FadeAlpha = 0; + m_iFadeFlags = 0; + m_iFadeTime = 0; + m_iFadeHold = 0; + fadeNeedsUpdate = TRUE; + } +} + + +int CBasePlayer::Save( CSave &save ) +{ + if ( !CBaseMonster::Save(save) ) + return 0; + + return save.WriteFields( "cPLAYER", "PLAYER", this, m_playerSaveData, ARRAYSIZE(m_playerSaveData) ); +} + + +// +// Marks everything as new so the player will resend this to the hud. +// +void CBasePlayer::RenewItems(void) +{ + +} + +int CBasePlayer::Restore( CRestore &restore ) +{ + if ( !CBaseMonster::Restore(restore) ) + return 0; + + int status = restore.ReadFields( "PLAYER", this, m_playerSaveData, ARRAYSIZE(m_playerSaveData) ); + + SAVERESTOREDATA *pSaveData = (SAVERESTOREDATA *)gpGlobals->pSaveData; + // landmark isn't present. + if ( !pSaveData->fUseLandmark ) + { + ALERT( at_debug, "No Landmark:%s\n", pSaveData->szLandmarkName ); + + // default to normal spawn + edict_t* pentSpawnSpot = EntSelectSpawnPoint( this ); + pev->origin = VARS(pentSpawnSpot)->origin + Vector(0,0,1); + pev->angles = VARS(pentSpawnSpot)->angles; + } + pev->v_angle.z = 0; // Clear out roll + pev->angles = pev->v_angle; + + pev->fixangle = TRUE; // turn this way immediately + +// Copied from spawn() for now + m_bloodColor = BLOOD_COLOR_RED; + + g_ulModelIndexPlayer = pev->modelindex; + + if( FBitSet( pev->flags, FL_DUCKING )) + { + // Use the crouch HACK + FixPlayerCrouchStuck( edict() ); + UTIL_SetSize(pev, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX); + } + else + { + UTIL_SetSize(pev, VEC_HULL_MIN, VEC_HULL_MAX); + } + + RenewItems(); + + return status; +} + + + +void CBasePlayer::SelectNextItem( int iItem ) +{ + CBasePlayerWeapon *pItem; + + pItem = m_rgpPlayerItems[ iItem ]; + + if (!pItem) + return; + + if (pItem == m_pActiveItem) + { + // select the next one in the chain + pItem = m_pActiveItem->m_pNext; + if (! pItem) + { + return; + } + + CBasePlayerWeapon *pLast; + pLast = pItem; + while (pLast->m_pNext) + pLast = pLast->m_pNext; + + // relink chain + pLast->m_pNext = m_pActiveItem; + m_pActiveItem->m_pNext = NULL; + m_rgpPlayerItems[ iItem ] = pItem; + } + + ResetAutoaim( ); + + if (m_pActiveItem) m_pActiveItem->Holster( ); + QueueItem(pItem); + if (m_pActiveItem) m_pActiveItem->Deploy( ); +} + +void CBasePlayer::QueueItem(CBasePlayerWeapon *pItem) +{ + if(!m_pActiveItem)// no active weapon + { + m_pActiveItem = pItem; + return;// just set this item as active + } + else + { + m_pLastItem = m_pActiveItem; + m_pActiveItem = NULL;// clear current + } + m_pNextItem = pItem;// add item to queue +} + +void CBasePlayer::SelectItem(const char *pstr) +{ + if (!pstr) return; + + CBasePlayerWeapon *pItem = NULL; + for (int i = 0; i < MAX_ITEM_TYPES; i++) + { + if (m_rgpPlayerItems[i]) + { + pItem = m_rgpPlayerItems[i]; + + while (pItem) + { + if(FStrEq( STRING( pItem->pev->netname), pstr )) + break; + pItem = pItem->m_pNext; + } + } + + if (pItem) + break; + } + + if (!pItem) + return; + + + if (pItem == m_pActiveItem) + return; + + ResetAutoaim( ); + + if (m_pActiveItem) m_pActiveItem->Holster( ); + QueueItem(pItem); + if (m_pActiveItem) m_pActiveItem->Deploy( ); +} + + +void CBasePlayer::SelectLastItem(void) +{ + if (!m_pLastItem) + { + return; + } + + if ( m_pActiveItem && !m_pActiveItem->CanHolster() ) + { + return; + } + + ResetAutoaim( ); + + // FIX, this needs to queue them up and delay + if (m_pActiveItem) m_pActiveItem->Holster( ); + QueueItem(m_pLastItem); + if (m_pActiveItem) m_pActiveItem->Deploy( ); +} + +//============================================== +// HasWeapons - do I have any weapons at all? +//============================================== +BOOL CBasePlayer::HasWeapons( void ) +{ + int i; + + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + if ( m_rgpPlayerItems[ i ] ) + { + return TRUE; + } + } + + return FALSE; +} + +void CBasePlayer::SelectPrevItem( int iItem ) +{ +} + + +const char *CBasePlayer::TeamID( void ) +{ + if ( pev == NULL ) // Not fully connected yet + return ""; + + // return their team name + return m_szTeamName; +} + + +//============================================== +// !!!UNDONE:ultra temporary SprayCan entity to apply +// decal frame at a time. For PreAlpha CD +//============================================== +class CSprayCan : public CBaseEntity +{ +public: + void Spawn ( entvars_t *pevOwner ); + void Think( void ); + + virtual int ObjectCaps( void ) { return FCAP_DONT_SAVE; } +}; + +void CSprayCan::Spawn ( entvars_t *pevOwner ) +{ + pev->origin = pevOwner->origin + Vector ( 0 , 0 , 32 ); + pev->angles = pevOwner->v_angle; + pev->owner = ENT(pevOwner); + pev->frame = 0; + + SetNextThink( 0.1 ); + EMIT_SOUND(ENT(pev), CHAN_VOICE, "player/sprayer.wav", 1, ATTN_NORM); +} + +void CSprayCan::Think( void ) +{ + TraceResult tr; + int playernum; + int nFrames; + CBasePlayer *pPlayer; + + pPlayer = (CBasePlayer *)GET_PRIVATE(pev->owner); + + if (pPlayer) + nFrames = pPlayer->GetCustomDecalFrames(); + else + nFrames = -1; + + playernum = ENTINDEX(pev->owner); + + // ALERT(at_console, "Spray by player %i, %i of %i\n", playernum, (int)(pev->frame + 1), nFrames); + + UTIL_MakeVectors(pev->angles); + UTIL_TraceLine ( pev->origin, pev->origin + gpGlobals->v_forward * 128, ignore_monsters, pev->owner, & tr); + + // No customization present. + if (nFrames == -1) + { + UTIL_DecalTrace( &tr, DECAL_LAMBDA6 ); + UTIL_Remove( this ); + } + else + { + UTIL_PlayerDecalTrace( &tr, playernum, pev->frame, TRUE ); + // Just painted last custom frame. + if ( pev->frame++ >= (nFrames - 1)) + UTIL_Remove( this ); + } + + SetNextThink( 0.1 ); +} + +class CBloodSplat : public CBaseEntity +{ +public: + void Spawn ( entvars_t *pevOwner ); + void Spray ( void ); +}; + +void CBloodSplat::Spawn ( entvars_t *pevOwner ) +{ + pev->origin = pevOwner->origin + Vector ( 0 , 0 , 32 ); + pev->angles = pevOwner->v_angle; + pev->owner = ENT(pevOwner); + + SetThink(&CBloodSplat:: Spray ); + SetNextThink( 0.1 ); +} + +void CBloodSplat::Spray ( void ) +{ + TraceResult tr; + + if ( g_Language != LANGUAGE_GERMAN ) + { + UTIL_MakeVectors(pev->angles); + UTIL_TraceLine ( pev->origin, pev->origin + gpGlobals->v_forward * 128, ignore_monsters, pev->owner, & tr); + + UTIL_BloodDecalTrace( &tr, BLOOD_COLOR_RED ); + } + SetThink( Remove ); + SetNextThink( 0.1 ); +} + +//============================================== + +void CBasePlayer::GiveNamedItem( const char *pszName ) +{ + edict_t *pent; + int istr = MAKE_STRING(pszName); + + if(GetProcAddress( GetModuleHandle("server"), pszName )) + { + pent = CREATE_NAMED_ENTITY(istr); + if ( FNullEnt( pent )) return; + } + else if(!strncmp( pszName, "weapon_", 7)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("weapon_generic")); + if ( FNullEnt( pent )) return;//this never gonna called anymore. just in case + pent->v.netname = istr; + } + else if(!strncmp( pszName, "item_", 5)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("item_generic")); + if ( FNullEnt( pent )) return;//this never gonna called anymore. just in case + pent->v.netname = istr; + } + else if(!strncmp( pszName, "ammo_", 5)) + { + //may be this a weapon_generic entity? + pent = CREATE_NAMED_ENTITY(MAKE_STRING("item_generic")); + if ( FNullEnt( pent )) return;//this never gonna called anymore. just in case + pent->v.netname = istr; + } + else //unknown error + { + Msg("can't create %s\n", pszName ); + return; + } + + DevMsg("Give %s\n", STRING(pent->v.classname)); + + VARS( pent )->origin = pev->origin; + pent->v.spawnflags |= SF_NORESPAWN; + + DispatchSpawn( pent ); + DispatchTouch( pent, ENT( pev ) ); +} + +BOOL CBasePlayer :: FlashlightIsOn( void ) +{ + return FBitSet(pev->effects, EF_DIMLIGHT); +} + + +void CBasePlayer :: FlashlightTurnOn( void ) +{ + if ( !g_pGameRules->FAllowFlashlight()) return; + + if (m_iFlashBattery == 0) + { + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, SOUND_FLASHLIGHT_ON, 1.0, ATTN_NORM, 0, PITCH_NORM ); + return; + } + + if (m_iHideHUD & ITEM_SUIT) + { + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, SOUND_FLASHLIGHT_ON, 1.0, ATTN_NORM, 0, PITCH_NORM ); + SetBits(pev->effects, EF_DIMLIGHT); + MESSAGE_BEGIN( MSG_ONE, gmsgFlashlight, NULL, pev ); + WRITE_BYTE(1); + WRITE_BYTE(m_iFlashBattery); + MESSAGE_END(); + + m_flFlashLightTime = FLASH_DRAIN_TIME + gpGlobals->time; + } +} + + +void CBasePlayer :: FlashlightTurnOff( void ) +{ + EMIT_SOUND_DYN( ENT(pev), CHAN_WEAPON, SOUND_FLASHLIGHT_OFF, 1.0, ATTN_NORM, 0, PITCH_NORM ); + + ClearBits(pev->effects, EF_DIMLIGHT); + MESSAGE_BEGIN( MSG_ONE, gmsgFlashlight, NULL, pev ); + WRITE_BYTE(0); + WRITE_BYTE(m_iFlashBattery); + MESSAGE_END(); + + m_flFlashLightTime = FLASH_CHARGE_TIME + gpGlobals->time; + +} + +/* +=============== +ForceClientDllUpdate + +When recording a demo, we need to have the server tell us the entire client state +so that the client side .dll can behave correctly. +Reset stuff so that the state is transmitted. +=============== +*/ +void CBasePlayer :: ForceClientDllUpdate( void ) +{ + m_iClientHealth = -1; + m_iClientBattery = -1; + m_iTrain |= TRAIN_NEW; // Force new train message. + m_fWeapon = FALSE; // Force weapon send + m_fKnownItem = FALSE; // Force weaponinit messages. + m_fInitHUD = TRUE; // Force HUD gmsgResetHUD message + + // Now force all the necessary messages + // to be sent. + UpdateClientData(); +} + +/* +============ +ImpulseCommands +============ +*/ +void CBasePlayer::ImpulseCommands( ) +{ + TraceResult tr;// UNDONE: kill me! This is temporary for PreAlpha CDs + + // Handle use events + PlayerUse(); + + int iImpulse = (int)pev->impulse; + switch (iImpulse) + { + case 100: //enable flashlight + if ( FlashlightIsOn()) FlashlightTurnOff(); + else FlashlightTurnOn(); + break; + case 201: // paint decal + if( gpGlobals->time < m_flNextDecalTime ) break; + + UTIL_MakeVectors(pev->v_angle); + UTIL_TraceLine ( pev->origin + pev->view_ofs, pev->origin + pev->view_ofs + gpGlobals->v_forward * 128, ignore_monsters, ENT(pev), & tr); + + if ( tr.flFraction != 1.0 ) + { + // line hit something, so paint a decal + m_flNextDecalTime = gpGlobals->time + CVAR_GET_FLOAT( "decalfrequency" ); + CSprayCan *pCan = GetClassPtr((CSprayCan *)NULL); + pCan->Spawn( pev ); + } + break; + case 204: //Demo recording, update client dll specific data again. + ForceClientDllUpdate(); + break; + default: + // check all of the cheat impulse commands now + CheatImpulseCommands( iImpulse ); + break; + } + + pev->impulse = 0; +} + +//========================================================= +//========================================================= +void CBasePlayer::CheatImpulseCommands( int iImpulse ) +{ + if ( !g_flWeaponCheat) return; + + + CBaseEntity *pEntity; + TraceResult tr; + + switch ( iImpulse ) + { + + case 101: + { + if(GiveOnlyAmmo) + { + gEvilImpulse101 = TRUE; + GiveNamedItem( "item_suit" ); //player may don't give suit at first time + GiveNamedItem("weapon_handgrenade"); + GiveNamedItem( "ammo_buckshot" ); + GiveNamedItem( "ammo_9mmclip" ); + GiveNamedItem( "ammo_357" ); + GiveNamedItem( "ammo_556" ); + GiveNamedItem( "ammo_762" ); + GiveNamedItem( "ammo_9mmAR" ); + GiveNamedItem( "ammo_m203" ); + GiveNamedItem( "ammo_rpgclip" ); + gEvilImpulse101 = FALSE; + } + else + { + gEvilImpulse101 = TRUE; + char *pfile = (char *)LOAD_FILE( "scripts/impulse101.txt", NULL ); + int ItemName [256]; + int count = 0; + char token[32]; + + if(pfile) + { + while ( pfile ) + { + //parsing impulse101.txt + pfile = COM_ParseFile(pfile, token); + if(strlen(token)) + { + ItemName[ count ] = ALLOC_STRING( (char *)token ); + count++; + } + } + COM_FreeFile( pfile ); + for( int i = 0; i < count; i++ ) + GiveNamedItem( (char *)STRING( ItemName[i]) ); + } + GiveOnlyAmmo = TRUE;//no need to give all weapons again + gEvilImpulse101 = FALSE; + } + } + break; + + case 102: + { + pEntity = UTIL_FindEntityForward( this ); + if (pEntity) pEntity->Use( this, this, USE_TOGGLE, 0); + break; + } + case 103: // print global info about current entity or texture + { + TraceResult tr; + edict_t *pWorld = g_engfuncs.pfnPEntityOfEntIndex( 0 ); + Vector start = pev->origin + pev->view_ofs; + Vector end = start + gpGlobals->v_forward * 1024; + UTIL_TraceLine( start, end, ignore_monsters, edict(), &tr ); + if ( tr.pHit ) pWorld = tr.pHit; + const char *pTextureName = TRACE_TEXTURE( pWorld, start, end ); + pEntity = UTIL_FindEntityForward( this ); + if ( pEntity ) + { + CBaseMonster *pMonster = pEntity->MyMonsterPointer(); + if ( pMonster ) pMonster->ReportAIState(); + else pEntity->Use( this, this, USE_SHOWINFO, 0 ); + } + else if ( pTextureName ) ALERT( at_console, "Texture: %s\n", pTextureName ); + } + break; + + case 104:// show shortest paths for entire level to nearest node + { + Create("node_viewer_fly", pev->origin, pev->angles); + Create("node_viewer_human", pev->origin, pev->angles); + } + break; + case 105:// remove any solid entity. + pEntity = UTIL_FindEntityForward( this ); + if ( pEntity ) UTIL_Remove (pEntity); + break; + case 106://give weapon_debug + GiveNamedItem( "weapon_debug" ); + break; + } +} + +// +// Add a weapon to the player (Item == Weapon == Selectable Object) +// +int CBasePlayer::AddPlayerItem( CBasePlayerWeapon *pItem ) +{ + CBasePlayerWeapon *pInsert; + + pInsert = m_rgpPlayerItems[pItem->iItemSlot()]; + + while (pInsert) + { + if (FClassnameIs( pInsert->pev, STRING( pItem->pev->netname))) + { + if (pItem->AddDuplicate( pInsert )) + { + g_pGameRules->PlayerGotWeapon ( this, pItem ); + pItem->CheckRespawn(); + pItem->Drop( ); + } + else if (gEvilImpulse101) + { + // FIXME: remove anyway for deathmatch testing + pItem->Drop(); + } + return FALSE; + } + pInsert = pInsert->m_pNext; + } + + if (pItem->AddToPlayer( this )) + { + g_pGameRules->PlayerGotWeapon ( this, pItem ); + pItem->CheckRespawn(); + + pItem->m_pNext = m_rgpPlayerItems[pItem->iItemSlot()]; + m_rgpPlayerItems[pItem->iItemSlot()] = pItem; + + // should we switch to this item? + if ( g_pGameRules->FShouldSwitchWeapon( this, pItem ) ) + { + SwitchWeapon( pItem ); + } + + return TRUE; + } + else if (gEvilImpulse101) + { + // FIXME: remove anyway for deathmatch testing + pItem->Drop(); + } + + return FALSE; +} + + + +int CBasePlayer::RemovePlayerItem( CBasePlayerWeapon *pItem ) +{ + if (m_pActiveItem == pItem) + { + ResetAutoaim( ); + pItem->Holster( ); + pItem->DontThink();// crowbar may be trying to swing again, etc. + pItem->SetThink( NULL ); + m_pActiveItem = NULL; + pev->viewmodel = 0; + pev->weaponmodel = 0; + } + else if ( m_pLastItem == pItem ) + m_pLastItem = NULL; + + CBasePlayerWeapon *pPrev = m_rgpPlayerItems[pItem->iItemSlot()]; + + if (pPrev == pItem) + { + m_rgpPlayerItems[pItem->iItemSlot()] = pItem->m_pNext; + return TRUE; + } + else + { + while (pPrev && pPrev->m_pNext != pItem) + { + pPrev = pPrev->m_pNext; + } + if (pPrev) + { + pPrev->m_pNext = pItem->m_pNext; + return TRUE; + } + } + return FALSE; +} + + +// +// Returns the unique ID for the ammo, or -1 if error +// +int CBasePlayer :: GiveAmmo( int iCount, char *szName, int iMax ) +{ + if ( !szName ) return -1; + + if ( !g_pGameRules->CanHaveAmmo( this, szName, iMax ) ) + { + // game rules say I can't have any more of this ammo type. + return -1; + } + + int i = 0; + + i = GetAmmoIndex( szName ); + + if ( i < 0 || i >= MAX_AMMO_SLOTS ) return -1; + + int iAdd = min( iCount, iMax - m_rgAmmo[i] ); + if ( iAdd < 1 ) return i; + + m_rgAmmo[ i ] += iAdd; + + + if ( gmsgAmmoPickup )// make sure the ammo messages have been linked first + { + // Send the message that ammo has been picked up + MESSAGE_BEGIN( MSG_ONE, gmsgAmmoPickup, NULL, pev ); + WRITE_BYTE( GetAmmoIndex(szName) ); + WRITE_BYTE( iAdd ); + MESSAGE_END(); + } + + return i; +} + + +/* +============ +ItemPreFrame + +Called every frame by the player PreThink +============ +*/ +void CBasePlayer::ItemPreFrame() +{ + if ( gpGlobals->time < m_flNextAttack ) + { + return; + } + + if (!m_pActiveItem)// XWider + { + if(m_pNextItem) + { + m_pActiveItem = m_pNextItem; + m_pActiveItem->Deploy(); + m_pNextItem = NULL; + } + } + + if (!m_pActiveItem) + return; + + m_pActiveItem->ItemPreFrame( ); +} + + +/* +============ +ItemPostFrame + +Called every frame by the player PostThink +============ +*/ +void CBasePlayer::ItemPostFrame() +{ + static int fInSelect = FALSE; + + // check if the player is using a tank + if ( m_pTank != NULL || m_pMonitor != NULL)return; + + if ( gpGlobals->time < m_flNextAttack ) + { + return; + } + + ImpulseCommands(); + + if (!m_pActiveItem) + return; + m_pActiveItem->ItemPostFrame( ); +} + +int CBasePlayer::AmmoInventory( int iAmmoIndex ) +{ + if (iAmmoIndex == -1) + { + return -1; + } + + return m_rgAmmo[ iAmmoIndex ]; +} + +int CBasePlayer::GetAmmoIndex(const char *psz) +{ + int i; + + if (!psz || !stricmp( psz, "none" )) return -1; + + for (i = 1; i < MAX_AMMO_SLOTS; i++) + { + if ( !CBasePlayerWeapon::AmmoInfoArray[i].pszName ) + continue; + + if (stricmp( psz, CBasePlayerWeapon::AmmoInfoArray[i].pszName ) == 0) + return i; + } + + return -1; +} + +const char *CBasePlayer::GetAmmoName(int index ) +{ + return CBasePlayerWeapon::AmmoInfoArray[index].pszName; +} + +// Called from UpdateClientData +// makes sure the client has all the necessary ammo info, if values have changed +void CBasePlayer::SendAmmoUpdate(void) +{ + for (int i=0; i < MAX_AMMO_SLOTS;i++) + { + if (m_rgAmmo[i] != m_rgAmmoLast[i]) + { + m_rgAmmoLast[i] = m_rgAmmo[i]; + + ASSERT( m_rgAmmo[i] >= 0 ); + ASSERT( m_rgAmmo[i] < 255 ); + + // send "Ammo" update message + MESSAGE_BEGIN( MSG_ONE, gmsgAmmoX, NULL, pev ); + WRITE_BYTE( i ); + WRITE_BYTE( max( min( m_rgAmmo[i], 254 ), 0 ) ); // clamp the value to one byte + MESSAGE_END(); + } + } +} + +/* +========================================================= + UpdateClientData + +resends any changed player HUD info to the client. +Called every frame by Player::PreThink +Also called at start of demo recording and playback by +ForceClientDllUpdate to ensure the demo gets messages +reflecting all of the HUD state info. +========================================================= +*/ + +void CBasePlayer :: UpdateClientData( void ) +{ + // check for cheats here + g_flWeaponCheat = CVAR_GET_FLOAT( "sv_cheats" ); // Is the impulse 101 command allowed? + + if (m_fInitHUD) + { + m_fInitHUD = FALSE; + gInitHUD = FALSE; + + MESSAGE_BEGIN( MSG_ONE, gmsgResetHUD, NULL, pev ); + WRITE_BYTE( 0 ); + MESSAGE_END(); + + if ( !m_fGameHUDInitialized ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgInitHUD, NULL, pev ); + MESSAGE_END(); + + g_pGameRules->InitHUD( this ); + m_fGameHUDInitialized = TRUE; + if ( g_pGameRules->IsMultiplayer() ) + { + UTIL_FireTargets( "game_playerjoin", this, this, USE_TOGGLE ); + } + m_iStartMessage = 1; //send player init messages + } + + UTIL_FireTargets( "game_playerspawn", this, this, USE_TOGGLE ); + InitStatusBar(); + } + + if ( m_iHideHUD != m_iClientHideHUD ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgHideWeapon, NULL, pev ); + WRITE_BYTE( m_iHideHUD ); + MESSAGE_END(); + + m_iClientHideHUD = m_iHideHUD; + } + + if ( m_iFOV != m_iClientFOV ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgSetFOV, NULL, pev ); + WRITE_BYTE( m_iFOV ); + MESSAGE_END(); + + // cache FOV change at end of function, so weapon updates can see that FOV has changed + } + + if (viewNeedsUpdate != 0) + { + int indexToSend; + + //we can entity to look at + if(pViewEnt)indexToSend = pViewEnt->entindex(); + else //just reset camera + { + indexToSend = 0; + viewFlags = 0; //clear possibly ACTIVE flag + } + + MESSAGE_BEGIN(MSG_ONE, gmsgCamData, NULL, pev); + WRITE_SHORT( indexToSend ); + WRITE_SHORT( viewFlags ); + MESSAGE_END(); + + viewNeedsUpdate = 0; + } + + if( hearNeedsUpdate != 0) + { + //update dsp sound + MESSAGE_BEGIN( MSG_ONE, SVC_ROOMTYPE, NULL, pev ); + WRITE_SHORT( m_iSndRoomtype ); + MESSAGE_END(); + hearNeedsUpdate = 0; + } + + if(fadeNeedsUpdate != 0) + { + //update screenfade + MESSAGE_BEGIN( MSG_ONE, gmsgFade, NULL, pev ); + WRITE_SHORT( FixedUnsigned16( m_iFadeTime, 1<<12 )); + WRITE_SHORT( FixedUnsigned16( m_iFadeHold, 1<<12 )); + WRITE_SHORT( m_iFadeFlags ); // fade flags + WRITE_BYTE( (byte)m_FadeColor.x ); // fade red + WRITE_BYTE( (byte)m_FadeColor.y ); // fade green + WRITE_BYTE( (byte)m_FadeColor.z ); // fade blue + WRITE_BYTE( (byte)m_FadeAlpha ); // fade alpha + MESSAGE_END(); + fadeNeedsUpdate = 0; + } + + if (m_iStartMessage != 0) + { + SendStartMessages(); + m_iStartMessage = 0; + } + + if(m_FogFadeTime != 0) + { + float flDegree = (gpGlobals->time - m_flStartTime) / m_FogFadeTime; + m_iFogEndDist -= (FOG_LIMIT - m_iFogFinalEndDist)*(flDegree / m_iFogFinalEndDist); + + // cap it + if (m_iFogEndDist > FOG_LIMIT) + { + m_iFogEndDist = FOG_LIMIT; + m_FogFadeTime = 0; + } + if (m_iFogEndDist < m_iFogFinalEndDist) + { + m_iFogEndDist = m_iFogFinalEndDist; + m_FogFadeTime = 0; + } + fogNeedsUpdate = TRUE; + } + + if(fogNeedsUpdate != 0) + { + //update fog + MESSAGE_BEGIN( MSG_ONE, gmsgSetFog, NULL, pev ); + WRITE_BYTE ( m_FogColor.x ); + WRITE_BYTE ( m_FogColor.y ); + WRITE_BYTE ( m_FogColor.z ); + WRITE_SHORT ( m_iFogStartDist ); + WRITE_SHORT ( m_iFogEndDist ); + MESSAGE_END(); + fogNeedsUpdate = 0; + } + + if( m_iWarHUD != m_iClientWarHUD ) + { + MESSAGE_BEGIN( MSG_ONE, gmsgWarHUD, NULL, pev ); + WRITE_BYTE( m_iWarHUD );//make static screen + MESSAGE_END(); + m_iClientWarHUD = m_iWarHUD; + } + + if (pev->health != m_iClientHealth) + { + int iHealth = max( pev->health, 0 ); // make sure that no negative health values are sent + + // send "health" update message + MESSAGE_BEGIN( MSG_ONE, gmsgHealth, NULL, pev ); + WRITE_BYTE( iHealth ); + MESSAGE_END(); + + m_iClientHealth = pev->health; + } + + + if (pev->armorvalue != m_iClientBattery) + { + m_iClientBattery = pev->armorvalue; + + ASSERT( gmsgBattery > 0 ); + // send "health" update message + MESSAGE_BEGIN( MSG_ONE, gmsgBattery, NULL, pev ); + WRITE_SHORT( (int)pev->armorvalue); + MESSAGE_END(); + } + + if (pev->dmg_take || pev->dmg_save || m_bitsHUDDamage != m_bitsDamageType) + { + // Comes from inside me if not set + Vector damageOrigin = pev->origin; + // send "damage" message + // causes screen to flash, and pain compass to show direction of damage + edict_t *other = pev->dmg_inflictor; + if ( other ) + { + CBaseEntity *pEntity = CBaseEntity::Instance(other); + if ( pEntity ) + damageOrigin = pEntity->Center(); + } + + // only send down damage type that have hud art + int visibleDamageBits = m_bitsDamageType & DMG_SHOWNHUD; + + MESSAGE_BEGIN( MSG_ONE, gmsgDamage, NULL, pev ); + WRITE_BYTE( pev->dmg_save ); + WRITE_BYTE( pev->dmg_take ); + WRITE_LONG( visibleDamageBits ); + WRITE_COORD( damageOrigin.x ); + WRITE_COORD( damageOrigin.y ); + WRITE_COORD( damageOrigin.z ); + MESSAGE_END(); + + pev->dmg_take = 0; + pev->dmg_save = 0; + m_bitsHUDDamage = m_bitsDamageType; + + // Clear off non-time-based damage indicators + m_bitsDamageType &= DMG_TIMEBASED; + } + + // Update Flashlight + if ((m_flFlashLightTime) && (m_flFlashLightTime <= gpGlobals->time)) + { + if (FlashlightIsOn()) + { + if (m_iFlashBattery) + { + m_flFlashLightTime = FLASH_DRAIN_TIME + gpGlobals->time; + m_iFlashBattery--; + + if (!m_iFlashBattery) FlashlightTurnOff(); + } + } + else + { + if (m_iFlashBattery < 100) + { + m_flFlashLightTime = FLASH_CHARGE_TIME + gpGlobals->time; + m_iFlashBattery++; + } + else m_flFlashLightTime = 0; + } + + MESSAGE_BEGIN( MSG_ONE, gmsgFlashBattery, NULL, pev ); + WRITE_BYTE(m_iFlashBattery); + MESSAGE_END(); + } + + // calculate and update rain fading + if (Rain_endFade > 0) + { + if (gpGlobals->time < Rain_endFade) + { // we're in fading process + if (Rain_nextFadeUpdate <= gpGlobals->time) + { + int secondsLeft = Rain_endFade - gpGlobals->time + 1; + + Rain_dripsPerSecond += (Rain_ideal_dripsPerSecond - Rain_dripsPerSecond) / secondsLeft; + Rain_windX += (Rain_ideal_windX - Rain_windX) / (float)secondsLeft; + Rain_windY += (Rain_ideal_windY - Rain_windY) / (float)secondsLeft; + Rain_randX += (Rain_ideal_randX - Rain_randX) / (float)secondsLeft; + Rain_randY += (Rain_ideal_randY - Rain_randY) / (float)secondsLeft; + + Rain_nextFadeUpdate = gpGlobals->time + 1; // update once per second + rainNeedsUpdate = 1; + + ALERT(at_aiconsole, "Rain fading: curdrips: %i, idealdrips %i\n", Rain_dripsPerSecond, Rain_ideal_dripsPerSecond); + } + } + else + { // finish fading process + Rain_nextFadeUpdate = 0; + Rain_endFade = 0; + + Rain_dripsPerSecond = Rain_ideal_dripsPerSecond; + Rain_windX = Rain_ideal_windX; + Rain_windY = Rain_ideal_windY; + Rain_randX = Rain_ideal_randX; + Rain_randY = Rain_ideal_randY; + rainNeedsUpdate = 1; + + ALERT(at_aiconsole, "Rain fading finished at %i drips\n", Rain_dripsPerSecond); + } + } + + // send rain message + if (rainNeedsUpdate) + { + //search for rain_settings entity + CBaseEntity *pFind; + pFind = UTIL_FindEntityByClassname( NULL, "env_rain" ); + if (!FNullEnt( pFind )) + { + // rain allowed on this map + CBaseEntity *pEnt = CBaseEntity::Instance( pFind->edict() ); + + float raindistance = pEnt->pev->armorvalue; + float rainheight = pEnt->pev->origin[2]; + int rainmode = pEnt->pev->button; + + // search for constant rain_modifies + pFind = UTIL_FindEntityByClassname( NULL, "util_rainmodify" ); + while ( !FNullEnt( pFind ) ) + { + if (pFind->pev->spawnflags & 1) + { + // copy settings to player's data and clear fading + CBaseEntity *pEnt = CBaseEntity::Instance( pFind->edict() ); + CUtilRainModify *pRainModify = (CUtilRainModify *)pEnt; + + Rain_dripsPerSecond = pRainModify->pev->button; + Rain_windX = pRainModify->windXY[1]; + Rain_windY = pRainModify->windXY[0]; + Rain_randX = pRainModify->randXY[1]; + Rain_randY = pRainModify->randXY[0]; + + Rain_endFade = 0; + break; + } + pFind = UTIL_FindEntityByClassname( pFind, "util_rainmodify" ); + } + + MESSAGE_BEGIN(MSG_ONE, gmsgRainData, NULL, pev); + WRITE_SHORT(Rain_dripsPerSecond); + WRITE_COORD(raindistance); + WRITE_COORD(Rain_windX); + WRITE_COORD(Rain_windY); + WRITE_COORD(Rain_randX); + WRITE_COORD(Rain_randY); + WRITE_SHORT(rainmode); + WRITE_COORD(rainheight); + MESSAGE_END(); + + if (Rain_dripsPerSecond) + ALERT(at_aiconsole, "Sending enabling rain message\n"); + else + ALERT(at_aiconsole, "Sending disabling rain message\n"); + } + else + { // no rain on this map + Rain_dripsPerSecond = 0; + Rain_windX = 0; + Rain_windY = 0; + Rain_randX = 0; + Rain_randY = 0; + Rain_ideal_dripsPerSecond = 0; + Rain_ideal_windX = 0; + Rain_ideal_windY = 0; + Rain_ideal_randX = 0; + Rain_ideal_randY = 0; + Rain_endFade = 0; + Rain_nextFadeUpdate = 0; + + ALERT(at_aiconsole, "Clearing rain data\n"); + } + + rainNeedsUpdate = 0; + } + + // Update Flashlight + if ((m_flFlashLightTime) && (m_flFlashLightTime <= gpGlobals->time)) + { + if (FlashlightIsOn()) + { + if (m_iFlashBattery) + { + m_flFlashLightTime = FLASH_DRAIN_TIME + gpGlobals->time; + m_iFlashBattery--; + + if (!m_iFlashBattery) + FlashlightTurnOff(); + } + } + else m_flFlashLightTime = 0; + + MESSAGE_BEGIN( MSG_ONE, gmsgFlashBattery, NULL, pev ); + WRITE_BYTE(m_iFlashBattery); + MESSAGE_END(); + } + + if (m_iTrain & TRAIN_NEW) + { + ASSERT( gmsgTrain > 0 ); + // send "health" update message + MESSAGE_BEGIN( MSG_ONE, gmsgTrain, NULL, pev ); + WRITE_BYTE(m_iTrain & 0xF); + MESSAGE_END(); + + m_iTrain &= ~TRAIN_NEW; + } + + // + // New Weapon? + // + if (!m_fKnownItem) + { + m_fKnownItem = TRUE; + + // WeaponInit Message + // byte = # of weapons + // + // for each weapon: + // byte name str length (not including null) + // bytes... name + // byte Ammo Type + // byte Ammo2 Type + // byte bucket + // byte bucket pos + // byte flags + // ???? Icons + + // Send ALL the weapon info now + int i; + + for (i = 0; i < MAX_WEAPONS; i++) + { + ItemInfo& II = CBasePlayerWeapon::ItemInfoArray[i]; + + if ( !II.iId ) + continue; + + const char *pszName; + if (!II.pszName) pszName = "Empty"; + else pszName = II.pszName; + + MESSAGE_BEGIN( MSG_ONE, gmsgWeaponList, NULL, pev ); + WRITE_STRING(pszName); // string weapon name + WRITE_BYTE(GetAmmoIndex(II.pszAmmo1)); // byte Ammo Type + WRITE_BYTE(II.iMaxAmmo1); // byte Max Ammo 1 + WRITE_BYTE(GetAmmoIndex(II.pszAmmo2)); // byte Ammo2 Type + WRITE_BYTE(II.iMaxAmmo2); // byte Max Ammo 2 + WRITE_BYTE(II.iSlot); // byte bucket + WRITE_BYTE(II.iPosition); // byte bucket pos + WRITE_BYTE(II.iId); // byte id (bit index into pev->weapons) + WRITE_BYTE(II.iFlags); // byte Flags + MESSAGE_END(); + } + } + + SendAmmoUpdate(); + + // Update all the items + for ( int i = 0; i < MAX_ITEM_TYPES; i++ ) + { + if ( m_rgpPlayerItems[i] ) // each item updates it's successors + m_rgpPlayerItems[i]->UpdateClientData( this ); + } + + // Cache and client weapon change + m_pClientActiveItem = m_pActiveItem; + m_iClientFOV = m_iFOV; + + // Update Status Bar + if ( m_flNextSBarUpdateTime < gpGlobals->time ) + { + UpdateStatusBar(); + m_flNextSBarUpdateTime = gpGlobals->time + 0.2; + } +} + +void CBasePlayer :: SendStartMessages( void ) +{ + edict_t *pEdict = g_engfuncs.pfnPEntityOfEntIndex( 1 ); + CBaseEntity *pEntity; + + if ( !pEdict ) return; + + for ( int i = 1; i < gpGlobals->maxEntities; i++, pEdict++ ) + { + if ( pEdict->free ) continue;// Not in use + + pEntity = CBaseEntity::Instance(pEdict); + if ( !pEntity ) continue; + + pEntity->StartMessage( this ); + } +} + +//========================================================= +// FBecomeProne - Overridden for the player to set the proper +// physics flags when a barnacle grabs player. +//========================================================= +BOOL CBasePlayer :: FBecomeProne ( void ) +{ + m_afPhysicsFlags |= PFLAG_ONBARNACLE; + return TRUE; +} + +//========================================================= +// BarnacleVictimBitten - bad name for a function that is called +// by Barnacle victims when the barnacle pulls their head +// into its mouth. For the player, just die. +//========================================================= +void CBasePlayer :: BarnacleVictimBitten ( entvars_t *pevBarnacle ) +{ + TakeDamage ( pevBarnacle, pevBarnacle, pev->health + pev->armorvalue, DMG_SLASH | DMG_ALWAYSGIB ); +} + +//========================================================= +// BarnacleVictimReleased - overridden for player who has +// physics flags concerns. +//========================================================= +void CBasePlayer :: BarnacleVictimReleased ( void ) +{ + m_afPhysicsFlags &= ~PFLAG_ONBARNACLE; +} + + +//========================================================= +// Illumination +// return player light level plus virtual muzzle flash +//========================================================= +int CBasePlayer :: Illumination( void ) +{ + int iIllum = CBaseEntity::Illumination( ); + + iIllum += m_iWeaponFlash; + if (iIllum > 255) + return 255; + return iIllum; +} + + +void CBasePlayer :: EnableControl(BOOL fControl) +{ + if( !fControl ) + { + pev->flags |= FL_FROZEN; + pev->velocity = g_vecZero; //LRC - stop view bobbing + } + else pev->flags &= ~FL_FROZEN; +} + + +#define DOT_1DEGREE 0.9998476951564 +#define DOT_2DEGREE 0.9993908270191 +#define DOT_3DEGREE 0.9986295347546 +#define DOT_4DEGREE 0.9975640502598 +#define DOT_5DEGREE 0.9961946980917 +#define DOT_6DEGREE 0.9945218953683 +#define DOT_7DEGREE 0.9925461516413 +#define DOT_8DEGREE 0.9902680687416 +#define DOT_9DEGREE 0.9876883405951 +#define DOT_10DEGREE 0.9848077530122 +#define DOT_15DEGREE 0.9659258262891 +#define DOT_20DEGREE 0.9396926207859 +#define DOT_25DEGREE 0.9063077870367 + +//========================================================= +// Autoaim +// set crosshair position to point to enemey +//========================================================= +Vector CBasePlayer :: GetAutoaimVector( float flDelta ) +{ + Vector vecSrc = GetGunPosition( ); + float flDist = 8192; + + BOOL m_fOldTargeting = m_fOnTarget; + Vector angles = AutoaimDeflection(vecSrc, flDist, flDelta ); + + // update ontarget if changed + if ( !g_pGameRules->AllowAutoTargetCrosshair() ) + m_fOnTarget = 0; + + if (angles.x > 180) + angles.x -= 360; + if (angles.x < -180) + angles.x += 360; + if (angles.y > 180) + angles.y -= 360; + if (angles.y < -180) + angles.y += 360; + + if (angles.x > 25) + angles.x = 25; + if (angles.x < -25) + angles.x = -25; + if (angles.y > 12) + angles.y = 12; + if (angles.y < -12) + angles.y = -12; + + + m_vecAutoAim = angles * 0.9; + + // Don't send across network if sv_aim is 0 + if( CVAR_GET_FLOAT( "sv_aim" ) != 0 ) + { + if ( m_vecAutoAim.x != m_lastx || m_vecAutoAim.y != m_lasty ) + { + SET_CROSSHAIRANGLE( edict(), -m_vecAutoAim.x, m_vecAutoAim.y ); + + m_lastx = m_vecAutoAim.x; + m_lasty = m_vecAutoAim.y; + } + } + + // ALERT( at_console, "%f %f\n", angles.x, angles.y ); + + UTIL_MakeVectors( pev->v_angle + pev->punchangle + m_vecAutoAim ); + return gpGlobals->v_forward; +} + + +Vector CBasePlayer :: AutoaimDeflection( Vector &vecSrc, float flDist, float flDelta ) +{ + edict_t *pEdict = g_engfuncs.pfnPEntityOfEntIndex( 1 ); + CBaseEntity *pEntity; + float bestdot; + Vector bestdir; + edict_t *bestent; + TraceResult tr; + + if( CVAR_GET_FLOAT( "sv_aim" ) == 0 ) + { + m_fOnTarget = FALSE; + return g_vecZero; + } + + UTIL_MakeVectors( pev->v_angle + pev->punchangle + m_vecAutoAim ); + + // try all possible entities + bestdir = gpGlobals->v_forward; + bestdot = flDelta; // +- 10 degrees + bestent = NULL; + + m_fOnTarget = FALSE; + + UTIL_TraceLine( vecSrc, vecSrc + bestdir * flDist, dont_ignore_monsters, edict(), &tr ); + + + if ( tr.pHit && tr.pHit->v.takedamage != DAMAGE_NO) + { + // don't look through water + if (!((pev->waterlevel != 3 && tr.pHit->v.waterlevel == 3) + || (pev->waterlevel == 3 && tr.pHit->v.waterlevel == 0))) + { + if (tr.pHit->v.takedamage == DAMAGE_AIM) + m_fOnTarget = TRUE; + + return m_vecAutoAim; + } + } + + for ( int i = 1; i < gpGlobals->maxEntities; i++, pEdict++ ) + { + Vector center; + Vector dir; + float dot; + + if ( pEdict->free ) // Not in use + continue; + + if (pEdict->v.takedamage != DAMAGE_AIM) + continue; + if (pEdict == edict()) + continue; + if ( !g_pGameRules->ShouldAutoAim( this, pEdict ) ) + continue; + + pEntity = Instance( pEdict ); + if (pEntity == NULL) + continue; + + if (!pEntity->IsAlive()) + continue; + + // don't look through water + if ((pev->waterlevel != 3 && pEntity->pev->waterlevel == 3) + || (pev->waterlevel == 3 && pEntity->pev->waterlevel == 0)) + continue; + + center = pEntity->BodyTarget( vecSrc ); + + dir = (center - vecSrc).Normalize( ); + + // make sure it's in front of the player + if (DotProduct (dir, gpGlobals->v_forward ) < 0) + continue; + + dot = fabs( DotProduct (dir, gpGlobals->v_right ) ) + + fabs( DotProduct (dir, gpGlobals->v_up ) ) * 0.5; + + // tweek for distance + dot *= 1.0 + 0.2 * ((center - vecSrc).Length() / flDist); + + if (dot > bestdot) + continue; // to far to turn + + UTIL_TraceLine( vecSrc, center, dont_ignore_monsters, edict(), &tr ); + if (tr.flFraction != 1.0 && tr.pHit != pEdict) + { + // ALERT( at_console, "hit %s, can't see %s\n", STRING( tr.pHit->v.classname ), STRING( pEdict->v.classname ) ); + continue; + } + + // don't shoot at friends + if (IRelationship( pEntity ) < 0) + { + if ( !pEntity->IsPlayer() && !g_pGameRules->IsDeathmatch()) + // ALERT( at_console, "friend\n"); + continue; + } + + // can shoot at this one + bestdot = dot; + bestent = pEdict; + bestdir = dir; + } + + if (bestent) + { + bestdir = UTIL_VecToAngles (bestdir); + bestdir.x = -bestdir.x; + bestdir = bestdir - pev->v_angle - pev->punchangle; + + if (bestent->v.takedamage == DAMAGE_AIM) + m_fOnTarget = TRUE; + + return bestdir; + } + + return Vector( 0, 0, 0 ); +} + + +void CBasePlayer :: ResetAutoaim( ) +{ + if (m_vecAutoAim.x != 0 || m_vecAutoAim.y != 0) + { + m_vecAutoAim = Vector( 0, 0, 0 ); + SET_CROSSHAIRANGLE( edict(), 0, 0 ); + } + m_fOnTarget = FALSE; +} + +/* +============= +SetCustomDecalFrames + + UNDONE: Determine real frame limit, 8 is a placeholder. + Note: -1 means no custom frames present. +============= +*/ +void CBasePlayer :: SetCustomDecalFrames( int nFrames ) +{ + if (nFrames > 0 && + nFrames < 8) + m_nCustomSprayFrames = nFrames; + else + m_nCustomSprayFrames = -1; +} + +/* +============= +GetCustomDecalFrames + + Returns the # of custom frames this player's custom clan logo contains. +============= +*/ +int CBasePlayer :: GetCustomDecalFrames( void ) +{ + return m_nCustomSprayFrames; +} + + +//========================================================= +// DropPlayerItem - drop the named item, or if no name, +// the active item. +//========================================================= +void CBasePlayer::DropPlayerItem ( char *pszItemName ) +{ + if( !g_pGameRules->IsMultiplayer() || (CVAR_GET_FLOAT( "mp_weaponstay" ) > 0 )) + { + // no dropping in single player. + //return; + } + + if ( !strlen( pszItemName ) ) + { + // if this string has no length, the client didn't type a name! + // assume player wants to drop the active item. + // make the string null to make future operations in this function easier + pszItemName = NULL; + } + + CBasePlayerWeapon *pWeapon; + int i; + + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + pWeapon = m_rgpPlayerItems[ i ]; + + while ( pWeapon ) + { + if ( pszItemName ) + { + // try to match by name. + if ( !strcmp( pszItemName, STRING( pWeapon->pev->netname ) ) ) + { + // match! + break; + } + } + else + { + // trying to drop active item + if ( pWeapon == m_pActiveItem ) + { + // active item! + break; + } + } + + pWeapon = pWeapon->m_pNext; + } + + + // if we land here with a valid pWeapon pointer, that's because we found the + // item we want to drop and hit a BREAK; pWeapon is the item. + if ( pWeapon ) + { + g_pGameRules->GetNextBestWeapon( this, pWeapon ); + + UTIL_MakeVectors ( pev->angles ); + + pev->weapons &= ~(1<m_iId);// take item off hud + + CWeaponBox *pWeaponBox = (CWeaponBox *)CBaseEntity::Create( "item_weaponbox", pev->origin + gpGlobals->v_forward * 10, pev->angles, edict() ); + pWeaponBox->pev->angles.x = 0; + pWeaponBox->pev->angles.z = 0; + pWeaponBox->PackWeapon( pWeapon ); + pWeaponBox->pev->velocity = gpGlobals->v_forward * 300 + gpGlobals->v_forward * 100; + + // drop half of the ammo for this weapon. + int iAmmoIndex; + + iAmmoIndex = GetAmmoIndex ( pWeapon->pszAmmo1() ); // ??? + + if ( iAmmoIndex != -1 ) + { + // this weapon weapon uses ammo, so pack an appropriate amount. + if ( pWeapon->iFlags() & ITEM_FLAG_EXHAUSTIBLE ) + { + // pack up all the ammo, this weapon is its own ammo type + pWeaponBox->PackAmmo( MAKE_STRING(pWeapon->pszAmmo1()), m_rgAmmo[ iAmmoIndex ] ); + m_rgAmmo[ iAmmoIndex ] = 0; + + } + else + { + // pack half of the ammo + pWeaponBox->PackAmmo( MAKE_STRING(pWeapon->pszAmmo1()), m_rgAmmo[ iAmmoIndex ] / 2 ); + m_rgAmmo[ iAmmoIndex ] /= 2; + } + + } + + return;// we're done, so stop searching with the FOR loop. + } + } +} + +//========================================================= +// HasPlayerItem Does the player already have this item? +//========================================================= +BOOL CBasePlayer::HasPlayerItem( CBasePlayerWeapon *pCheckItem ) +{ + CBasePlayerWeapon *pItem = m_rgpPlayerItems[pCheckItem->iItemSlot()]; + + while (pItem) + { + if (FClassnameIs( pItem->pev, STRING( pCheckItem->pev->netname) )) + return TRUE; + pItem = pItem->m_pNext; + } + + return FALSE; +} + +//========================================================= +// HasNamedPlayerItem Does the player already have this item? +//========================================================= +BOOL CBasePlayer::HasNamedPlayerItem( const char *pszItemName ) +{ + CBasePlayerWeapon *pItem; + int i; + + for ( i = 0 ; i < MAX_ITEM_TYPES ; i++ ) + { + pItem = m_rgpPlayerItems[ i ]; + + while (pItem) + { + if ( !strcmp( pszItemName, STRING( pItem->pev->netname ))) + return TRUE; + pItem = pItem->m_pNext; + } + } + + return FALSE; +} + +//========================================================= +// +//========================================================= +BOOL CBasePlayer :: SwitchWeapon( CBasePlayerWeapon *pWeapon ) +{ + if ( !pWeapon->CanDeploy() ) return FALSE; + ResetAutoaim( ); + + if (m_pActiveItem) m_pActiveItem->Holster( ); + QueueItem(pWeapon); + if (m_pActiveItem) m_pActiveItem->Deploy( ); + + return TRUE; +} + +//========================================================= +// Body queue class here.... It's really just CBaseEntity +//========================================================= +class CCorpse : public CBaseEntity +{ + virtual int ObjectCaps( void ) { return FCAP_DONT_SAVE; } +}; +LINK_ENTITY_TO_CLASS( bodyque, CCorpse ); + +//========================================================= +// Dead HEV suit prop +// LRC- i.e. the dead blokes you see in Xen. +//========================================================= +class CDeadHEV : public CBaseMonster +{ +public: + void Spawn( void ); + int Classify ( void ) { return CLASS_HUMAN_MILITARY; } + + void KeyValue( KeyValueData *pkvd ); + + int m_iPose;// which sequence to display -- temporary, don't need to save + static char *m_szPoses[4]; +}; + +char *CDeadHEV::m_szPoses[] = { "deadback", "deadsitting", "deadstomach", "deadtable" }; + +void CDeadHEV::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "pose")) + { + m_iPose = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseMonster::KeyValue( pkvd ); +} + +LINK_ENTITY_TO_CLASS( monster_hevsuit_dead, CDeadHEV ); + +//========================================================= +// ********** DeadHEV SPAWN ********** +//========================================================= +void CDeadHEV :: Spawn( void ) +{ + PRECACHE_MODEL("models/player.mdl"); + SET_MODEL(ENT(pev), "models/player.mdl"); + + pev->effects = 0; + pev->yaw_speed = 8; + pev->sequence = 0; + pev->body = 1; + m_bloodColor = BLOOD_COLOR_RED; + + pev->sequence = LookupSequence( m_szPoses[m_iPose] ); + + if (pev->sequence == -1) + { + ALERT ( at_debug, "Dead hevsuit with bad pose\n" ); + pev->sequence = 0; + pev->effects = EF_BRIGHTFIELD; + } + + // Corpses have less health + pev->health = 8; + + MonsterInitDead(); +} \ No newline at end of file diff --git a/server/monsters/player.h b/server/monsters/player.h new file mode 100644 index 00000000..baa55025 --- /dev/null +++ b/server/monsters/player.h @@ -0,0 +1,382 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef PLAYER_H +#define PLAYER_H + +#define PLAYER_FATAL_FALL_SPEED 1024 // approx 60 feet +#define PLAYER_MAX_SAFE_FALL_SPEED 580 // approx 20 feet +#define DAMAGE_FOR_FALL_SPEED (float) 100 / ( PLAYER_FATAL_FALL_SPEED - PLAYER_MAX_SAFE_FALL_SPEED )// damage per unit per second. +#define PLAYER_MIN_BOUNCE_SPEED 200 +#define PLAYER_FALL_PUNCH_THRESHHOLD (float) 350 // won't punch player's screen/make scrape noise unless player falling at least this fast. + +#define CBTEXTURENAMEMAX 13 // only load first n chars of name + +#define CHAR_TEX_CONCRETE 'C' // texture types +#define CHAR_TEX_METAL 'M' +#define CHAR_TEX_DIRT 'D' +#define CHAR_TEX_VENT 'V' +#define CHAR_TEX_GRATE 'G' +#define CHAR_TEX_TILE 'T' +#define CHAR_TEX_SLOSH 'S' +#define CHAR_TEX_WOOD 'W' +#define CHAR_TEX_COMPUTER 'P' +#define CHAR_TEX_GLASS 'Y' +#define CHAR_TEX_FLESH 'F' + +// +// Player PHYSICS FLAGS bits +// +#define PFLAG_ONLADDER ( 1<<0 ) +#define PFLAG_ONSWING ( 1<<0 ) +#define PFLAG_ONTRAIN ( 1<<1 ) +#define PFLAG_ONBARNACLE ( 1<<2 ) +#define PFLAG_DUCKING ( 1<<3 ) // In the process of ducking, but totally squatted yet +#define PFLAG_USING ( 1<<4 ) // Using a continuous entity +#define PFLAG_OBSERVER ( 1<<5 ) // player is locked in stationary cam mode. Spectators can move, observers can't. + +// +// generic player +// +//----------------------------------------------------- +//This is Half-Life player entity +//----------------------------------------------------- +#define CSUITPLAYLIST 4 // max of 4 suit sentences queued up at any time + +#define SUIT_GROUP TRUE +#define SUIT_SENTENCE FALSE + +#define SUIT_REPEAT_OK 0 +#define SUIT_NEXT_IN_30SEC 30 +#define SUIT_NEXT_IN_1MIN 60 +#define SUIT_NEXT_IN_5MIN 300 +#define SUIT_NEXT_IN_10MIN 600 +#define SUIT_NEXT_IN_30MIN 1800 +#define SUIT_NEXT_IN_1HOUR 3600 + +#define CSUITNOREPEAT 32 + +#define SOUND_FLASHLIGHT_ON "items/flashlight1.wav" +#define SOUND_FLASHLIGHT_OFF "items/flashlight1.wav" + +#define TEAM_NAME_LENGTH 16 + +typedef enum +{ + PLAYER_IDLE, + PLAYER_WALK, + PLAYER_JUMP, + PLAYER_SUPERJUMP, + PLAYER_DIE, + PLAYER_ATTACK1, +} PLAYER_ANIM; + + +//NB: changing this structure will cause problems! --LRC + +#define MAX_ID_RANGE 2048 +#define SBAR_STRING_SIZE 128 + +enum sbar_data +{ + SBAR_ID_TARGETNAME = 1, + SBAR_ID_TARGETHEALTH, + SBAR_ID_TARGETARMOR, + SBAR_END, +}; + +#define CHAT_INTERVAL 1.0f + +class CBasePlayer : public CBaseMonster +{ +public: + int random_seed; // See that is shared between client & server for shared weapons code + + int m_iPlayerSound;// the index of the sound list slot reserved for this player + int m_iTargetVolume;// ideal sound volume. + int m_iWeaponVolume;// how loud the player's weapon is right now. + int m_iExtraSoundTypes;// additional classification for this weapon's sound + int m_iWeaponFlash;// brightness of the weapon flash + float m_flStopExtraSoundTime; + + float m_flFlashLightTime; // Time until next battery draw/Recharge + int m_iFlashBattery; // Flashlight Battery Draw + + int m_afButtonLast; + int m_afButtonPressed; + int m_afButtonReleased; + + float m_flFallVelocity; + + int m_rgItems[MAX_ITEMS]; + int m_fKnownItem; // True when a new item needs to be added + int m_fNewAmmo; // True when a new item has been added + + unsigned int m_afPhysicsFlags; // physics flags - set when 'normal' physics should be revisited or overriden + float m_fNextSuicideTime; // the time after which the player can next use the suicide command + + +// these are time-sensitive things that we keep track of + float m_flTimeStepSound; // when the last stepping sound was made + float m_flTimeWeaponIdle; // when to play another weapon idle animation. + float m_flSwimTime; // how long player has been underwater + float m_flDuckTime; // how long we've been ducking + float m_flWallJumpTime; // how long until next walljump + + float m_flSuitUpdate; // when to play next suit update + int m_rgSuitPlayList[CSUITPLAYLIST];// next sentencenum to play for suit update + int m_iSuitPlayNext; // next sentence slot for queue storage; + int m_rgiSuitNoRepeat[CSUITNOREPEAT]; // suit sentence no repeat list + float m_rgflSuitNoRepeatTime[CSUITNOREPEAT]; // how long to wait before allowing repeat + int m_lastDamageAmount; // Last damage taken + float m_tbdPrev; // Time-based damage timer + + float m_flgeigerRange; // range to nearest radiation source + float m_flgeigerDelay; // delay per update of range msg to client + int m_igeigerRangePrev; + int m_iStepLeft; // alternate left/right foot stepping sound + char m_szTextureName[CBTEXTURENAMEMAX]; // current texture name we're standing on + char m_chTextureType; // current texture type + + int m_idrowndmg; // track drowning damage taken + int m_idrownrestored; // track drowning damage restored + + int m_bitsHUDDamage; // Damage bits for the current fame. These get sent to + // the hude via the DAMAGE message + BOOL m_fInitHUD; // True when deferred HUD restart msg needs to be sent + BOOL m_fGameHUDInitialized; + int m_iTrain; // Train control position + BOOL m_fWeapon; // Set this to FALSE to force a reset of the current weapon HUD info + + EHANDLE m_pTank; // the tank which the player is currently controlling, NULL if no tank + EHANDLE m_pMonitor; + float m_fDeadTime; // the time at which the player died (used in PlayerDeathThink()) + + BOOL m_fNoPlayerSound; // a debugging feature. Player makes no sound if this is true. + BOOL m_fLongJump; // does this player have the longjump module? + + float m_tSneaking; + int m_iUpdateTime; // stores the number of frame ticks before sending HUD update messages + int m_iClientHealth; // the health currently known by the client. If this changes, send a new + int m_iClientBattery; // the Battery currently known by the client. If this changes, send a new + int m_iHideHUD; // the players hud weapon info is to be hidden + int m_iClientHideHUD; + int m_iFOV; // field of view + int m_iClientFOV; // client's known FOV + + // usable player items + CBasePlayerWeapon *m_rgpPlayerItems[MAX_ITEM_TYPES]; + CBasePlayerWeapon *m_pActiveItem; + CBasePlayerWeapon *m_pClientActiveItem; // client version of the active item + CBasePlayerWeapon *m_pLastItem; + CBasePlayerWeapon *m_pNextItem; + // shared ammo slots + int m_rgAmmo[MAX_AMMO_SLOTS]; + int m_rgAmmoLast[MAX_AMMO_SLOTS]; + + Vector m_vecAutoAim; + BOOL m_fOnTarget; + int m_iDeaths; + float m_iRespawnFrames; // used in PlayerDeathThink() to make sure players can always respawn + + int m_lastx, m_lasty; // These are the previous update's crosshair angles, DON"T SAVE/RESTORE + + int m_nCustomSprayFrames;// Custom clan logo frames for this player + float m_flNextDecalTime;// next time this player can spray a decal + + char m_szTeamName[TEAM_NAME_LENGTH]; + + virtual void Spawn( void ); + void Pain( void ); + + virtual void Jump( void ); + virtual void Duck( void ); + virtual void PreThink( void ); + virtual void PostThink( void ); + virtual Vector GetGunPosition( void ); + virtual int TakeHealth( float flHealth, int bitsDamageType ); + virtual int TakeArmor( float flArmor, int suit = 0 ); + virtual int TakeItem( int iItem ); + virtual void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + virtual int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType); + virtual void Killed( entvars_t *pevAttacker, int iGib ); + virtual Vector BodyTarget( const Vector &posSrc ) { return Center( ) + pev->view_ofs * RANDOM_FLOAT( 0.5, 1.1 ); }; // position to shoot at + virtual void StartSneaking( void ) { m_tSneaking = gpGlobals->time - 1; } + virtual void StopSneaking( void ) { m_tSneaking = gpGlobals->time + 30; } + virtual BOOL IsSneaking( void ) { return m_tSneaking <= gpGlobals->time; } + virtual BOOL IsAlive( void ) { return (pev->deadflag == DEAD_NO) && pev->health > 0; } + virtual BOOL IsPlayer( void ) { return TRUE; } // Spectators should return FALSE for this, they aren't "players" as far as game logic is concerned + + virtual BOOL IsNetClient( void ) { return TRUE; } // Bots should return FALSE for this, they can't receive NET messages + // Spectators should return TRUE for this + virtual const char *TeamID( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + void RenewItems(void); + void PackDeadPlayerItems( void ); + void RemoveAllItems( BOOL removeSuit ); + BOOL SwitchWeapon( CBasePlayerWeapon *pWeapon ); + + // JOHN: sends custom messages if player HUD data has changed (eg health, ammo) + virtual void UpdateClientData( void ); + + static TYPEDESCRIPTION m_playerSaveData[]; + + // Player is moved across the transition by other means + virtual int ObjectCaps( void ) { return CBaseMonster :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION; } + virtual void Precache( void ); + BOOL IsOnLadder( void ); + BOOL FlashlightIsOn( void ); + void FlashlightTurnOn( void ); + void FlashlightTurnOff( void ); + + void UpdatePlayerSound ( void ); + void DeathSound ( void ); + + int Classify ( void ); + void SetAnimation( PLAYER_ANIM playerAnim ); + void SetWeaponAnimType( const char *szExtention ); + char m_szAnimExtention[32]; + + // custom player functions + virtual void ImpulseCommands( void ); + void CheatImpulseCommands( int iImpulse ); + + void StartDeathCam( void ); + void StartObserver( Vector vecPosition, Vector vecViewAngle ); + + void AddPoints( int score, BOOL bAllowNegativeScore ); + void AddPointsToTeam( int score, BOOL bAllowNegativeScore ); + BOOL AddPlayerItem( CBasePlayerWeapon *pItem ); + BOOL RemovePlayerItem( CBasePlayerWeapon *pItem ); + void DropPlayerItem ( char *pszItemName ); + BOOL HasPlayerItem( CBasePlayerWeapon *pCheckItem ); + BOOL HasNamedPlayerItem( const char *pszItemName ); + BOOL HasWeapons( void );// do I have ANY weapons? + void SelectPrevItem( int iItem ); + void SelectNextItem( int iItem ); + void SelectLastItem(void); + void SelectItem(const char *pstr); + void QueueItem(CBasePlayerWeapon *pItem); + void ItemPreFrame( void ); + void ItemPostFrame( void ); + void GiveNamedItem( const char *szName ); + void EnableControl(BOOL fControl); + + int GiveAmmo( int iAmount, char *szName, int iMax ); + void SendAmmoUpdate(void); + + void WaterMove( void ); + void CheckWaterJump( void ); + void EXPORT PlayerDeathThink( void ); + void PlayerUse( void ); + + void CheckSuitUpdate(); + void SetSuitUpdate(char *name, int fgroup, int iNoRepeat); + void UpdateGeigerCounter( void ); + void CheckTimeBasedDamage( void ); + void UpdateStepSound( void ); + void PlayStepSound(int step, float fvol); + + BOOL FBecomeProne ( void ); + void BarnacleVictimBitten ( entvars_t *pevBarnacle ); + void BarnacleVictimReleased ( void ); + static int GetAmmoIndex(const char *psz); + const char *GetAmmoName(int index ); + int AmmoInventory( int iAmmoIndex ); + int Illumination( void ); + + void ResetAutoaim( void ); + Vector GetAutoaimVector( float flDelta ); + Vector AutoaimDeflection( Vector &vecSrc, float flDist, float flDelta ); + + void ForceClientDllUpdate( void ); // Forces all client .dll specific data to be resent to client. + + void DeathMessage( entvars_t *pevKiller ); + void SendStartMessages( void ); + + void SetCustomDecalFrames( int nFrames ); + int GetCustomDecalFrames( void ); + + //Player ID + void InitStatusBar( void ); + void UpdateStatusBar( void ); + int m_izSBarState[ SBAR_END ]; + float m_flNextSBarUpdateTime; + float m_flStatusBarDisappearDelay; + char m_SbarString0[ SBAR_STRING_SIZE ]; + char m_SbarString1[ SBAR_STRING_SIZE ]; + // for trigger_viewset + CBaseEntity *pViewEnt; // entity to look at + int viewFlags; // 1-active, 2-draw hud, 4 - inverse x, 8 - monster look + int viewNeedsUpdate; // precache sets to 1, UpdateClientData() sets to 0 + + // for trigger_sound + byte m_iSndRoomtype; // last roomtype set by sound entity + int hearNeedsUpdate; // update DSP contents + + //warhud message + int m_iWarHUD; + int m_iClientWarHUD; + + //fog variables + int m_iFogStartDist; + int m_iFogEndDist; + int m_iFogFinalEndDist; + int m_FogFadeTime; + Vector m_FogColor; + int fogNeedsUpdate; + + //fade fariables + Vector m_FadeColor; + int m_FadeAlpha; + int m_iFadeFlags; + int m_iFadeTime; + int m_iFadeHold; + int fadeNeedsUpdate; + int m_iStartMessage; + + float m_flStartTime; //start time + + float m_flNextChatTime; + int Rain_dripsPerSecond; + float Rain_windX, Rain_windY; + float Rain_randX, Rain_randY; + + int Rain_ideal_dripsPerSecond; + float Rain_ideal_windX, Rain_ideal_windY; + float Rain_ideal_randX, Rain_ideal_randY; + + float Rain_endFade; // 0 means off + float Rain_nextFadeUpdate; + + int rainNeedsUpdate; + Vector v_LastAngles; +}; + +#define AUTOAIM_2DEGREES 0.0348994967025 +#define AUTOAIM_5DEGREES 0.08715574274766 +#define AUTOAIM_8DEGREES 0.1391731009601 +#define AUTOAIM_10DEGREES 0.1736481776669 + + +extern int gmsgHudText; +extern int gmsgSetBody; +extern int gmsgSetSkin; +extern int gmsgZoomHUD; +extern int gmsgWarHUD; +extern BOOL gInitHUD; + +#endif // PLAYER_H diff --git a/server/monsters/rat.cpp b/server/monsters/rat.cpp new file mode 100644 index 00000000..9eef8202 --- /dev/null +++ b/server/monsters/rat.cpp @@ -0,0 +1,104 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// rat - environmental monster +//========================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= + +class CRat : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed( void ); + int Classify ( void ); +}; +LINK_ENTITY_TO_CLASS( monster_rat, CRat ); + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CRat :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_INSECT; //LRC- maybe someone needs to give them a basic biology lesson... +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CRat :: SetYawSpeed ( void ) +{ + int ys; + + switch ( m_Activity ) + { + case ACT_IDLE: + default: + ys = 45; + break; + } + + pev->yaw_speed = ys; +} + +//========================================================= +// Spawn +//========================================================= +void CRat :: Spawn() +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/bigrat.mdl"); + UTIL_SetSize( pev, Vector( 0, 0, 0 ), Vector( 0, 0, 0 ) ); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_RED; + pev->health = 8; + pev->view_ofs = Vector ( 0, 0, 6 );// position of the eyes relative to monster's origin. + m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + + MonsterInit(); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CRat :: Precache() +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/bigrat.mdl"); +} + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= diff --git a/server/monsters/roach.cpp b/server/monsters/roach.cpp new file mode 100644 index 00000000..b84e1985 --- /dev/null +++ b/server/monsters/roach.cpp @@ -0,0 +1,466 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// cockroach +//========================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "soundent.h" +#include "decals.h" + +#define ROACH_IDLE 0 +#define ROACH_BORED 1 +#define ROACH_SCARED_BY_ENT 2 +#define ROACH_SCARED_BY_LIGHT 3 +#define ROACH_SMELL_FOOD 4 +#define ROACH_EAT 5 + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +class CRoach : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed( void ); + void EXPORT MonsterThink ( void ); + void Move ( float flInterval ); + void PickNewDest ( int iCondition ); + void EXPORT Touch ( CBaseEntity *pOther ); + void Killed( entvars_t *pevAttacker, int iGib ); + + float m_flLastLightLevel; + float m_flNextSmellTime; + int Classify ( void ); + void Look ( int iDistance ); + int ISoundMask ( void ); + + // UNDONE: These don't necessarily need to be save/restored, but if we add more data, it may + BOOL m_fLightHacked; + int m_iMode; + // ----------------------------- +}; +LINK_ENTITY_TO_CLASS( monster_cockroach, CRoach ); + +//========================================================= +// ISoundMask - returns a bit mask indicating which types +// of sounds this monster regards. In the base class implementation, +// monsters care about all sounds, but no scents. +//========================================================= +int CRoach :: ISoundMask ( void ) +{ + return bits_SOUND_CARCASS | bits_SOUND_MEAT; +} + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CRoach :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_INSECT; +} + +//========================================================= +// Touch +//========================================================= +void CRoach :: Touch ( CBaseEntity *pOther ) +{ + Vector vecSpot; + TraceResult tr; + + if ( pOther->pev->velocity == g_vecZero || !pOther->IsPlayer() ) + { + return; + } + + vecSpot = pev->origin + Vector ( 0 , 0 , 8 );//move up a bit, and trace down. + UTIL_TraceLine ( vecSpot, vecSpot + Vector ( 0, 0, -24 ), ignore_monsters, ENT(pev), & tr); + + // This isn't really blood. So you don't have to screen it out based on violence levels (UTIL_ShouldShowBlood()) + UTIL_DecalTrace( &tr, DECAL_YBLOOD1 +RANDOM_LONG(0,5) ); + + TakeDamage( pOther->pev, pOther->pev, pev->health, DMG_CRUSH ); +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CRoach :: SetYawSpeed ( void ) +{ + int ys; + + ys = 120; + + pev->yaw_speed = ys; +} + +//========================================================= +// Spawn +//========================================================= +void CRoach :: Spawn() +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/roach.mdl"); + UTIL_SetSize( pev, Vector( -1, -1, 0 ), Vector( 1, 1, 2 ) ); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_YELLOW; + pev->effects = 0; + pev->health = 1; + m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + + MonsterInit(); + SetActivity ( ACT_IDLE ); + + pev->view_ofs = Vector ( 0, 0, 1 );// position of the eyes relative to monster's origin. + pev->takedamage = DAMAGE_YES; + m_fLightHacked = FALSE; + m_flLastLightLevel = -1; + m_iMode = ROACH_IDLE; + m_flNextSmellTime = gpGlobals->time; +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CRoach :: Precache() +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/roach.mdl"); + + PRECACHE_SOUND("roach/rch_die.wav"); + PRECACHE_SOUND("roach/rch_walk.wav"); + PRECACHE_SOUND("roach/rch_smash.wav"); +} + + +//========================================================= +// Killed. +//========================================================= +void CRoach :: Killed( entvars_t *pevAttacker, int iGib ) +{ + pev->solid = SOLID_NOT; + + //random sound + if ( RANDOM_LONG(0,4) == 1 ) + { + EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, "roach/rch_die.wav", 0.8, ATTN_NORM, 0, 80 + RANDOM_LONG(0,39) ); + } + else + { + EMIT_SOUND_DYN(ENT(pev), CHAN_BODY, "roach/rch_smash.wav", 0.7, ATTN_NORM, 0, 80 + RANDOM_LONG(0,39) ); + } + + CSoundEnt::InsertSound ( bits_SOUND_WORLD, pev->origin, 128, 1 ); + + CBaseEntity *pOwner = CBaseEntity::Instance(pev->owner); + if ( pOwner ) + { + pOwner->DeathNotice( pev ); + } + UTIL_Remove( this ); +} + +//========================================================= +// MonsterThink, overridden for roaches. +//========================================================= +void CRoach :: MonsterThink( void ) +{ + if ( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) && !HaveCamerasInPVS( edict() )) + SetNextThink( RANDOM_FLOAT(1,1.5) ); + else + SetNextThink( 0.1 );// keep monster thinking + + float flInterval = StudioFrameAdvance( ); // animate + + if ( !m_fLightHacked ) + { + // if light value hasn't been collection for the first time yet, + // suspend the creature for a second so the world finishes spawning, then we'll collect the light level. + SetNextThink( 1 ); + m_fLightHacked = TRUE; + return; + } + else if ( m_flLastLightLevel < 0 ) + { + // collect light level for the first time, now that all of the lightmaps in the roach's area have been calculated. + m_flLastLightLevel = GETENTITYILLUM( ENT( pev ) ); + } + + switch ( m_iMode ) + { + case ROACH_IDLE: + case ROACH_EAT: + { + // if not moving, sample environment to see if anything scary is around. Do a radius search 'look' at random. + if ( RANDOM_LONG(0,3) == 1 ) + { + Look( 150 ); + if (HasConditions(bits_COND_SEE_FEAR)) + { + // if see something scary + //ALERT ( at_aiconsole, "Scared\n" ); + Eat( 30 + ( RANDOM_LONG(0,14) ) );// roach will ignore food for 30 to 45 seconds + PickNewDest( ROACH_SCARED_BY_ENT ); + SetActivity ( ACT_WALK ); + } + else if ( RANDOM_LONG(0,149) == 1 ) + { + // if roach doesn't see anything, there's still a chance that it will move. (boredom) + //ALERT ( at_aiconsole, "Bored\n" ); + PickNewDest( ROACH_BORED ); + SetActivity ( ACT_WALK ); + + if ( m_iMode == ROACH_EAT ) + { + // roach will ignore food for 30 to 45 seconds if it got bored while eating. + Eat( 30 + ( RANDOM_LONG(0,14) ) ); + } + } + } + + // don't do this stuff if eating! + if ( m_iMode == ROACH_IDLE ) + { + if ( FShouldEat() ) + { + Listen(); + } + + if ( GETENTITYILLUM( ENT(pev) ) > m_flLastLightLevel ) + { + // someone turned on lights! + //ALERT ( at_console, "Lights!\n" ); + PickNewDest( ROACH_SCARED_BY_LIGHT ); + SetActivity ( ACT_WALK ); + } + else if ( HasConditions(bits_COND_SMELL_FOOD) ) + { + CSound *pSound; + + pSound = CSoundEnt::SoundPointerForIndex( m_iAudibleList ); + + // roach smells food and is just standing around. Go to food unless food isn't on same z-plane. + if ( pSound && abs( pSound->m_vecOrigin.z - pev->origin.z ) <= 3 ) + { + PickNewDest( ROACH_SMELL_FOOD ); + SetActivity ( ACT_WALK ); + } + } + } + + break; + } + case ROACH_SCARED_BY_LIGHT: + { + // if roach was scared by light, then stop if we're over a spot at least as dark as where we started! + if ( GETENTITYILLUM( ENT( pev ) ) <= m_flLastLightLevel ) + { + SetActivity ( ACT_IDLE ); + m_flLastLightLevel = GETENTITYILLUM( ENT ( pev ) );// make this our new light level. + } + break; + } + } + + if ( m_flGroundSpeed != 0 ) + { + Move( flInterval ); + } +} + +//========================================================= +// Picks a new spot for roach to run to.( +//========================================================= +void CRoach :: PickNewDest ( int iCondition ) +{ + Vector vecNewDir; + Vector vecDest; + float flDist; + + m_iMode = iCondition; + + if ( m_iMode == ROACH_SMELL_FOOD ) + { + // find the food and go there. + CSound *pSound; + + pSound = CSoundEnt::SoundPointerForIndex( m_iAudibleList ); + + if ( pSound ) + { + m_Route[ 0 ].vecLocation.x = pSound->m_vecOrigin.x + ( 3 - RANDOM_LONG(0,5) ); + m_Route[ 0 ].vecLocation.y = pSound->m_vecOrigin.y + ( 3 - RANDOM_LONG(0,5) ); + m_Route[ 0 ].vecLocation.z = pSound->m_vecOrigin.z; + m_Route[ 0 ].iType = bits_MF_TO_LOCATION; + m_movementGoal = RouteClassify( m_Route[ 0 ].iType ); + return; + } + } + + do + { + // picks a random spot, requiring that it be at least 128 units away + // else, the roach will pick a spot too close to itself and run in + // circles. this is a hack but buys me time to work on the real monsters. + vecNewDir.x = RANDOM_FLOAT( -1, 1 ); + vecNewDir.y = RANDOM_FLOAT( -1, 1 ); + flDist = 256 + ( RANDOM_LONG(0,255) ); + vecDest = pev->origin + vecNewDir * flDist; + + } while ( ( vecDest - pev->origin ).Length2D() < 128 ); + + m_Route[ 0 ].vecLocation.x = vecDest.x; + m_Route[ 0 ].vecLocation.y = vecDest.y; + m_Route[ 0 ].vecLocation.z = pev->origin.z; + m_Route[ 0 ].iType = bits_MF_TO_LOCATION; + m_movementGoal = RouteClassify( m_Route[ 0 ].iType ); + + if ( RANDOM_LONG(0,9) == 1 ) + { + // every once in a while, a roach will play a skitter sound when they decide to run + EMIT_SOUND_DYN(ENT(pev), CHAN_BODY, "roach/rch_walk.wav", 1, ATTN_NORM, 0, 80 + RANDOM_LONG(0,39) ); + } +} + +//========================================================= +// roach's move function +//========================================================= +void CRoach :: Move ( float flInterval ) +{ + float flWaypointDist; + Vector vecApex; + + // local move to waypoint. + flWaypointDist = ( m_Route[ m_iRouteIndex ].vecLocation - pev->origin ).Length2D(); + MakeIdealYaw ( m_Route[ m_iRouteIndex ].vecLocation ); + + ChangeYaw ( pev->yaw_speed ); + UTIL_MakeVectors( pev->angles ); + + if ( RANDOM_LONG(0,7) == 1 ) + { + // randomly check for blocked path.(more random load balancing) + if ( !WALK_MOVE( ENT(pev), pev->ideal_yaw, 4, WALKMOVE_NORMAL ) ) + { + // stuck, so just pick a new spot to run off to + PickNewDest( m_iMode ); + } + } + + WALK_MOVE( ENT(pev), pev->ideal_yaw, m_flGroundSpeed * flInterval, WALKMOVE_NORMAL ); + + // if the waypoint is closer than step size, then stop after next step (ok for roach to overshoot) + if ( flWaypointDist <= m_flGroundSpeed * flInterval ) + { + // take truncated step and stop + + SetActivity ( ACT_IDLE ); + m_flLastLightLevel = GETENTITYILLUM( ENT ( pev ) );// this is roach's new comfortable light level + + if ( m_iMode == ROACH_SMELL_FOOD ) + { + m_iMode = ROACH_EAT; + } + else + { + m_iMode = ROACH_IDLE; + } + } + + if ( RANDOM_LONG(0,149) == 1 && m_iMode != ROACH_SCARED_BY_LIGHT && m_iMode != ROACH_SMELL_FOOD ) + { + // random skitter while moving as long as not on a b-line to get out of light or going to food + PickNewDest( FALSE ); + } +} + +//========================================================= +// Look - overriden for the roach, which can virtually see +// 360 degrees. +//========================================================= +void CRoach :: Look ( int iDistance ) +{ + CBaseEntity *pSightEnt = NULL;// the current visible entity that we're dealing with + CBaseEntity *pPreviousEnt;// the last entity added to the link list + int iSighted = 0; + + // DON'T let visibility information from last frame sit around! + ClearConditions( bits_COND_SEE_HATE |bits_COND_SEE_DISLIKE | bits_COND_SEE_ENEMY | bits_COND_SEE_FEAR ); + + // don't let monsters outside of the player's PVS act up, or most of the interesting + // things will happen before the player gets there! + if ( FNullEnt( FIND_CLIENT_IN_PVS( edict() ) ) && !HaveCamerasInPVS( edict() )) + { + return; + } + + m_pLink = NULL; + pPreviousEnt = this; + + // Does sphere also limit itself to PVS? + // Examine all entities within a reasonable radius + // !!!PERFORMANCE - let's trivially reject the ent list before radius searching! + while ((pSightEnt = UTIL_FindEntityInSphere( pSightEnt, pev->origin, iDistance )) != NULL) + { + // only consider ents that can be damaged. !!!temporarily only considering other monsters and clients + if ( pSightEnt->IsPlayer() || FBitSet ( pSightEnt->pev->flags, FL_MONSTER ) ) + { + if ( /*FVisible( pSightEnt ) &&*/ !FBitSet( pSightEnt->pev->flags, FL_NOTARGET ) && pSightEnt->pev->health > 0 ) + { + // NULL the Link pointer for each ent added to the link list. If other ents follow, the will overwrite + // this value. If this ent happens to be the last, the list will be properly terminated. + pPreviousEnt->m_pLink = pSightEnt; + pSightEnt->m_pLink = NULL; + pPreviousEnt = pSightEnt; + + // don't add the Enemy's relationship to the conditions. We only want to worry about conditions when + // we see monsters other than the Enemy. + switch ( IRelationship ( pSightEnt ) ) + { + case R_FR: + iSighted |= bits_COND_SEE_FEAR; + break; + case R_NO: + break; + default: + ALERT ( at_debug, "%s can't assess %s\n", STRING(pev->classname), STRING(pSightEnt->pev->classname ) ); + break; + } + } + } + } + SetConditions( iSighted ); +} + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= + diff --git a/server/monsters/schedule.h b/server/monsters/schedule.h new file mode 100644 index 00000000..59e4079d --- /dev/null +++ b/server/monsters/schedule.h @@ -0,0 +1,292 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// Scheduling +//========================================================= + +#ifndef SCHEDULE_H +#define SCHEDULE_H + +#define TASKSTATUS_NEW 0 // Just started +#define TASKSTATUS_RUNNING 1 // Running task & movement +#define TASKSTATUS_RUNNING_MOVEMENT 2 // Just running movement +#define TASKSTATUS_RUNNING_TASK 3 // Just running task +#define TASKSTATUS_COMPLETE 4 // Completed, get next task + + +//========================================================= +// These are the schedule types +//========================================================= +typedef enum +{ + SCHED_NONE = 0, + SCHED_IDLE_STAND, + SCHED_IDLE_WALK, + SCHED_WAKE_ANGRY, + SCHED_WAKE_CALLED, + SCHED_ALERT_FACE, + SCHED_ALERT_SMALL_FLINCH, + SCHED_ALERT_BIG_FLINCH, + SCHED_ALERT_STAND, + SCHED_INVESTIGATE_SOUND, + SCHED_COMBAT_FACE, + SCHED_COMBAT_STAND, + SCHED_CHASE_ENEMY, + SCHED_CHASE_ENEMY_FAILED, + SCHED_VICTORY_DANCE, + SCHED_TARGET_FACE, + SCHED_TARGET_CHASE, + SCHED_SMALL_FLINCH, + SCHED_TAKE_COVER_FROM_ENEMY, + SCHED_TAKE_COVER_FROM_BEST_SOUND, + SCHED_TAKE_COVER_FROM_ORIGIN, + SCHED_COWER, // usually a last resort! + SCHED_MELEE_ATTACK1, + SCHED_MELEE_ATTACK2, + SCHED_RANGE_ATTACK1, + SCHED_RANGE_ATTACK2, + SCHED_SPECIAL_ATTACK1, + SCHED_SPECIAL_ATTACK2, + SCHED_STANDOFF, + SCHED_ARM_WEAPON, + SCHED_RELOAD, + SCHED_GUARD, + SCHED_AMBUSH, + SCHED_DIE, + SCHED_WAIT_TRIGGER, + SCHED_FOLLOW, + SCHED_SLEEP, + SCHED_WAKE, + SCHED_BARNACLE_VICTIM_GRAB, + SCHED_BARNACLE_VICTIM_CHOMP, + SCHED_AISCRIPT, + SCHED_FAIL, + + LAST_COMMON_SCHEDULE // Leave this at the bottom +} SCHEDULE_TYPE; + +//========================================================= +// These are the shared tasks +//========================================================= +typedef enum +{ + TASK_INVALID = 0, + TASK_WAIT, + TASK_WAIT_FACE_ENEMY, + TASK_WAIT_PVS, + TASK_SUGGEST_STATE, + TASK_WALK_TO_SCRIPT, + TASK_RUN_TO_SCRIPT, + TASK_MOVE_TO_TARGET_RANGE, + TASK_GET_PATH_TO_ENEMY, + TASK_GET_PATH_TO_ENEMY_LKP, + TASK_GET_PATH_TO_ENEMY_CORPSE, + TASK_GET_PATH_TO_LEADER, + TASK_GET_PATH_TO_SPOT, + TASK_GET_PATH_TO_TARGET, + TASK_GET_PATH_TO_SCRIPT, + TASK_GET_PATH_TO_HINTNODE, + TASK_GET_PATH_TO_LASTPOSITION, + TASK_GET_PATH_TO_BESTSOUND, + TASK_GET_PATH_TO_BESTSCENT, + TASK_RUN_PATH, + TASK_WALK_PATH, + TASK_STRAFE_PATH, + TASK_CLEAR_MOVE_WAIT, + TASK_STORE_LASTPOSITION, + TASK_CLEAR_LASTPOSITION, + TASK_PLAY_ACTIVE_IDLE, + TASK_FIND_HINTNODE, + TASK_CLEAR_HINTNODE, + TASK_SMALL_FLINCH, + TASK_FACE_IDEAL, + TASK_FACE_ROUTE, + TASK_FACE_ENEMY, + TASK_FACE_HINTNODE, + TASK_FACE_TARGET, + TASK_FACE_LASTPOSITION, + TASK_RANGE_ATTACK1, + TASK_RANGE_ATTACK2, + TASK_MELEE_ATTACK1, + TASK_MELEE_ATTACK2, + TASK_RELOAD, + TASK_RANGE_ATTACK1_NOTURN, + TASK_RANGE_ATTACK2_NOTURN, + TASK_MELEE_ATTACK1_NOTURN, + TASK_MELEE_ATTACK2_NOTURN, + TASK_RELOAD_NOTURN, + TASK_SPECIAL_ATTACK1, + TASK_SPECIAL_ATTACK2, + TASK_CROUCH, + TASK_STAND, + TASK_GUARD, + TASK_STEP_LEFT, + TASK_STEP_RIGHT, + TASK_STEP_FORWARD, + TASK_STEP_BACK, + TASK_DODGE_LEFT, + TASK_DODGE_RIGHT, + TASK_SOUND_ANGRY, + TASK_SOUND_DEATH, + TASK_SET_ACTIVITY, + TASK_SET_SCHEDULE, + TASK_SET_FAIL_SCHEDULE, + TASK_CLEAR_FAIL_SCHEDULE, + TASK_PLAY_SEQUENCE, + TASK_PLAY_SEQUENCE_FACE_ENEMY, + TASK_PLAY_SEQUENCE_FACE_TARGET, + TASK_SOUND_IDLE, + TASK_SOUND_WAKE, + TASK_SOUND_PAIN, + TASK_SOUND_DIE, + TASK_FIND_COVER_FROM_BEST_SOUND,// tries lateral cover first, then node cover + TASK_FIND_COVER_FROM_ENEMY,// tries lateral cover first, then node cover + TASK_FIND_LATERAL_COVER_FROM_ENEMY, + TASK_FIND_NODE_COVER_FROM_ENEMY, + TASK_FIND_NEAR_NODE_COVER_FROM_ENEMY,// data for this one is the MAXIMUM acceptable distance to the cover. + TASK_FIND_FAR_NODE_COVER_FROM_ENEMY,// data for this one is there MINIMUM aceptable distance to the cover. + TASK_FIND_COVER_FROM_ORIGIN, + TASK_EAT, + TASK_DIE, + TASK_WAIT_FOR_SCRIPT, + TASK_PLAY_SCRIPT, + TASK_ENABLE_SCRIPT, + TASK_PLANT_ON_SCRIPT, + TASK_FACE_SCRIPT, + TASK_END_SCRIPT, //LRC + TASK_WAIT_RANDOM, + TASK_WAIT_INDEFINITE, + TASK_STOP_MOVING, + TASK_TURN_LEFT, + TASK_TURN_RIGHT, + TASK_REMEMBER, + TASK_FORGET, + TASK_WAIT_FOR_MOVEMENT, // wait until MovementIsComplete() + LAST_COMMON_TASK, // LEAVE THIS AT THE BOTTOM!! (sjb) +} SHARED_TASKS; + + +// These go in the flData member of the TASK_WALK_TO_TARGET, TASK_RUN_TO_TARGET +enum +{ + TARGET_MOVE_NORMAL = 0, + TARGET_MOVE_SCRIPTED = 1, +}; + + +// A goal should be used for a task that requires several schedules to complete. +// The goal index should indicate which schedule (ordinally) the monster is running. +// That way, when tasks fail, the AI can make decisions based on the context of the +// current goal and sequence rather than just the current schedule. +enum +{ + GOAL_ATTACK_ENEMY, + GOAL_MOVE, + GOAL_TAKE_COVER, + GOAL_MOVE_TARGET, + GOAL_EAT, +}; + +// an array of tasks is a task list +// an array of schedules is a schedule list +struct Task_t +{ + + int iTask; + float flData; +}; + +struct Schedule_t +{ + + Task_t *pTasklist; + int cTasks; + int iInterruptMask;// a bit mask of conditions that can interrupt this schedule + + // a more specific mask that indicates which TYPES of sounds will interrupt the schedule in the + // event that the schedule is broken by COND_HEAR_SOUND + int iSoundMask; + const char *pName; +}; + +// an array of waypoints makes up the monster's route. +// !!!LATER- this declaration doesn't belong in this file. +struct WayPoint_t +{ + Vector vecLocation; + int iType; +}; + +// these MoveFlag values are assigned to a WayPoint's TYPE in order to demonstrate the +// type of movement the monster should use to get there. +#define bits_MF_TO_TARGETENT ( 1 << 0 ) // local move to targetent. +#define bits_MF_TO_ENEMY ( 1 << 1 ) // local move to enemy +#define bits_MF_TO_COVER ( 1 << 2 ) // local move to a hiding place +#define bits_MF_TO_DETOUR ( 1 << 3 ) // local move to detour point. +#define bits_MF_TO_PATHCORNER ( 1 << 4 ) // local move to a path corner +#define bits_MF_TO_NODE ( 1 << 5 ) // local move to a node +#define bits_MF_TO_LOCATION ( 1 << 6 ) // local move to an arbitrary point +#define bits_MF_IS_GOAL ( 1 << 7 ) // this waypoint is the goal of the whole move. +#define bits_MF_DONT_SIMPLIFY ( 1 << 8 ) // Don't let the route code simplify this waypoint + +// If you define any flags that aren't _TO_ flags, add them here so we can mask +// them off when doing compares. +#define bits_MF_NOT_TO_MASK (bits_MF_IS_GOAL | bits_MF_DONT_SIMPLIFY) + +#define MOVEGOAL_NONE (0) +#define MOVEGOAL_TARGETENT (bits_MF_TO_TARGETENT) +#define MOVEGOAL_ENEMY (bits_MF_TO_ENEMY) +#define MOVEGOAL_PATHCORNER (bits_MF_TO_PATHCORNER) +#define MOVEGOAL_LOCATION (bits_MF_TO_LOCATION) +#define MOVEGOAL_NODE (bits_MF_TO_NODE) + +// these bits represent conditions that may befall the monster, of which some are allowed +// to interrupt certain schedules. +#define bits_COND_NO_AMMO_LOADED ( 1 << 0 ) // weapon needs to be reloaded! +#define bits_COND_SEE_HATE ( 1 << 1 ) // see something that you hate +#define bits_COND_SEE_FEAR ( 1 << 2 ) // see something that you are afraid of +#define bits_COND_SEE_DISLIKE ( 1 << 3 ) // see something that you dislike +#define bits_COND_SEE_ENEMY ( 1 << 4 ) // target entity is in full view. +#define bits_COND_ENEMY_OCCLUDED ( 1 << 5 ) // target entity occluded by the world +#define bits_COND_SMELL_FOOD ( 1 << 6 ) +#define bits_COND_ENEMY_TOOFAR ( 1 << 7 ) +#define bits_COND_LIGHT_DAMAGE ( 1 << 8 ) // hurt a little +#define bits_COND_HEAVY_DAMAGE ( 1 << 9 ) // hurt a lot +#define bits_COND_CAN_RANGE_ATTACK1 ( 1 << 10) +#define bits_COND_CAN_MELEE_ATTACK1 ( 1 << 11) +#define bits_COND_CAN_RANGE_ATTACK2 ( 1 << 12) +#define bits_COND_CAN_MELEE_ATTACK2 ( 1 << 13) +// #define bits_COND_CAN_RANGE_ATTACK3 ( 1 << 14) +#define bits_COND_PROVOKED ( 1 << 15) +#define bits_COND_NEW_ENEMY ( 1 << 16) +#define bits_COND_HEAR_SOUND ( 1 << 17) // there is an interesting sound +#define bits_COND_SMELL ( 1 << 18) // there is an interesting scent +#define bits_COND_ENEMY_FACING_ME ( 1 << 19) // enemy is facing me +#define bits_COND_ENEMY_DEAD ( 1 << 20) // enemy was killed. If you get this in combat, try to find another enemy. If you get it in alert, victory dance. +#define bits_COND_SEE_CLIENT ( 1 << 21) // see a client +#define bits_COND_SEE_NEMESIS ( 1 << 22) // see my nemesis + +#define bits_COND_SPECIAL1 ( 1 << 28) // Defined by individual monster +#define bits_COND_SPECIAL2 ( 1 << 29) // Defined by individual monster + +#define bits_COND_TASK_FAILED ( 1 << 30) +#define bits_COND_SCHEDULE_DONE ( 1 << 31) + + +#define bits_COND_ALL_SPECIAL (bits_COND_SPECIAL1 | bits_COND_SPECIAL2) + +#define bits_COND_CAN_ATTACK (bits_COND_CAN_RANGE_ATTACK1 | bits_COND_CAN_MELEE_ATTACK1 | bits_COND_CAN_RANGE_ATTACK2 | bits_COND_CAN_MELEE_ATTACK2) + +#endif // SCHEDULE_H diff --git a/server/monsters/scientist.cpp b/server/monsters/scientist.cpp new file mode 100644 index 00000000..993d8ac9 --- /dev/null +++ b/server/monsters/scientist.cpp @@ -0,0 +1,1458 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// human scientist (passive lab worker) +//========================================================= + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "talkmonster.h" +#include "schedule.h" +#include "defaultai.h" +#include "scripted.h" +#include "animation.h" +#include "soundent.h" +#include "defaults.h" + + +#define NUM_SCIENTIST_HEADS 4 // four heads available for scientist model +enum { HEAD_GLASSES = 0, HEAD_EINSTEIN = 1, HEAD_LUTHER = 2, HEAD_SLICK = 3 }; + +enum +{ + SCHED_HIDE = LAST_TALKMONSTER_SCHEDULE + 1, + SCHED_FEAR, + SCHED_PANIC, + SCHED_STARTLE, + SCHED_TARGET_CHASE_SCARED, + SCHED_TARGET_FACE_SCARED, +}; + +enum +{ + TASK_SAY_HEAL = LAST_TALKMONSTER_TASK + 1, + TASK_HEAL, + TASK_SAY_FEAR, + TASK_RUN_PATH_SCARED, + TASK_SCREAM, + TASK_RANDOM_SCREAM, + TASK_MOVE_TO_TARGET_RANGE_SCARED, +}; + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +#define SCIENTIST_AE_HEAL ( 1 ) +#define SCIENTIST_AE_NEEDLEON ( 2 ) +#define SCIENTIST_AE_NEEDLEOFF ( 3 ) + +//======================================================= +// Scientist +//======================================================= + +class CScientist : public CTalkMonster +{ +public: + void Spawn( void ); + void Precache( void ); + + void SetYawSpeed( void ); + int Classify ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + void RunTask( Task_t *pTask ); + void StartTask( Task_t *pTask ); + int ObjectCaps( void ) { return CTalkMonster :: ObjectCaps() | FCAP_IMPULSE_USE; } + int TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType); + virtual int FriendNumber( int arrayNumber ); + void SetActivity ( Activity newActivity ); + Activity GetStoppedActivity( void ); + int ISoundMask( void ); + void DeclineFollowing( void ); + + float CoverRadius( void ) { return 1200; } // Need more room for cover because scientists want to get far away! + BOOL DisregardEnemy( CBaseEntity *pEnemy ) { return !pEnemy->IsAlive() || (gpGlobals->time - m_fearTime) > 15; } + + BOOL CanHeal( void ); + void Heal( void ); + void Scream( void ); + + // Override these to set behavior + Schedule_t *GetScheduleOfType ( int Type ); + Schedule_t *GetSchedule ( void ); + MONSTERSTATE GetIdealState ( void ); + + void DeathSound( void ); + void PainSound( void ); + + void TalkInit( void ); + + void Killed( entvars_t *pevAttacker, int iGib ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + CUSTOM_SCHEDULES; + +private: + float m_painTime; + float m_healTime; + float m_fearTime; +}; + +LINK_ENTITY_TO_CLASS( monster_scientist, CScientist ); + +TYPEDESCRIPTION CScientist::m_SaveData[] = +{ + DEFINE_FIELD( CScientist, m_painTime, FIELD_TIME ), + DEFINE_FIELD( CScientist, m_healTime, FIELD_TIME ), + DEFINE_FIELD( CScientist, m_fearTime, FIELD_TIME ), +}; + +IMPLEMENT_SAVERESTORE( CScientist, CTalkMonster ); + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= +Task_t tlFollow[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_CANT_FOLLOW }, // If you fail, bail out of follow + { TASK_MOVE_TO_TARGET_RANGE,(float)128 }, // Move within 128 of target ent (client) +// { TASK_SET_SCHEDULE, (float)SCHED_TARGET_FACE }, +}; + +Schedule_t slFollow[] = +{ + { + tlFollow, + ARRAYSIZE ( tlFollow ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND, + bits_SOUND_COMBAT | + bits_SOUND_DANGER, + "Follow" + }, +}; + +Task_t tlFollowScared[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_TARGET_CHASE },// If you fail, follow normally + { TASK_MOVE_TO_TARGET_RANGE_SCARED,(float)128 }, // Move within 128 of target ent (client) +// { TASK_SET_SCHEDULE, (float)SCHED_TARGET_FACE_SCARED }, +}; + +Schedule_t slFollowScared[] = +{ + { + tlFollowScared, + ARRAYSIZE ( tlFollowScared ), + bits_COND_NEW_ENEMY | + bits_COND_HEAR_SOUND | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE, + bits_SOUND_DANGER, + "FollowScared" + }, +}; + +Task_t tlFaceTargetScared[] = +{ + { TASK_FACE_TARGET, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_CROUCHIDLE }, + { TASK_SET_SCHEDULE, (float)SCHED_TARGET_CHASE_SCARED }, +}; + +Schedule_t slFaceTargetScared[] = +{ + { + tlFaceTargetScared, + ARRAYSIZE ( tlFaceTargetScared ), + bits_COND_HEAR_SOUND | + bits_COND_NEW_ENEMY, + bits_SOUND_DANGER, + "FaceTargetScared" + }, +}; + +Task_t tlStopFollowing[] = +{ + { TASK_CANT_FOLLOW, (float)0 }, +}; + +Schedule_t slStopFollowing[] = +{ + { + tlStopFollowing, + ARRAYSIZE ( tlStopFollowing ), + 0, + 0, + "StopFollowing" + }, +}; + + +Task_t tlHeal[] = +{ + { TASK_MOVE_TO_TARGET_RANGE,(float)50 }, // Move within 60 of target ent (client) + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_TARGET_CHASE }, // If you fail, catch up with that guy! (change this to put syringe away and then chase) + { TASK_FACE_IDEAL, (float)0 }, + { TASK_SAY_HEAL, (float)0 }, + { TASK_PLAY_SEQUENCE_FACE_TARGET, (float)ACT_ARM }, // Whip out the needle + { TASK_HEAL, (float)0 }, // Put it in the player + { TASK_PLAY_SEQUENCE_FACE_TARGET, (float)ACT_DISARM }, // Put away the needle +}; + +Schedule_t slHeal[] = +{ + { + tlHeal, + ARRAYSIZE ( tlHeal ), + 0, // Don't interrupt or he'll end up running around with a needle all the time + 0, + "Heal" + }, +}; + + +Task_t tlFaceTarget[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_TARGET, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_SET_SCHEDULE, (float)SCHED_TARGET_CHASE }, +}; + +Schedule_t slFaceTarget[] = +{ + { + tlFaceTarget, + ARRAYSIZE ( tlFaceTarget ), + bits_COND_CLIENT_PUSH | + bits_COND_NEW_ENEMY | + bits_COND_HEAR_SOUND, + bits_SOUND_COMBAT | + bits_SOUND_DANGER, + "FaceTarget" + }, +}; + + +Task_t tlSciPanic[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_SCREAM, (float)0 }, + { TASK_PLAY_SEQUENCE_FACE_ENEMY, (float)ACT_EXCITED }, // This is really fear-stricken excitement + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, +}; + +Schedule_t slSciPanic[] = +{ + { + tlSciPanic, + ARRAYSIZE ( tlSciPanic ), + 0, + 0, + "SciPanic" + }, +}; + + +Task_t tlIdleSciStand[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_WAIT, (float)2 }, // repick IDLESTAND every two seconds. + { TASK_TLK_HEADRESET, (float)0 }, // reset head position +}; + +Schedule_t slIdleSciStand[] = +{ + { + tlIdleSciStand, + ARRAYSIZE ( tlIdleSciStand ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_SMELL | + bits_COND_CLIENT_PUSH | + bits_COND_PROVOKED, + + bits_SOUND_COMBAT |// sound flags + //bits_SOUND_PLAYER | + //bits_SOUND_WORLD | + bits_SOUND_DANGER | + bits_SOUND_MEAT |// scents + bits_SOUND_CARCASS | + bits_SOUND_GARBAGE, + "IdleSciStand" + + }, +}; + + +Task_t tlScientistCover[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_PANIC }, // If you fail, just panic! + { TASK_STOP_MOVING, (float)0 }, + { TASK_FIND_COVER_FROM_ENEMY, (float)0 }, + { TASK_RUN_PATH_SCARED, (float)0 }, + { TASK_TURN_LEFT, (float)179 }, + { TASK_SET_SCHEDULE, (float)SCHED_HIDE }, +}; + +Schedule_t slScientistCover[] = +{ + { + tlScientistCover, + ARRAYSIZE ( tlScientistCover ), + bits_COND_NEW_ENEMY, + 0, + "ScientistCover" + }, +}; + + + +Task_t tlScientistHide[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_PANIC }, // If you fail, just panic! + { TASK_STOP_MOVING, (float)0 }, + { TASK_PLAY_SEQUENCE, (float)ACT_CROUCH }, + { TASK_SET_ACTIVITY, (float)ACT_CROUCHIDLE }, // FIXME: This looks lame + { TASK_WAIT_RANDOM, (float)10.0 }, +}; + +Schedule_t slScientistHide[] = +{ + { + tlScientistHide, + ARRAYSIZE ( tlScientistHide ), + bits_COND_NEW_ENEMY | + bits_COND_HEAR_SOUND | + bits_COND_SEE_ENEMY | + bits_COND_SEE_HATE | + bits_COND_SEE_FEAR | + bits_COND_SEE_DISLIKE, + bits_SOUND_DANGER, + "ScientistHide" + }, +}; + + +Task_t tlScientistStartle[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_PANIC }, // If you fail, just panic! + { TASK_RANDOM_SCREAM, (float)0.3 }, // Scream 30% of the time + { TASK_STOP_MOVING, (float)0 }, + { TASK_PLAY_SEQUENCE_FACE_ENEMY, (float)ACT_CROUCH }, + { TASK_RANDOM_SCREAM, (float)0.1 }, // Scream again 10% of the time + { TASK_PLAY_SEQUENCE_FACE_ENEMY, (float)ACT_CROUCHIDLE }, + { TASK_WAIT_RANDOM, (float)1.0 }, +}; + +Schedule_t slScientistStartle[] = +{ + { + tlScientistStartle, + ARRAYSIZE ( tlScientistStartle ), + bits_COND_NEW_ENEMY | + bits_COND_SEE_ENEMY | + bits_COND_SEE_HATE | + bits_COND_SEE_FEAR | + bits_COND_SEE_DISLIKE, + 0, + "ScientistStartle" + }, +}; + + + +Task_t tlFear[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_ENEMY, (float)0 }, + { TASK_SAY_FEAR, (float)0 }, +// { TASK_PLAY_SEQUENCE, (float)ACT_FEAR_DISPLAY }, +}; + +Schedule_t slFear[] = +{ + { + tlFear, + ARRAYSIZE ( tlFear ), + bits_COND_NEW_ENEMY, + 0, + "Fear" + }, +}; + + +DEFINE_CUSTOM_SCHEDULES( CScientist ) +{ + slFollow, + slFaceTarget, + slIdleSciStand, + slFear, + slScientistCover, + slScientistHide, + slScientistStartle, + slHeal, + slStopFollowing, + slSciPanic, + slFollowScared, + slFaceTargetScared, +}; + + +IMPLEMENT_CUSTOM_SCHEDULES( CScientist, CTalkMonster ); + + +void CScientist::DeclineFollowing( void ) +{ + Talk( 10 ); + m_hTalkTarget = m_hEnemy; + PlaySentence( m_szGrp[TLK_DECLINE], 2, VOL_NORM, ATTN_NORM ); //LRC +} + + +void CScientist :: Scream( void ) +{ + if ( FOkToSpeak() ) + { + Talk( 10 ); + m_hTalkTarget = m_hEnemy; + PlaySentence( "SC_SCREAM", RANDOM_FLOAT(3, 6), VOL_NORM, ATTN_NORM ); + } +} + + +Activity CScientist::GetStoppedActivity( void ) +{ + if ( m_hEnemy != NULL ) + return ACT_EXCITED; + return CTalkMonster::GetStoppedActivity(); +} + + +void CScientist :: StartTask( Task_t *pTask ) +{ + switch( pTask->iTask ) + { + case TASK_SAY_HEAL: +// if ( FOkToSpeak() ) + Talk( 2 ); + m_hTalkTarget = m_hTargetEnt; + PlaySentence( "SC_HEAL", 2, VOL_NORM, ATTN_IDLE ); + + TaskComplete(); + break; + + case TASK_SCREAM: + Scream(); + TaskComplete(); + break; + + case TASK_RANDOM_SCREAM: + if ( RANDOM_FLOAT( 0, 1 ) < pTask->flData ) + Scream(); + TaskComplete(); + break; + + case TASK_SAY_FEAR: + if ( FOkToSpeak() ) + { + Talk( 2 ); + m_hTalkTarget = m_hEnemy; + if ( m_hEnemy->IsPlayer() ) + PlaySentence( "SC_PLFEAR", 5, VOL_NORM, ATTN_NORM ); + else + PlaySentence( "SC_FEAR", 5, VOL_NORM, ATTN_NORM ); + } + TaskComplete(); + break; + + case TASK_HEAL: + m_IdealActivity = ACT_MELEE_ATTACK1; + break; + + case TASK_RUN_PATH_SCARED: + m_movementActivity = ACT_RUN_SCARED; + break; + + case TASK_MOVE_TO_TARGET_RANGE_SCARED: + { + if ( (m_hTargetEnt->pev->origin - pev->origin).Length() < 1 ) + TaskComplete(); + else + { + m_vecMoveGoal = m_hTargetEnt->pev->origin; + if ( !MoveToTarget( ACT_WALK_SCARED, 0.5 ) ) + TaskFail(); + } + } + break; + + default: + CTalkMonster::StartTask( pTask ); + break; + } +} + +void CScientist :: RunTask( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_RUN_PATH_SCARED: + if ( MovementIsComplete() ) + TaskComplete(); + if ( RANDOM_LONG(0,31) < 8 ) + Scream(); + break; + + case TASK_MOVE_TO_TARGET_RANGE_SCARED: + { + if ( RANDOM_LONG(0,63)< 8 ) + Scream(); + + if ( m_hEnemy == NULL ) + { + TaskFail(); + } + else + { + float distance; + + distance = ( m_vecMoveGoal - pev->origin ).Length2D(); + // Re-evaluate when you think your finished, or the target has moved too far + if ( (distance < pTask->flData) || (m_vecMoveGoal - m_hTargetEnt->pev->origin).Length() > pTask->flData * 0.5 ) + { + m_vecMoveGoal = m_hTargetEnt->pev->origin; + distance = ( m_vecMoveGoal - pev->origin ).Length2D(); + FRefreshRoute(); + } + + // Set the appropriate activity based on an overlapping range + // overlap the range to prevent oscillation + if ( distance < pTask->flData ) + { + TaskComplete(); + RouteClear(); // Stop moving + } + else if ( distance < 190 && m_movementActivity != ACT_WALK_SCARED ) + m_movementActivity = ACT_WALK_SCARED; + else if ( distance >= 270 && m_movementActivity != ACT_RUN_SCARED ) + m_movementActivity = ACT_RUN_SCARED; + } + } + break; + + case TASK_HEAL: + if ( m_fSequenceFinished ) + { + TaskComplete(); + } + else + { + if ( TargetDistance() > 90 ) + TaskComplete(); + pev->ideal_yaw = UTIL_VecToYaw( m_hTargetEnt->pev->origin - pev->origin ); + ChangeYaw( pev->yaw_speed ); + } + break; + default: + CTalkMonster::RunTask( pTask ); + break; + } +} + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CScientist :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_HUMAN_PASSIVE; +} + + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CScientist :: SetYawSpeed ( void ) +{ + int ys; + + ys = 90; + + switch ( m_Activity ) + { + case ACT_IDLE: + ys = 120; + break; + case ACT_WALK: + ys = 180; + break; + case ACT_RUN: + ys = 150; + break; + case ACT_TURN_LEFT: + case ACT_TURN_RIGHT: + ys = 120; + break; + } + + pev->yaw_speed = ys; +} + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +//========================================================= +void CScientist :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case SCIENTIST_AE_HEAL: // Heal my target (if within range) + Heal(); + break; + case SCIENTIST_AE_NEEDLEON: + { + int oldBody = pev->body; + pev->body = (oldBody % NUM_SCIENTIST_HEADS) + NUM_SCIENTIST_HEADS * 1; + } + break; + case SCIENTIST_AE_NEEDLEOFF: + { + int oldBody = pev->body; + pev->body = (oldBody % NUM_SCIENTIST_HEADS) + NUM_SCIENTIST_HEADS * 0; + } + break; + + default: + CTalkMonster::HandleAnimEvent( pEvent ); + } +} + +//========================================================= +// Spawn +//========================================================= +void CScientist :: Spawn( void ) +{ + Precache( ); + + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/scientist.mdl"); + UTIL_SetSize(pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_RED; + if (pev->health == 0) + pev->health = SCIENTIST_HEALTH; + pev->view_ofs = Vector ( 0, 0, 50 );// position of the eyes relative to monster's origin. + m_flFieldOfView = VIEW_FIELD_WIDE; // NOTE: we need a wide field of view so scientists will notice player and say hello + m_MonsterState = MONSTERSTATE_NONE; + +// m_flDistTooFar = 256.0; + + m_afCapability = bits_CAP_HEAR | bits_CAP_TURN_HEAD | bits_CAP_OPEN_DOORS | bits_CAP_AUTO_DOORS | bits_CAP_USE; + + // White hands + pev->skin = 0; + + if ( pev->body == -1 ) + {// -1 chooses a random head + pev->body = RANDOM_LONG(0, NUM_SCIENTIST_HEADS-1);// pick a head, any head + } + + // Luther is black, make his hands black + if ( pev->body == HEAD_LUTHER ) + pev->skin = 1; + + MonsterInit(); + SetUse(&CScientist :: FollowerUse ); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CScientist :: Precache( void ) +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/scientist.mdl"); + PRECACHE_SOUND("scientist/sci_pain1.wav"); + PRECACHE_SOUND("scientist/sci_pain2.wav"); + PRECACHE_SOUND("scientist/sci_pain3.wav"); + PRECACHE_SOUND("scientist/sci_pain4.wav"); + PRECACHE_SOUND("scientist/sci_pain5.wav"); + + // every new scientist must call this, otherwise + // when a level is loaded, nobody will talk (time is reset to 0) + TalkInit(); + + CTalkMonster::Precache(); +} + +// Init talk data +void CScientist :: TalkInit() +{ + + CTalkMonster::TalkInit(); + + // scientist will try to talk to friends in this order: + + m_szFriends[0] = "monster_scientist"; + m_szFriends[1] = "monster_sitting_scientist"; + m_szFriends[2] = "monster_barney"; + + // scientists speach group names (group names are in sentences.txt) + + if (!m_iszSpeakAs) + { + m_szGrp[TLK_ANSWER] = "SC_ANSWER"; + m_szGrp[TLK_QUESTION] = "SC_QUESTION"; + m_szGrp[TLK_IDLE] = "SC_IDLE"; + m_szGrp[TLK_STARE] = "SC_STARE"; + if (pev->spawnflags & SF_MONSTER_PREDISASTER) + m_szGrp[TLK_USE] = "SC_PFOLLOW"; + else + m_szGrp[TLK_USE] = "SC_OK"; + if (pev->spawnflags & SF_MONSTER_PREDISASTER) + m_szGrp[TLK_UNUSE] = "SC_PWAIT"; + else + m_szGrp[TLK_UNUSE] = "SC_WAIT"; + if (pev->spawnflags & SF_MONSTER_PREDISASTER) + m_szGrp[TLK_DECLINE] = "SC_POK"; + else + m_szGrp[TLK_DECLINE] = "SC_NOTOK"; + m_szGrp[TLK_STOP] = "SC_STOP"; + m_szGrp[TLK_NOSHOOT] = "SC_SCARED"; + m_szGrp[TLK_HELLO] = "SC_HELLO"; + + m_szGrp[TLK_PLHURT1] = "!SC_CUREA"; + m_szGrp[TLK_PLHURT2] = "!SC_CUREB"; + m_szGrp[TLK_PLHURT3] = "!SC_CUREC"; + + m_szGrp[TLK_PHELLO] = "SC_PHELLO"; + m_szGrp[TLK_PIDLE] = "SC_PIDLE"; + m_szGrp[TLK_PQUESTION] = "SC_PQUEST"; + m_szGrp[TLK_SMELL] = "SC_SMELL"; + + m_szGrp[TLK_WOUND] = "SC_WOUND"; + m_szGrp[TLK_MORTAL] = "SC_MORTAL"; + } + + // get voice for head + switch (pev->body % 3) + { + default: + case HEAD_GLASSES: m_voicePitch = 105; break; //glasses + case HEAD_EINSTEIN: m_voicePitch = 100; break; //einstein + case HEAD_LUTHER: m_voicePitch = 95; break; //luther + case HEAD_SLICK: m_voicePitch = 100; break;//slick + } +} + +int CScientist :: TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType) +{ + + if ( pevInflictor && pevInflictor->flags & FL_CLIENT ) + { + Remember( bits_MEMORY_PROVOKED ); + StopFollowing( TRUE ); + } + + // make sure friends talk about it if player hurts scientist... + return CTalkMonster::TakeDamage(pevInflictor, pevAttacker, flDamage, bitsDamageType); +} + + +//========================================================= +// ISoundMask - returns a bit mask indicating which types +// of sounds this monster regards. In the base class implementation, +// monsters care about all sounds, but no scents. +//========================================================= +int CScientist :: ISoundMask ( void ) +{ + return bits_SOUND_WORLD | + bits_SOUND_COMBAT | + bits_SOUND_DANGER | + bits_SOUND_PLAYER; +} + +//========================================================= +// PainSound +//========================================================= +void CScientist :: PainSound ( void ) +{ + if (gpGlobals->time < m_painTime ) + return; + + m_painTime = gpGlobals->time + RANDOM_FLOAT(0.5, 0.75); + + switch (RANDOM_LONG(0,4)) + { + case 0: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "scientist/sci_pain1.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 1: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "scientist/sci_pain2.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 2: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "scientist/sci_pain3.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 3: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "scientist/sci_pain4.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + case 4: EMIT_SOUND_DYN( ENT(pev), CHAN_VOICE, "scientist/sci_pain5.wav", 1, ATTN_NORM, 0, GetVoicePitch()); break; + } +} + +//========================================================= +// DeathSound +//========================================================= +void CScientist :: DeathSound ( void ) +{ + PainSound(); +} + + +void CScientist::Killed( entvars_t *pevAttacker, int iGib ) +{ + SetUse( NULL ); + CTalkMonster::Killed( pevAttacker, iGib ); +} + + +void CScientist :: SetActivity ( Activity newActivity ) +{ + int iSequence; + + iSequence = LookupActivity ( newActivity ); + + // Set to the desired anim, or default anim if the desired is not present + if ( iSequence == ACTIVITY_NOT_AVAILABLE ) + newActivity = ACT_IDLE; + CTalkMonster::SetActivity( newActivity ); +} + + +Schedule_t* CScientist :: GetScheduleOfType ( int Type ) +{ + Schedule_t *psched; + + switch( Type ) + { + // Hook these to make a looping schedule + case SCHED_TARGET_FACE: + // call base class default so that scientist will talk + // when 'used' + psched = CTalkMonster::GetScheduleOfType(Type); + + if (psched == slIdleStand) + return slFaceTarget; // override this for different target face behavior + else + return psched; + + case SCHED_TARGET_CHASE: + return slFollow; + + case SCHED_CANT_FOLLOW: + return slStopFollowing; + + case SCHED_PANIC: + return slSciPanic; + + case SCHED_TARGET_CHASE_SCARED: + return slFollowScared; + + case SCHED_TARGET_FACE_SCARED: + return slFaceTargetScared; + + case SCHED_IDLE_STAND: + // call base class default so that scientist will talk + // when standing during idle + psched = CTalkMonster::GetScheduleOfType(Type); + + if (psched == slIdleStand) + return slIdleSciStand; + else + return psched; + + case SCHED_HIDE: + return slScientistHide; + + case SCHED_STARTLE: + return slScientistStartle; + + case SCHED_FEAR: + return slFear; + } + + return CTalkMonster::GetScheduleOfType( Type ); +} + +Schedule_t *CScientist :: GetSchedule ( void ) +{ + // so we don't keep calling through the EHANDLE stuff + CBaseEntity *pEnemy = m_hEnemy; + + if ( HasConditions( bits_COND_HEAR_SOUND ) ) + { + CSound *pSound; + pSound = PBestSound(); + + ASSERT( pSound != NULL ); + if ( pSound && (pSound->m_iType & bits_SOUND_DANGER) ) + return GetScheduleOfType( SCHED_TAKE_COVER_FROM_BEST_SOUND ); + } + + switch( m_MonsterState ) + { + case MONSTERSTATE_ALERT: + case MONSTERSTATE_IDLE: + if ( pEnemy ) + { + if ( HasConditions( bits_COND_SEE_ENEMY ) ) + m_fearTime = gpGlobals->time; + else if ( DisregardEnemy( pEnemy ) ) // After 15 seconds of being hidden, return to alert + { + m_hEnemy = NULL; + pEnemy = NULL; + } + } + + if ( HasConditions(bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE)) + { + // flinch if hurt + return GetScheduleOfType( SCHED_SMALL_FLINCH ); + } + + // Cower when you hear something scary + if ( HasConditions( bits_COND_HEAR_SOUND ) ) + { + CSound *pSound; + pSound = PBestSound(); + + ASSERT( pSound != NULL ); + if ( pSound ) + { + if ( pSound->m_iType & (bits_SOUND_DANGER | bits_SOUND_COMBAT) ) + { + if ( gpGlobals->time - m_fearTime > 3 ) // Only cower every 3 seconds or so + { + m_fearTime = gpGlobals->time; // Update last fear + return GetScheduleOfType( SCHED_STARTLE ); // This will just duck for a second + } + } + } + } + + // Behavior for following the player + if ( IsFollowing() ) + { + if ( !m_hTargetEnt->IsAlive() ) + { + // UNDONE: Comment about the recently dead player here? + StopFollowing( FALSE ); + break; + } + + int relationship = R_NO; + + // Nothing scary, just me and the player + if ( pEnemy != NULL ) + relationship = IRelationship( pEnemy ); + + // UNDONE: Model fear properly, fix R_FR and add multiple levels of fear + if ( relationship != R_DL && relationship != R_HT ) + { + // If I'm already close enough to my target + if ( TargetDistance() <= 128 ) + { + if ( CanHeal() ) // Heal opportunistically + return slHeal; + if ( HasConditions( bits_COND_CLIENT_PUSH ) ) // Player wants me to move + return GetScheduleOfType( SCHED_MOVE_AWAY_FOLLOW ); + } + return GetScheduleOfType( SCHED_TARGET_FACE ); // Just face and follow. + } + else // UNDONE: When afraid, scientist won't move out of your way. Keep This? If not, write move away scared + { + if ( HasConditions( bits_COND_NEW_ENEMY ) ) // I just saw something new and scary, react + return GetScheduleOfType( SCHED_FEAR ); // React to something scary + return GetScheduleOfType( SCHED_TARGET_FACE_SCARED ); // face and follow, but I'm scared! + } + } + + if ( HasConditions( bits_COND_CLIENT_PUSH ) ) // Player wants me to move + return GetScheduleOfType( SCHED_MOVE_AWAY ); + + // try to say something about smells + TrySmellTalk(); + break; + case MONSTERSTATE_COMBAT: + if ( HasConditions( bits_COND_NEW_ENEMY ) ) + return slFear; // Point and scream! + if ( HasConditions( bits_COND_SEE_ENEMY ) ) + return slScientistCover; // Take Cover + + if ( HasConditions( bits_COND_HEAR_SOUND ) ) + return slTakeCoverFromBestSound; // Cower and panic from the scary sound! + + return slScientistCover; // Run & Cower + break; + } + + return CTalkMonster::GetSchedule(); +} + +MONSTERSTATE CScientist :: GetIdealState ( void ) +{ + switch ( m_MonsterState ) + { + case MONSTERSTATE_ALERT: + case MONSTERSTATE_IDLE: + if ( HasConditions( bits_COND_NEW_ENEMY ) ) + { + if ( IsFollowing() ) + { + int relationship = IRelationship( m_hEnemy ); + if ( relationship != R_FR || relationship != R_HT && !HasConditions( bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE ) ) + { + // Don't go to combat if you're following the player + m_IdealMonsterState = MONSTERSTATE_ALERT; + return m_IdealMonsterState; + } + StopFollowing( TRUE ); + } + } + else if ( HasConditions( bits_COND_LIGHT_DAMAGE | bits_COND_HEAVY_DAMAGE ) ) + { + // Stop following if you take damage + if ( IsFollowing() ) + StopFollowing( TRUE ); + } + break; + + case MONSTERSTATE_COMBAT: + { + CBaseEntity *pEnemy = m_hEnemy; + if ( pEnemy != NULL ) + { + if ( DisregardEnemy( pEnemy ) ) // After 15 seconds of being hidden, return to alert + { + // Strip enemy when going to alert + m_IdealMonsterState = MONSTERSTATE_ALERT; + m_hEnemy = NULL; + return m_IdealMonsterState; + } + // Follow if only scared a little + if ( m_hTargetEnt != NULL ) + { + m_IdealMonsterState = MONSTERSTATE_ALERT; + return m_IdealMonsterState; + } + + if ( HasConditions ( bits_COND_SEE_ENEMY ) ) + { + m_fearTime = gpGlobals->time; + m_IdealMonsterState = MONSTERSTATE_COMBAT; + return m_IdealMonsterState; + } + + } + } + break; + } + + return CTalkMonster::GetIdealState(); +} + + +BOOL CScientist::CanHeal( void ) +{ + if ( (m_healTime > gpGlobals->time) || (m_hTargetEnt == NULL) || (m_hTargetEnt->pev->health > (m_hTargetEnt->pev->max_health * 0.5)) ) + return FALSE; + + return TRUE; +} + +void CScientist::Heal( void ) +{ + if ( !CanHeal() ) + return; + + Vector target = m_hTargetEnt->pev->origin - pev->origin; + if ( target.Length() > 100 ) + return; + + m_hTargetEnt->TakeHealth( SCIENTIST_HEAL, DMG_GENERIC ); + // Don't heal again for 1 minute + m_healTime = gpGlobals->time + 60; +} + +int CScientist::FriendNumber( int arrayNumber ) +{ + static int array[3] = { 1, 2, 0 }; + if ( arrayNumber < 3 ) + return array[ arrayNumber ]; + return arrayNumber; +} + + +//========================================================= +// Dead Scientist PROP +//========================================================= +class CDeadScientist : public CBaseMonster +{ +public: + void Spawn( void ); + int Classify ( void ) { return CLASS_HUMAN_PASSIVE; } + + void KeyValue( KeyValueData *pkvd ); + int m_iPose;// which sequence to display + static char *m_szPoses[7]; +}; +char *CDeadScientist::m_szPoses[] = { "lying_on_back", "lying_on_stomach", "dead_sitting", "dead_hang", "dead_table1", "dead_table2", "dead_table3" }; + +void CDeadScientist::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "pose")) + { + m_iPose = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseMonster::KeyValue( pkvd ); +} +LINK_ENTITY_TO_CLASS( monster_scientist_dead, CDeadScientist ); + +// +// ********** DeadScientist SPAWN ********** +// +void CDeadScientist :: Spawn( ) +{ + PRECACHE_MODEL("models/scientist.mdl"); + SET_MODEL(ENT(pev), "models/scientist.mdl"); + + pev->effects = 0; + pev->sequence = 0; + // Corpses have less health + pev->health = 8;//gSkillData.scientistHealth; + + m_bloodColor = BLOOD_COLOR_RED; + + if ( pev->body == -1 ) + {// -1 chooses a random head + pev->body = RANDOM_LONG(0, NUM_SCIENTIST_HEADS-1);// pick a head, any head + } + // Luther is black, make his hands black + if ( pev->body == HEAD_LUTHER ) + pev->skin = 1; + else + pev->skin = 0; + + pev->sequence = LookupSequence( m_szPoses[m_iPose] ); + if (pev->sequence == -1) + { + ALERT ( at_debug, "Dead scientist with bad pose\n" ); + } + + // pev->skin += 2; // use bloody skin -- UNDONE: Turn this back on when we have a bloody skin again! + MonsterInitDead(); +} + + +//========================================================= +// Sitting Scientist PROP +//========================================================= + +class CSittingScientist : public CScientist // kdb: changed from public CBaseMonster so he can speak +{ +public: + void Spawn( void ); + void Precache( void ); + + void EXPORT SittingThink( void ); + int Classify ( void ); + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + virtual void SetAnswerQuestion( CTalkMonster *pSpeaker ); + int FriendNumber( int arrayNumber ); + + int FIdleSpeak ( void ); + int m_baseSequence; + int m_headTurn; + float m_flResponseDelay; +}; + +LINK_ENTITY_TO_CLASS( monster_sitting_scientist, CSittingScientist ); +TYPEDESCRIPTION CSittingScientist::m_SaveData[] = +{ + // Don't need to save/restore m_baseSequence (recalced) + DEFINE_FIELD( CSittingScientist, m_headTurn, FIELD_INTEGER ), + DEFINE_FIELD( CSittingScientist, m_flResponseDelay, FIELD_FLOAT ), +}; + +IMPLEMENT_SAVERESTORE( CSittingScientist, CScientist ); + +// animation sequence aliases +typedef enum +{ +SITTING_ANIM_sitlookleft, +SITTING_ANIM_sitlookright, +SITTING_ANIM_sitscared, +SITTING_ANIM_sitting2, +SITTING_ANIM_sitting3 +} SITTING_ANIM; + + +#define SF_SITTINGSCI_POSTDISASTER 1024 + +// +// ********** Scientist SPAWN ********** +// +void CSittingScientist :: Spawn( ) +{ + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL("models/scientist.mdl"); + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/scientist.mdl"); + Precache(); + InitBoneControllers(); + + UTIL_SetSize(pev, Vector(-14, -14, 0), Vector(14, 14, 36)); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + pev->effects = 0; + pev->health = 50; + + m_bloodColor = BLOOD_COLOR_RED; + m_flFieldOfView = VIEW_FIELD_WIDE; // indicates the width of this monster's forward view cone ( as a dotproduct result ) + + m_afCapability = bits_CAP_HEAR | bits_CAP_TURN_HEAD; + + if (!FBitSet(pev->spawnflags, SF_SITTINGSCI_POSTDISASTER)) //LRC- allow a sitter to be postdisaster. + SetBits(pev->spawnflags, SF_MONSTER_PREDISASTER); // predisaster only! + + if ( pev->body == -1 ) + {// -1 chooses a random head + pev->body = RANDOM_LONG(0, NUM_SCIENTIST_HEADS-1);// pick a head, any head + } + // Luther is black, make his hands black + if ( pev->body == HEAD_LUTHER ) + pev->skin = 1; + + m_baseSequence = LookupSequence( "sitlookleft" ); + pev->sequence = m_baseSequence + RANDOM_LONG(0,4); + ResetSequenceInfo( ); + + SetThink(&CSittingScientist ::SittingThink); + SetNextThink( 0.1 ); + + DROP_TO_FLOOR ( ENT(pev) ); +} + +void CSittingScientist :: Precache( void ) +{ + m_baseSequence = LookupSequence( "sitlookleft" ); + TalkInit(); +} + +//========================================================= +// ID as a passive human +//========================================================= +int CSittingScientist :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_HUMAN_PASSIVE; +} + + +int CSittingScientist::FriendNumber( int arrayNumber ) +{ + static int array[3] = { 2, 1, 0 }; + if ( arrayNumber < 3 ) + return array[ arrayNumber ]; + return arrayNumber; +} + + + +//========================================================= +// sit, do stuff +//========================================================= +void CSittingScientist :: SittingThink( void ) +{ + CBaseEntity *pent; + + StudioFrameAdvance( ); + + // try to greet player + if (FIdleHello()) + { + pent = FindNearestFriend(TRUE); + if (pent) + { + float yaw = VecToYaw(pent->pev->origin - pev->origin) - pev->angles.y; + + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + + if (yaw > 0) + pev->sequence = m_baseSequence + SITTING_ANIM_sitlookleft; + else + pev->sequence = m_baseSequence + SITTING_ANIM_sitlookright; + + ResetSequenceInfo( ); + pev->frame = 0; + SetBoneController( 0, 0 ); + } + } + else if (m_fSequenceFinished) + { + int i = RANDOM_LONG(0,99); + m_headTurn = 0; + + if (m_flResponseDelay && gpGlobals->time > m_flResponseDelay) + { + // respond to question + IdleRespond(); + pev->sequence = m_baseSequence + SITTING_ANIM_sitscared; + m_flResponseDelay = 0; + } + else if (i < 30) + { + pev->sequence = m_baseSequence + SITTING_ANIM_sitting3; + + // turn towards player or nearest friend and speak + + if (!FBitSet(m_bitsSaid, bit_saidHelloPlayer)) + pent = FindNearestFriend(TRUE); + else + pent = FindNearestFriend(FALSE); + + if (!FIdleSpeak() || !pent) + { + m_headTurn = RANDOM_LONG(0,8) * 10 - 40; + pev->sequence = m_baseSequence + SITTING_ANIM_sitting3; + } + else + { + // only turn head if we spoke + float yaw = VecToYaw(pent->pev->origin - pev->origin) - pev->angles.y; + + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + + if (yaw > 0) + pev->sequence = m_baseSequence + SITTING_ANIM_sitlookleft; + else + pev->sequence = m_baseSequence + SITTING_ANIM_sitlookright; + + //ALERT(at_console, "sitting speak\n"); + } + } + else if (i < 60) + { + pev->sequence = m_baseSequence + SITTING_ANIM_sitting3; + m_headTurn = RANDOM_LONG(0,8) * 10 - 40; + if (RANDOM_LONG(0,99) < 5) + { + //ALERT(at_console, "sitting speak2\n"); + FIdleSpeak(); + } + } + else if (i < 80) + { + pev->sequence = m_baseSequence + SITTING_ANIM_sitting2; + } + else if (i < 100) + { + pev->sequence = m_baseSequence + SITTING_ANIM_sitscared; + } + + ResetSequenceInfo( ); + pev->frame = 0; + SetBoneController( 0, m_headTurn ); + } + SetNextThink( 0.1 ); +} + +// prepare sitting scientist to answer a question +void CSittingScientist :: SetAnswerQuestion( CTalkMonster *pSpeaker ) +{ + m_flResponseDelay = gpGlobals->time + RANDOM_FLOAT(3, 4); + m_hTalkTarget = (CBaseMonster *)pSpeaker; +} + + +//========================================================= +// FIdleSpeak +// ask question of nearby friend, or make statement +//========================================================= +int CSittingScientist :: FIdleSpeak ( void ) +{ + // try to start a conversation, or make statement + int pitch; + + if (!FOkToSpeak()) + return FALSE; + + // set global min delay for next conversation + CTalkMonster::g_talkWaitTime = gpGlobals->time + RANDOM_FLOAT(4.8, 5.2); + + pitch = GetVoicePitch(); + + // if there is a friend nearby to speak to, play sentence, set friend's response time, return + + // try to talk to any standing or sitting scientists nearby + CBaseEntity *pentFriend = FindNearestFriend(FALSE); + + if (pentFriend && RANDOM_LONG(0,1)) + { + CTalkMonster *pTalkMonster = GetClassPtr((CTalkMonster *)pentFriend->pev); + pTalkMonster->SetAnswerQuestion( this ); + + IdleHeadTurn(pentFriend->pev->origin); + SENTENCEG_PlayRndSz( ENT(pev), m_szGrp[TLK_PQUESTION], 1.0, ATTN_IDLE, 0, pitch ); + // set global min delay for next conversation + CTalkMonster::g_talkWaitTime = gpGlobals->time + RANDOM_FLOAT(4.8, 5.2); + return TRUE; + } + + // otherwise, play an idle statement + if (RANDOM_LONG(0,1)) + { + SENTENCEG_PlayRndSz( ENT(pev), m_szGrp[TLK_PIDLE], 1.0, ATTN_IDLE, 0, pitch ); + // set global min delay for next conversation + CTalkMonster::g_talkWaitTime = gpGlobals->time + RANDOM_FLOAT(4.8, 5.2); + return TRUE; + } + + // never spoke + CTalkMonster::g_talkWaitTime = 0; + return FALSE; +} diff --git a/server/monsters/scripted.cpp b/server/monsters/scripted.cpp new file mode 100644 index 00000000..4c523466 --- /dev/null +++ b/server/monsters/scripted.cpp @@ -0,0 +1,1243 @@ +/*** +* +* Copyright (c) 1999, 2000 Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +/* + + +===== scripted.cpp ======================================================== + +*/ + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "player.h" + +#ifndef ANIMATION_H +#include "animation.h" +#endif + +#ifndef SAVERESTORE_H +#include "saverestore.h" +#endif + +#include "schedule.h" +#include "scripted.h" +#include "defaultai.h" + + +/* +classname "scripted_sequence" +targetname "me" - there can be more than one with the same name, and they act in concert +target "the_entity_I_want_to_start_playing" or "class entity_classname" will pick the closest inactive scientist +play "name_of_sequence" +idle "name of idle sequence to play before starting" +donetrigger "whatever" - can be any other triggerable entity such as another sequence, train, door, or a special case like "die" or "remove" +moveto - if set the monster first moves to this nodes position +range # - only search this far to find the target +spawnflags - (stop if blocked, stop if player seen) +*/ + + +// +// Cache user-entity-field values until spawn is called. +// + +void CCineMonster :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "m_iszIdle")) + { + m_iszIdle = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszPlay")) + { + m_iszPlay = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszEntity")) + { + m_iszEntity = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszAttack")) + { + m_iszAttack = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszMoveTarget")) + { + m_iszMoveTarget = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iszFireOnBegin")) + { + m_iszFireOnBegin = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_fMoveTo")) + { + m_fMoveTo = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_fTurnType")) + { + m_fTurnType = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_fAction")) + { + m_fAction = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } +// LRC else if (FStrEq(pkvd->szKeyName, "m_flRepeat")) +// { +// m_flRepeat = atof( pkvd->szValue ); +// pkvd->fHandled = TRUE; +// } + else if (FStrEq(pkvd->szKeyName, "m_flRadius")) + { + m_flRadius = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iRepeats")) + { + m_iRepeats = atoi( pkvd->szValue ); + m_iRepeatsLeft = m_iRepeats; + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_fRepeatFrame")) + { + m_fRepeatFrame = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iFinishSchedule")) + { + m_iFinishSchedule = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "m_iPriority")) + { + m_iPriority = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else + { + CBaseMonster::KeyValue( pkvd ); + } +} + +TYPEDESCRIPTION CCineMonster::m_SaveData[] = +{ + DEFINE_FIELD( CCineMonster, m_iState, FIELD_INTEGER ), //LRC + DEFINE_FIELD( CCineMonster, m_iszIdle, FIELD_STRING ), + DEFINE_FIELD( CCineMonster, m_iszPlay, FIELD_STRING ), + DEFINE_FIELD( CCineMonster, m_iszEntity, FIELD_STRING ), + DEFINE_FIELD( CCineMonster, m_iszAttack, FIELD_STRING ), //LRC + DEFINE_FIELD( CCineMonster, m_iszMoveTarget, FIELD_STRING ), //LRC + DEFINE_FIELD( CCineMonster, m_iszFireOnBegin, FIELD_STRING ), + DEFINE_FIELD( CCineMonster, m_fMoveTo, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_fTurnType, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_fAction, FIELD_INTEGER ), +//LRC- this is unused DEFINE_FIELD( CCineMonster, m_flRepeat, FIELD_FLOAT ), + DEFINE_FIELD( CCineMonster, m_flRadius, FIELD_FLOAT ), + + DEFINE_FIELD( CCineMonster, m_iDelay, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_startTime, FIELD_TIME ), + + DEFINE_FIELD( CCineMonster, m_saved_movetype, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_saved_solid, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_saved_effects, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_iFinishSchedule, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_interruptable, FIELD_BOOLEAN ), + + //LRC + DEFINE_FIELD( CCineMonster, m_iRepeats, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_iRepeatsLeft, FIELD_INTEGER ), + DEFINE_FIELD( CCineMonster, m_fRepeatFrame, FIELD_FLOAT ), + DEFINE_FIELD( CCineMonster, m_iPriority, FIELD_INTEGER ), +}; + + +IMPLEMENT_SAVERESTORE( CCineMonster, CBaseMonster ); + +LINK_ENTITY_TO_CLASS( scripted_sequence, CCineMonster ); +LINK_ENTITY_TO_CLASS( scripted_action, CCineMonster ); //LRC + +void CCineMonster :: Spawn( void ) +{ + // pev->solid = SOLID_TRIGGER; + // UTIL_SetSize(pev, Vector(-8, -8, -8), Vector(8, 8, 8)); + pev->solid = SOLID_NOT; + + m_iState = STATE_OFF; //LRC + + if ( FStringNull(m_iszIdle) && FStringNull(pev->targetname) ) // if no targetname, start now + { + SetThink(&CCineMonster :: CineThink ); + SetNextThink( 1.0 ); + } + else if ( m_iszIdle ) + { + SetThink(&CCineMonster :: InitIdleThink ); + SetNextThink( 1.0 ); + } + if ( pev->spawnflags & SF_SCRIPT_NOINTERRUPT ) + m_interruptable = FALSE; + else + m_interruptable = TRUE; + + //LRC - the only difference between AI and normal sequences + if ( pev->spawnflags & SF_SCRIPT_OVERRIDESTATE ) + { + m_iPriority |= SS_INTERRUPT_ANYSTATE; + } +} + +// +// CineStart +// +void CCineMonster :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + // do I already know who I should use + CBaseEntity *pEntity = m_hTargetEnt; + CBaseMonster *pTarget = NULL; + + if ( pEntity ) + pTarget = pEntity->MyMonsterPointer(); + + if ( pTarget ) + { +// ALERT(at_console, "Sequence \"%s\" triggered, already has a target.\n", STRING(pev->targetname)); + // am I already playing the script? + if ( pTarget->m_scriptState == SCRIPT_PLAYING ) + return; + + m_startTime = gpGlobals->time + 0.05; //why the delay? -- LRC + } + else + { +// ALERT(at_console, "Sequence \"%s\" triggered, can't find target; searching\n", STRING(pev->targetname)); + m_hActivator = pActivator; + // if not, try finding them + SetThink(&CCineMonster :: CineThink ); +// SetNextThink( 0 ); + CineThink(); //LRC + } +} + + +// This doesn't really make sense since only MOVETYPE_PUSH get 'Blocked' events +void CCineMonster :: Blocked( CBaseEntity *pOther ) +{ + +} + +void CCineMonster :: Touch( CBaseEntity *pOther ) +{ +} + +// +// ********** Cinematic DIE ********** +// +void CCineMonster :: Die( void ) +{ + SetThink( Remove ); +} + +// +// ********** Cinematic PAIN ********** +// +void CCineMonster :: Pain( void ) +{ + +} + +// +// ********** Cinematic Think ********** +// + +//LRC: now redefined... find a viable entity with the given name, and return it (or NULL if not found). +CBaseMonster* CCineMonster :: FindEntity( const char* sName, CBaseEntity *pActivator ) +{ + CBaseEntity *pEntity; + + pEntity = UTIL_FindEntityByTargetname(NULL, sName, pActivator); + //m_hTargetEnt = NULL; + CBaseMonster *pMonster = NULL; + + while (pEntity) + { + if ( FBitSet( pEntity->pev->flags, FL_MONSTER )) + { + pMonster = pEntity->MyMonsterPointer( ); + if ( pMonster && pMonster->CanPlaySequence( m_iPriority | SS_INTERRUPT_ALERT ) ) + { + return pMonster; + } + ALERT( at_debug, "Found %s, but can't play!\n", sName ); + } + pEntity = UTIL_FindEntityByTargetname(pEntity, sName, pActivator); + pMonster = NULL; + } + + // couldn't find something with the given targetname; assume it's a classname instead. + if ( !pMonster ) + { + pEntity = NULL; + while ((pEntity = UTIL_FindEntityInSphere( pEntity, pev->origin, m_flRadius )) != NULL) + { + if (FClassnameIs( pEntity->pev, sName)) + { + if ( FBitSet( pEntity->pev->flags, FL_MONSTER )) + { + pMonster = pEntity->MyMonsterPointer( ); + if ( pMonster && pMonster->CanPlaySequence( m_iPriority ) ) + { + return pMonster; + } + } + } + } + } + return NULL; +} + +// make the entity enter a scripted sequence +void CCineMonster :: PossessEntity( void ) +{ + CBaseEntity *pEntity = m_hTargetEnt; + CBaseMonster *pTarget = NULL; + if ( pEntity ) + pTarget = pEntity->MyMonsterPointer(); + +// ALERT( at_console, "Possess: pEntity %s, pTarget %s\n", STRING(pEntity->pev->targetname), STRING(pTarget->pev->targetname)); + + if ( pTarget ) + { + if (pTarget->m_pCine) + { + pTarget->m_pCine->CancelScript(); + } + + pTarget->m_pCine = this; + if (m_iszAttack) + { + // anything with that name? + pTarget->m_hTargetEnt = UTIL_FindEntityByTargetname(NULL, STRING(m_iszAttack), m_hActivator); + if ( pTarget->m_hTargetEnt == NULL ) + { // nothing. Anything with that classname? + while ((pTarget->m_hTargetEnt = UTIL_FindEntityInSphere( pTarget->m_hTargetEnt, pev->origin, m_flRadius )) != NULL) + { + if (FClassnameIs( pTarget->m_hTargetEnt->pev, STRING(m_iszAttack))) break; + } + } + if (pTarget->m_hTargetEnt == NULL) + { // nothing. Oh well. + ALERT(at_debug,"%s %s has a missing \"turn target\": %s\n",STRING(pev->classname),STRING(pev->targetname),STRING(m_iszAttack)); + pTarget->m_hTargetEnt = this; + } + } + else + { + pTarget->m_hTargetEnt = this; + } + + if (m_iszMoveTarget) + { + // anything with that name? + pTarget->m_pGoalEnt = UTIL_FindEntityByTargetname(NULL, STRING(m_iszMoveTarget), m_hActivator); + if (pTarget->m_pGoalEnt == NULL) + { // nothing. Oh well. + ALERT(at_debug,"%s %s has a missing \"move target\": %s\n",STRING(pev->classname),STRING(pev->targetname),STRING(m_iszMoveTarget)); + pTarget->m_pGoalEnt = this; + } + } + else + { + pTarget->m_pGoalEnt = this; + } +// if (IsAction()) +// pTarget->PushEnemy(this,pev->origin); + + m_saved_movetype = pTarget->pev->movetype; + m_saved_solid = pTarget->pev->solid; + m_saved_effects = pTarget->pev->effects; + pTarget->pev->effects |= pev->effects; + +// ALERT(at_console, "script. IsAction = %d",IsAction()); + + m_iState = STATE_ON; // LRC: assume we'll set it to 'on', unless proven otherwise... + switch (m_fMoveTo) + { + case 1: + case 2: + DelayStart( 1 ); + m_iState = STATE_TURN_ON; + // fall through... + case 0: + case 4: + //G-Cont. this is a not a better way :( + //in my new project this bug will be removed + //in Spirit... as is. Sorry about that. + //If we interesting - decomment UTIL_AssignOrigin + //and run c1a4i with tentacle script - no comments + //UTIL_AssignOrigin( pTarget, pev->origin ); + pTarget->pev->ideal_yaw = pev->angles.y; + pTarget->pev->avelocity = Vector( 0, 0, 0 ); + pTarget->pev->velocity = Vector( 0, 0, 0 ); + pTarget->pev->effects |= EF_NOINTERP; + pTarget->pev->angles.y = pev->angles.y; + pTarget->m_scriptState = SCRIPT_WAIT; + //m_startTime = gpGlobals->time + 1E6; + break; + case 5: + case 6: + pTarget->m_scriptState = SCRIPT_WAIT; + break; + } +// ALERT( at_aiconsole, "\"%s\" found and used (INT: %s)\n", STRING( pTarget->pev->targetname ), FBitSet(pev->spawnflags, SF_SCRIPT_NOINTERRUPT)?"No":"Yes" ); + + pTarget->m_IdealMonsterState = MONSTERSTATE_SCRIPT; +// if (m_iszIdle) +// { +// ALERT(at_console, "Possess: Play idle sequence\n"); +// StartSequence( pTarget, m_iszIdle, FALSE ); +// if (FStrEq( STRING(m_iszIdle), STRING(m_iszPlay))) +// { +// pTarget->pev->framerate = 0; +// } +// } +// ALERT(at_console, "Finished PossessEntity, ms %d, ims %d\n", pTarget->m_MonsterState, pTarget->m_IdealMonsterState); + } + +} + + +// at the beginning of the level, set up the idle animation. --LRC +void CCineMonster :: InitIdleThink( void ) +{ + if ((m_hTargetEnt = FindEntity(STRING(m_iszEntity), NULL)) != NULL) + { + PossessEntity( ); + m_startTime = gpGlobals->time + 1E6; + ALERT( at_aiconsole, "script \"%s\" using monster \"%s\"\n", STRING( pev->targetname ), STRING( m_iszEntity ) ); + } + else + { + CancelScript( ); + ALERT( at_aiconsole, "script \"%s\" can't find monster \"%s\"\n", STRING( pev->targetname ), STRING( m_iszEntity ) ); + SetNextThink( 1.0 ); + } +} + +void CCineMonster :: CineThink( void ) +{ +// ALERT(at_console, "Sequence think, activator %s\n", STRING(m_hActivator->pev->targetname)); + if ((m_hTargetEnt = FindEntity(STRING(m_iszEntity),m_hActivator)) != NULL) + { +// ALERT(at_console, "Sequence found %s \"%s\"\n", STRING(m_hTargetEnt->pev->classname), STRING(m_hTargetEnt->pev->targetname)); + PossessEntity( ); + ALERT( at_aiconsole, "script \"%s\" using monster \"%s\"\n", STRING( pev->targetname ), STRING( m_iszEntity ) ); + } + else + { +// ALERT(at_console, "Sequence found nothing called %s\n", STRING(m_iszEntity)); + CancelScript( ); + ALERT( at_aiconsole, "script \"%s\" can't find monster \"%s\"\n", STRING( pev->targetname ), STRING( m_iszEntity ) ); + SetNextThink( 1.0 ); + } +} + + +// lookup a sequence name and setup the target monster to play it +BOOL CCineMonster :: StartSequence( CBaseMonster *pTarget, int iszSeq, BOOL completeOnEmpty ) +{ +// ALERT( at_console, "StartSequence %s \"%s\"\n", STRING(pev->classname), STRING(pev->targetname)); + + if ( !iszSeq && completeOnEmpty ) + { + SequenceDone( pTarget ); + return FALSE; + } + + pTarget->pev->sequence = pTarget->LookupSequence( STRING( iszSeq ) ); + if (pTarget->pev->sequence == -1) + { + ALERT( at_error, "%s: unknown scripted sequence \"%s\"\n", STRING( pTarget->pev->targetname ), STRING( iszSeq) ); + pTarget->pev->sequence = 0; + // return FALSE; + } + +#if 0 + char *s; + if ( pev->spawnflags & SF_SCRIPT_NOINTERRUPT ) + s = "No"; + else + s = "Yes"; + + ALERT( at_debug, "%s (%s): started \"%s\":INT:%s\n", STRING( pTarget->pev->targetname ), STRING( pTarget->pev->classname ), STRING( iszSeq), s ); +#endif + + pTarget->pev->frame = 0; + pTarget->ResetSequenceInfo( ); + return TRUE; +} + +//========================================================= +// SequenceDone - called when a scripted sequence animation +// sequence is done playing ( or when an AI Scripted Sequence +// doesn't supply an animation sequence to play ). Expects +// the CBaseMonster pointer to the monster that the sequence +// possesses. +//========================================================= +void CCineMonster :: SequenceDone ( CBaseMonster *pMonster ) +{ + m_iRepeatsLeft = m_iRepeats; //LRC - reset the repeater count + m_iState = STATE_OFF; // we've finished. +// ALERT( at_console, "Sequence %s finished\n", STRING(pev->targetname));//STRING( m_pCine->m_iszPlay ) ); + + if ( !( pev->spawnflags & SF_SCRIPT_REPEATABLE ) ) + { + SetThink( Remove ); + SetNextThink( 0.1 ); + } + + // This is done so that another sequence can take over the monster when triggered by the first + + pMonster->CineCleanup(); + + FixScriptMonsterSchedule( pMonster ); + + // This may cause a sequence to attempt to grab this guy NOW, so we have to clear him out + // of the existing sequence + UTIL_FireTargets( pev->target, NULL, this, USE_TOGGLE ); +} + +//========================================================= +// When a monster finishes a scripted sequence, we have to +// fix up its state and schedule for it to return to a +// normal AI monster. +// +// Scripted sequences just dirty the Schedule and drop the +// monster in Idle State. +// +// or select a specific AMBUSH schedule, regardless of state. //LRC +//========================================================= +void CCineMonster :: FixScriptMonsterSchedule( CBaseMonster *pMonster ) +{ + if ( pMonster->m_IdealMonsterState != MONSTERSTATE_DEAD ) pMonster->m_IdealMonsterState = MONSTERSTATE_IDLE; +// pMonster->ClearSchedule(); + + switch ( m_iFinishSchedule ) + { + case SCRIPT_FINISHSCHED_DEFAULT: + pMonster->ClearSchedule(); + break; + case SCRIPT_FINISHSCHED_AMBUSH: + pMonster->ChangeSchedule( pMonster->GetScheduleOfType( SCHED_AMBUSH ) ); + break; + default: + ALERT ( at_aiconsole, "FixScriptMonsterSchedule - no case!\n" ); + pMonster->ClearSchedule(); + break; + } +} + +BOOL CBaseMonster :: ExitScriptedSequence( ) +{ + if ( pev->deadflag == DEAD_DYING ) + { + // is this legal? + // BUGBUG -- This doesn't call Killed() + m_IdealMonsterState = MONSTERSTATE_DEAD; + return FALSE; + } + + if (m_pCine) + { + m_pCine->CancelScript( ); + } + + return TRUE; +} + + +void CCineMonster::AllowInterrupt( BOOL fAllow ) +{ + if ( pev->spawnflags & SF_SCRIPT_NOINTERRUPT ) + return; + m_interruptable = fAllow; +} + + +BOOL CCineMonster::CanInterrupt( void ) +{ + if ( !m_interruptable ) + return FALSE; + + CBaseEntity *pTarget = m_hTargetEnt; + + if ( pTarget != NULL && pTarget->pev->deadflag == DEAD_NO ) + return TRUE; + + return FALSE; +} + + +int CCineMonster::IgnoreConditions( void ) +{ + if ( CanInterrupt() ) + return 0; + + // Big fat BUG: This is an IgnoreConditions function - we need to return the conditions + // that _shouldn't_ be able to break the script, instead of the conditions that _should_!! + return SCRIPT_BREAK_CONDITIONS; +} + + +void ScriptEntityCancel( edict_t *pentCine ) +{ + // make sure they are a scripted_sequence + if (FClassnameIs( pentCine, "scripted_sequence" ) || FClassnameIs( pentCine, "scripted_action" )) + { + GetClassPtr((CCineMonster *)VARS(pentCine))->m_iState = STATE_OFF; + CCineMonster *pCineTarget = GetClassPtr((CCineMonster *)VARS(pentCine)); + // make sure they have a monster in mind for the script + CBaseEntity *pEntity = pCineTarget->m_hTargetEnt; + CBaseMonster *pTarget = NULL; + if ( pEntity ) + pTarget = pEntity->MyMonsterPointer(); + + if (pTarget) + { + // make sure their monster is actually playing a script + if ( pTarget->m_MonsterState == MONSTERSTATE_SCRIPT ) + { + // tell them do die + pTarget->m_scriptState = CCineMonster::SCRIPT_CLEANUP; + // do it now + pTarget->CineCleanup( ); + //LRC - clean up so that if another script is starting immediately, the monster will notice it. + pTarget->ClearSchedule( ); + } + } + } +} + + +// find all the cinematic entities with my targetname and stop them from playing +void CCineMonster :: CancelScript( void ) +{ + ALERT( at_aiconsole, "Cancelling script: %s\n", STRING(m_iszPlay) ); + + if ( !pev->targetname ) + { + ScriptEntityCancel( edict() ); + return; + } + + CBaseEntity *pCineTarget = UTIL_FindEntityByTargetname(NULL, STRING(pev->targetname)); + + while (pCineTarget) + { + ScriptEntityCancel( ENT(pCineTarget->pev) ); + pCineTarget = UTIL_FindEntityByTargetname(pCineTarget, STRING(pev->targetname)); + } +} + + +// find all the cinematic entities with my targetname and tell them whether to wait before starting +void CCineMonster :: DelayStart( int state ) +{ + CBaseEntity *pCine = UTIL_FindEntityByTargetname(NULL, STRING(pev->targetname)); + + while ( pCine ) + { + if (FClassnameIs( pCine->pev, "scripted_sequence" ) || FClassnameIs( pCine->pev, "scripted_action" )) + { + CCineMonster *pTarget = GetClassPtr((CCineMonster *)(pCine->pev)); + if (state) + { +// ALERT(at_console, "Delaying start\n"); + pTarget->m_iDelay++; + } + else + { +// ALERT(at_console, "Undelaying start\n"); + pTarget->m_iDelay--; + if (pTarget->m_iDelay <= 0) + { + pTarget->m_iState = STATE_ON; //LRC + UTIL_FireTargets( m_iszFireOnBegin, this, this, USE_TOGGLE ); //LRC + pTarget->m_startTime = gpGlobals->time + 0.05; // why the delay? -- LRC + } + } + } + pCine = UTIL_FindEntityByTargetname(pCine, STRING(pev->targetname)); + } +} + + + +// Find an entity that I'm interested in and precache the sounds he'll need in the sequence. +void CCineMonster :: Activate( void ) +{ + CBaseEntity *pEntity; + CBaseMonster *pTarget; + + // The entity name could be a target name or a classname + // Check the targetname + pEntity = UTIL_FindEntityByTargetname(NULL, STRING(m_iszEntity)); + pTarget = NULL; + + while (!pTarget && pEntity) + { + if ( FBitSet( pEntity->pev->flags, FL_MONSTER )) + { + pTarget = pEntity->MyMonsterPointer( ); + } + pEntity = UTIL_FindEntityByTargetname(pEntity, STRING(m_iszEntity)); + } + + // If no entity with that targetname, check the classname + if ( !pTarget ) + { + pEntity = UTIL_FindEntityByClassname(NULL, STRING(m_iszEntity)); + while (!pTarget && pEntity) + { + pTarget = pEntity->MyMonsterPointer( ); + pEntity = UTIL_FindEntityByClassname(pEntity, STRING(m_iszEntity)); + } + } + // Found a compatible entity + if ( pTarget ) + { + void *pmodel; + pmodel = GET_MODEL_PTR( pTarget->edict() ); + if ( pmodel ) + { + // Look through the event list for stuff to precache + SequencePrecache( pmodel, STRING( m_iszIdle ) ); + SequencePrecache( pmodel, STRING( m_iszPlay ) ); + } + } + + CBaseMonster::Activate(); +} + + +BOOL CBaseMonster :: CineCleanup( ) +{ + CCineMonster *pOldCine = m_pCine; + + // am I linked to a cinematic? + if (m_pCine) + { + // okay, reset me to what it thought I was before + m_pCine->m_hTargetEnt = NULL; + pev->movetype = m_pCine->m_saved_movetype; + pev->solid = m_pCine->m_saved_solid; + pev->effects = m_pCine->m_saved_effects; + + if (m_pCine->pev->spawnflags & SF_SCRIPT_STAYDEAD) + pev->deadflag = DEAD_DYING; + } + else + { + // arg, punt + pev->movetype = MOVETYPE_STEP;// this is evil + pev->solid = SOLID_SLIDEBOX; + } + m_pCine = NULL; + m_hTargetEnt = NULL; + m_pGoalEnt = NULL; + if (pev->deadflag == DEAD_DYING) + { + // last frame of death animation? + pev->health = 0; + pev->framerate = 0.0; + pev->solid = SOLID_NOT; + SetState( MONSTERSTATE_DEAD ); + pev->deadflag = DEAD_DEAD; + UTIL_SetSize( pev, pev->mins, Vector(pev->maxs.x, pev->maxs.y, pev->mins.z + 2) ); + + if ( pOldCine ) + { + SetUse( NULL ); // BUGBUG -- This doesn't call Killed() + SetThink( NULL ); // This will probably break some stuff + SetTouch( NULL ); + } + // This turns off animation & physics in case their origin ends up stuck in the world or something + StopAnimation(); + pev->movetype = MOVETYPE_NONE; + pev->effects |= EF_NOINTERP; // Don't interpolate either, assume the corpse is positioned in its final resting place + return FALSE; + } + + // If we actually played a sequence + if ( pOldCine && pOldCine->m_iszPlay ) + { + if ( !(pOldCine->pev->spawnflags & SF_SCRIPT_NOSCRIPTMOVEMENT) ) + { + // reset position + Vector new_origin, new_angle; + GetBonePosition( 0, new_origin, new_angle ); + + // Figure out how far they have moved + // We can't really solve this problem because we can't query the movement of the origin relative + // to the sequence. We can get the root bone's position as we do here, but there are + // cases where the root bone is in a different relative position to the entity's origin + // before/after the sequence plays. So we are stuck doing this: + + // !!!HACKHACK: Float the origin up and drop to floor because some sequences have + // irregular motion that can't be properly accounted for. + + // UNDONE: THIS SHOULD ONLY HAPPEN IF WE ACTUALLY PLAYED THE SEQUENCE. + Vector oldOrigin = pev->origin; + + // UNDONE: ugly hack. Don't move monster if they don't "seem" to move + // this really needs to be done with the AX,AY,etc. flags, but that aren't consistantly + // being set, so animations that really do move won't be caught. + if ((oldOrigin - new_origin).Length2D() < 8.0) + new_origin = oldOrigin; + + pev->origin.x = new_origin.x; + pev->origin.y = new_origin.y; + pev->origin.z += 1; + + pev->flags |= FL_ONGROUND; + int drop = DROP_TO_FLOOR( ENT(pev) ); + + // Origin in solid? Set to org at the end of the sequence + if ( drop < 0 ) + pev->origin = oldOrigin; + else if ( drop == 0 ) // Hanging in air? + { + pev->origin.z = new_origin.z; + pev->flags &= ~FL_ONGROUND; + } + // else entity hit floor, leave there + + // pEntity->pev->origin.z = new_origin.z + 5.0; // damn, got to fix this + + UTIL_SetOrigin( this, pev->origin ); + pev->effects |= EF_NOINTERP; + } + + // We should have some animation to put these guys in, but for now it's idle. + // Due to NOINTERP above, there won't be any blending between this anim & the sequence + m_Activity = ACT_RESET; + } + // set them back into a normal state + pev->enemy = NULL; + if ( pev->health > 0 ) + m_IdealMonsterState = MONSTERSTATE_IDLE; // m_previousState; + else + { + // Dropping out because he got killed + // Can't call killed() no attacker and weirdness (late gibbing) may result + m_IdealMonsterState = MONSTERSTATE_DEAD; + SetConditions( bits_COND_LIGHT_DAMAGE ); + pev->deadflag = DEAD_DYING; + FCheckAITrigger(); + pev->deadflag = DEAD_NO; + } + + + // SetAnimation( m_MonsterState ); + //LRC- removed, was never implemented. ClearBits(pev->spawnflags, SF_MONSTER_WAIT_FOR_SCRIPT ); + + return TRUE; +} + + + + +class CScriptedSentence : public CBaseAnimating +{ +public: + void Spawn( void ); + void KeyValue( KeyValueData *pkvd ); + void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + void EXPORT FindThink( void ); + void EXPORT DelayThink( void ); + void EXPORT DurationThink( void ); + int ObjectCaps( void ) { return (CBaseAnimating :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); } + + STATE GetState() { return m_playing?STATE_ON:STATE_OFF; } + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static TYPEDESCRIPTION m_SaveData[]; + + CBaseMonster *FindEntity( CBaseEntity *pActivator ); + BOOL AcceptableSpeaker( CBaseMonster *pMonster ); + BOOL StartSentence( CBaseMonster *pTarget ); + + +private: + int m_iszSentence; // string index for idle animation + int m_iszEntity; // entity that is wanted for this sentence + float m_flRadius; // range to search + float m_flDuration; // How long the sentence lasts + float m_flRepeat; // maximum repeat rate + float m_flAttenuation; + float m_flVolume; + BOOL m_active; // is the sentence enabled? (for m_flRepeat) + BOOL m_playing; //LRC- is the sentence playing? (for GetState) + int m_iszListener; // name of entity to look at while talking +}; + +#define SF_SENTENCE_ONCE 0x0001 +#define SF_SENTENCE_FOLLOWERS 0x0002 // only say if following player +#define SF_SENTENCE_INTERRUPT 0x0004 // force talking except when dead +#define SF_SENTENCE_CONCURRENT 0x0008 // allow other people to keep talking + +TYPEDESCRIPTION CScriptedSentence::m_SaveData[] = +{ + DEFINE_FIELD( CScriptedSentence, m_iszSentence, FIELD_STRING ), + DEFINE_FIELD( CScriptedSentence, m_iszEntity, FIELD_STRING ), + DEFINE_FIELD( CScriptedSentence, m_flRadius, FIELD_FLOAT ), + DEFINE_FIELD( CScriptedSentence, m_flDuration, FIELD_FLOAT ), + DEFINE_FIELD( CScriptedSentence, m_flRepeat, FIELD_FLOAT ), + DEFINE_FIELD( CScriptedSentence, m_flAttenuation, FIELD_FLOAT ), + DEFINE_FIELD( CScriptedSentence, m_flVolume, FIELD_FLOAT ), + DEFINE_FIELD( CScriptedSentence, m_active, FIELD_BOOLEAN ), + DEFINE_FIELD( CScriptedSentence, m_playing, FIELD_BOOLEAN ), + DEFINE_FIELD( CScriptedSentence, m_iszListener, FIELD_STRING ), +}; + + +IMPLEMENT_SAVERESTORE( CScriptedSentence, CBaseAnimating ); + +LINK_ENTITY_TO_CLASS( scripted_sentence, CScriptedSentence ); + +void CScriptedSentence :: KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "sentence")) + { + m_iszSentence = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "entity")) + { + m_iszEntity = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "duration")) + { + m_flDuration = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "radius")) + { + m_flRadius = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "refire")) + { + m_flRepeat = atof( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if(FStrEq(pkvd->szKeyName, "attenuation")) + { + pev->impulse = atoi( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else if(FStrEq(pkvd->szKeyName, "volume")) + { + m_flVolume = atof( pkvd->szValue ) * 0.1; + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "listener")) + { + m_iszListener = ALLOC_STRING( pkvd->szValue ); + pkvd->fHandled = TRUE; + } + else + CBaseAnimating::KeyValue( pkvd ); +} + + +void CScriptedSentence :: Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if ( !m_active ) + return; +// ALERT( at_console, "Firing sentence: %s\n", STRING(m_iszSentence) ); + m_hActivator = pActivator; + SetThink(&CScriptedSentence :: FindThink ); + SetNextThink( 0 ); +} + + +void CScriptedSentence :: Spawn( void ) +{ + pev->solid = SOLID_NOT; + + m_active = TRUE; + m_playing = FALSE; //LRC + // if no targetname, start now + if ( !pev->targetname ) + { + SetThink(&CScriptedSentence :: FindThink ); + SetNextThink( 1.0 ); + } + + switch( pev->impulse ) + { + case 1: // Medium radius + m_flAttenuation = ATTN_STATIC; + break; + + case 2: // Large radius + m_flAttenuation = ATTN_NORM; + break; + + case 3: //EVERYWHERE + m_flAttenuation = ATTN_NONE; + break; + + default: + case 0: // Small radius + m_flAttenuation = ATTN_IDLE; + break; + } + pev->impulse = 0; + + // No volume, use normal + if ( m_flVolume <= 0 ) + m_flVolume = 1.0; +} + + +void CScriptedSentence :: FindThink( void ) +{ + if (!m_iszEntity) //LRC- no target monster given: speak through HEV + { + CBasePlayer* pPlayer = (CBasePlayer*)UTIL_FindEntityByClassname( NULL, "player" ); + if (pPlayer) + { + m_playing = TRUE; + if ((STRING(m_iszSentence))[0] == '!') + pPlayer->SetSuitUpdate((char*)STRING(m_iszSentence),FALSE,0); + else + pPlayer->SetSuitUpdate((char*)STRING(m_iszSentence),TRUE,0); + if ( pev->spawnflags & SF_SENTENCE_ONCE ) + UTIL_Remove( this ); + SetThink(&CScriptedSentence :: DurationThink ); + SetNextThink( m_flDuration ); + m_active = FALSE; + } + else + ALERT( at_debug, "ScriptedSentence: can't find \"player\" to play HEV sentence!?\n"); + return; + } + + CBaseMonster *pMonster = FindEntity( m_hActivator ); + if ( pMonster ) + { + m_playing = TRUE; + StartSentence( pMonster ); + if ( pev->spawnflags & SF_SENTENCE_ONCE ) + UTIL_Remove( this ); + SetThink(&CScriptedSentence :: DurationThink ); + SetNextThink( m_flDuration ); + m_active = FALSE; +// ALERT( at_console, "%s: found monster %s\n", STRING(m_iszSentence), STRING(m_iszEntity) ); + } + else + { +// ALERT( at_console, "%s: can't find monster %s\n", STRING(m_iszSentence), STRING(m_iszEntity) ); + SetNextThink( m_flRepeat + 0.5 ); + } +} + +//LRC +void CScriptedSentence :: DurationThink( void ) +{ + m_playing = FALSE; + SetNextThink( m_flRepeat ); + SetThink(&CScriptedSentence :: DelayThink ); +} + +void CScriptedSentence :: DelayThink( void ) +{ + m_active = TRUE; + if ( !pev->targetname ) + SetNextThink( 0.1 ); + SetThink(&CScriptedSentence :: FindThink ); +} + + +BOOL CScriptedSentence :: AcceptableSpeaker( CBaseMonster *pMonster ) +{ + if ( pMonster ) + { + if ( pev->spawnflags & SF_SENTENCE_FOLLOWERS ) + { + if ( pMonster->m_hTargetEnt == NULL || !FClassnameIs(pMonster->m_hTargetEnt->pev, "player") ) + return FALSE; + } + BOOL override; + if ( pev->spawnflags & SF_SENTENCE_INTERRUPT ) + override = TRUE; + else + override = FALSE; + if ( pMonster->CanPlaySentence( override ) ) + return TRUE; + } + return FALSE; +} + + +CBaseMonster *CScriptedSentence :: FindEntity( CBaseEntity *pActivator ) +{ + CBaseEntity *pTarget; + CBaseMonster *pMonster; + + pTarget = UTIL_FindEntityByTargetname(NULL, STRING(m_iszEntity), pActivator); + pMonster = NULL; + + while ( pTarget ) + { + pMonster = pTarget->MyMonsterPointer( ); + if ( pMonster != NULL ) + { + if ( AcceptableSpeaker( pMonster ) ) + return pMonster; +// ALERT( at_console, "%s (%s), not acceptable\n", STRING(pMonster->pev->classname), STRING(pMonster->pev->targetname) ); + } + pTarget = UTIL_FindEntityByTargetname(pTarget, STRING(m_iszEntity), pActivator); + } + + pTarget = NULL; + while ((pTarget = UTIL_FindEntityInSphere( pTarget, pev->origin, m_flRadius )) != NULL) + { + if (FClassnameIs( pTarget->pev, STRING(m_iszEntity))) + { + if ( FBitSet( pTarget->pev->flags, FL_MONSTER )) + { + pMonster = pTarget->MyMonsterPointer( ); + if ( AcceptableSpeaker( pMonster ) ) + return pMonster; + } + } + } + + return NULL; +} + + +BOOL CScriptedSentence :: StartSentence( CBaseMonster *pTarget ) +{ + if ( !pTarget ) + { + ALERT( at_aiconsole, "Not Playing sentence %s\n", STRING(m_iszSentence) ); + return NULL; + } + + BOOL bConcurrent = FALSE; + //LRC: Er... if the "concurrent" flag is NOT set, we make bConcurrent true!? + if ( !(pev->spawnflags & SF_SENTENCE_CONCURRENT) ) + bConcurrent = TRUE; + + CBaseEntity *pListener = NULL; + if (!FStringNull(m_iszListener)) + { + float radius = m_flRadius; + + if ( FStrEq( STRING(m_iszListener ), "player" ) ) + radius = 4096; // Always find the player + + pListener = UTIL_FindEntityGeneric( STRING( m_iszListener ), pTarget->pev->origin, radius ); + } + + pTarget->PlayScriptedSentence( STRING(m_iszSentence), m_flDuration, m_flVolume, m_flAttenuation, bConcurrent, pListener ); + ALERT( at_aiconsole, "Playing sentence %s (%.1f)\n", STRING(m_iszSentence), m_flDuration ); + UTIL_FireTargets( pev->target, NULL, this, USE_TOGGLE ); + return TRUE; +} + + + + + +/* + +*/ + + +//========================================================= +// Furniture - this is the cool comment I cut-and-pasted +//========================================================= +class CFurniture : public CBaseMonster +{ +public: + void Spawn ( void ); + void Die( void ); + int Classify ( void ); + virtual int ObjectCaps( void ) { return (CBaseMonster :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); } +}; + + +LINK_ENTITY_TO_CLASS( monster_furniture, CFurniture ); + + +//========================================================= +// Furniture is killed +//========================================================= +void CFurniture :: Die ( void ) +{ + SetThink( Remove ); + SetNextThink( 0 ); +} + +//========================================================= +// This used to have something to do with bees flying, but +// now it only initializes moving furniture in scripted sequences +//========================================================= +void CFurniture :: Spawn( ) +{ + PRECACHE_MODEL((char *)STRING(pev->model)); + SET_MODEL(ENT(pev), STRING(pev->model)); + + pev->movetype = MOVETYPE_NONE; + pev->solid = SOLID_BBOX; + pev->health = 80000; + pev->takedamage = DAMAGE_AIM; + pev->effects = 0; + pev->yaw_speed = 0; + pev->sequence = 0; + pev->frame = 0; + +// pev->nextthink += 1.0; +// SetThink (WalkMonsterDelay); + + ResetSequenceInfo( ); + pev->frame = 0; + MonsterInit(); +} + +//========================================================= +// ID's Furniture as neutral (noone will attack it) +//========================================================= +int CFurniture::Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_NONE; +} + + diff --git a/server/monsters/scripted.h b/server/monsters/scripted.h new file mode 100644 index 00000000..9f5cb94e --- /dev/null +++ b/server/monsters/scripted.h @@ -0,0 +1,128 @@ +/*** +* +* Copyright (c) 1999, 2000 Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#ifndef SCRIPTED_H +#define SCRIPTED_H + +#ifndef SCRIPTEVENT_H +#include "scriptevent.h" +#endif + +#define SF_SCRIPT_WAITTILLSEEN 1 +#define SF_SCRIPT_EXITAGITATED 2 +#define SF_SCRIPT_REPEATABLE 4 +#define SF_SCRIPT_LEAVECORPSE 8 +//#define SF_SCRIPT_INTERPOLATE 16 // don't use, old bug +#define SF_SCRIPT_NOINTERRUPT 32 +#define SF_SCRIPT_OVERRIDESTATE 64 +#define SF_SCRIPT_NOSCRIPTMOVEMENT 128 +#define SF_SCRIPT_STAYDEAD 256 // LRC- signifies that the animation kills the monster + // (needed because the monster animations don't use AnimEvent 1000 properly) + +#define SCRIPT_BREAK_CONDITIONS (bits_COND_LIGHT_DAMAGE|bits_COND_HEAVY_DAMAGE) + +//LRC - rearranged into flags +#define SS_INTERRUPT_IDLE 0x0 +#define SS_INTERRUPT_ALERT 0x1 +#define SS_INTERRUPT_ANYSTATE 0x2 +#define SS_INTERRUPT_SCRIPTS 0x4 + +// when a monster finishes an AI scripted sequence, we can choose +// a schedule to place them in. These defines are the aliases to +// resolve worldcraft input to real schedules (sjb) +#define SCRIPT_FINISHSCHED_DEFAULT 0 +#define SCRIPT_FINISHSCHED_AMBUSH 1 + +class CCineMonster : public CBaseMonster +{ +public: + void Spawn( void ); + virtual void KeyValue( KeyValueData *pkvd ); + virtual void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + virtual void Blocked( CBaseEntity *pOther ); + virtual void Touch( CBaseEntity *pOther ); + virtual int ObjectCaps( void ) { return (CBaseMonster :: ObjectCaps() & ~FCAP_ACROSS_TRANSITION); } + virtual void Activate( void ); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static TYPEDESCRIPTION m_SaveData[]; + + //LRC: states for script entities + virtual STATE GetState( void ) { return m_iState; }; + STATE m_iState; + + // void EXPORT CineSpawnThink( void ); + void EXPORT CineThink( void ); + void EXPORT InitIdleThink( void ); //LRC + void Pain( void ); + void Die( void ); + void DelayStart( int state ); + CBaseMonster* FindEntity( const char* sName, CBaseEntity *pActivator ); + virtual void PossessEntity( void ); + + inline BOOL IsAction( void ) { return FClassnameIs(pev,"scripted_action"); }; //LRC + + //LRC: Should the monster do a precise attack for this scripted_action? + // (Do a precise attack if we'll be turning to face the target, but we haven't just walked to the target.) + BOOL PreciseAttack( void ) + { + // if (m_fTurnType != 1) { ALERT(at_console,"preciseattack fails check 1\n"); return FALSE; } + // if (m_fMoveTo == 0) { ALERT(at_console,"preciseattack fails check 2\n"); return FALSE; } + // if (m_fMoveTo != 5 && m_iszAttack == 0) { ALERT(at_console,"preciseattack fails check 3\n"); return FALSE; } + // ALERT(at_console,"preciseattack passes!\n"); + // return TRUE; + return m_fTurnType == 1 && ( m_fMoveTo == 5 || (m_fMoveTo != 0 && !FStrEq(STRING(m_iszAttack), STRING(m_iszMoveTarget)) )); + }; + + void ReleaseEntity( CBaseMonster *pEntity ); + void CancelScript( void ); + virtual BOOL StartSequence( CBaseMonster *pTarget, int iszSeq, BOOL completeOnEmpty ); + void SequenceDone ( CBaseMonster *pMonster ); + virtual void FixScriptMonsterSchedule( CBaseMonster *pMonster ); + BOOL CanInterrupt( void ); + void AllowInterrupt( BOOL fAllow ); + int IgnoreConditions( void ); + + int m_iszIdle; // string index for idle animation + int m_iszPlay; // string index for scripted animation + int m_iszEntity; // entity that is wanted for this script + int m_iszAttack; // entity to attack + int m_iszMoveTarget; // entity to move to + int m_iszFireOnBegin; // entity to fire when the sequence _starts_. + int m_fMoveTo; + int m_fTurnType; + int m_fAction; + int m_iFinishSchedule; + float m_flRadius; // range to search +//LRC- this does nothing!! float m_flRepeat; // repeat rate + int m_iRepeats; //LRC - number of times to repeat the animation + int m_iRepeatsLeft; //LRC + float m_fRepeatFrame; //LRC + int m_iPriority; //LRC + + int m_iDelay; + float m_startTime; + + int m_saved_movetype; + int m_saved_solid; + int m_saved_effects; +// Vector m_vecOrigOrigin; + BOOL m_interruptable; +}; + +//LRC - removed CCineAI, obsolete + +#endif //SCRIPTED_H diff --git a/server/monsters/scriptevent.h b/server/monsters/scriptevent.h new file mode 100644 index 00000000..9a02bd64 --- /dev/null +++ b/server/monsters/scriptevent.h @@ -0,0 +1,29 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* Use, distribution, and modification of this source code and/or resulting +* object code is restricted to non-commercial enhancements to products from +* Valve LLC. All other use, distribution, or modification is prohibited +* without written permission from Valve LLC. +* +****/ +#ifndef SCRIPTEVENT_H +#define SCRIPTEVENT_H + +#define SCRIPT_EVENT_DEAD 1000 // character is now dead +#define SCRIPT_EVENT_NOINTERRUPT 1001 // does not allow interrupt +#define SCRIPT_EVENT_CANINTERRUPT 1002 // will allow interrupt +#define SCRIPT_EVENT_FIREEVENT 1003 // event now fires +#define SCRIPT_EVENT_SOUND 1004 // Play named wave file (on CHAN_BODY) +#define SCRIPT_EVENT_SENTENCE 1005 // Play named sentence +#define SCRIPT_EVENT_INAIR 1006 // Leave the character in air at the end of the sequence (don't find the floor) +#define SCRIPT_EVENT_ENDANIMATION 1007 // Set the animation by name after the sequence completes +#define SCRIPT_EVENT_SOUND_VOICE 1008 // Play named wave file (on CHAN_VOICE) +#define SCRIPT_EVENT_SENTENCE_RND1 1009 // Play sentence group 25% of the time +#define SCRIPT_EVENT_NOT_DEAD 1010 // Bring back to life (for life/death sequences) +#endif //SCRIPTEVENT_H diff --git a/server/monsters/squad.h b/server/monsters/squad.h new file mode 100644 index 00000000..9acfccb2 --- /dev/null +++ b/server/monsters/squad.h @@ -0,0 +1,20 @@ +//========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============ +// +// Purpose: New version of the slider bar +// +// $NoKeywords: $ +//============================================================================= + +//========================================================= +// squad.h +//========================================================= + +// these are special group roles that are assigned to members when the group is formed. +// the reason these are explicitly assigned and tasks like throwing grenades to flush out +// enemies is that it's bad to have two members trying to flank left at the same time, but +// ok to have two throwing grenades at the same time. When a squad member cannot attack the +// enemy, it will choose to execute its special role. +#define bits_SQUAD_FLANK_LEFT ( 1 << 0 ) +#define bits_SQUAD_FLANK_RIGHT ( 1 << 1 ) +#define bits_SQUAD_ADVANCE ( 1 << 2 ) +#define bits_SQUAD_FLUSH_ATTACK ( 1 << 3 ) \ No newline at end of file diff --git a/server/monsters/squadmonster.cpp b/server/monsters/squadmonster.cpp new file mode 100644 index 00000000..5c9c7851 --- /dev/null +++ b/server/monsters/squadmonster.cpp @@ -0,0 +1,642 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// Squadmonster functions +//========================================================= +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "nodes.h" +#include "monsters.h" +#include "animation.h" +#include "saverestore.h" +#include "squadmonster.h" +#include "plane.h" + +//========================================================= +// Save/Restore +//========================================================= +TYPEDESCRIPTION CSquadMonster::m_SaveData[] = +{ + DEFINE_FIELD( CSquadMonster, m_hSquadLeader, FIELD_EHANDLE ), + DEFINE_ARRAY( CSquadMonster, m_hSquadMember, FIELD_EHANDLE, MAX_SQUAD_MEMBERS - 1 ), + + DEFINE_FIELD( CSquadMonster, m_fEnemyEluded, FIELD_BOOLEAN ), + DEFINE_FIELD( CSquadMonster, m_flLastEnemySightTime, FIELD_TIME ), + + DEFINE_FIELD( CSquadMonster, m_iMySlot, FIELD_INTEGER ), + + +}; + +IMPLEMENT_SAVERESTORE( CSquadMonster, CBaseMonster ); + + +//========================================================= +// OccupySlot - if any slots of the passed slots are +// available, the monster will be assigned to one. +//========================================================= +BOOL CSquadMonster :: OccupySlot( int iDesiredSlots ) +{ + int i; + int iMask; + int iSquadSlots; + + if ( !InSquad() ) + { + return TRUE; + } + + if ( SquadEnemySplit() ) + { + // if the squad members aren't all fighting the same enemy, slots are disabled + // so that a squad member doesn't get stranded unable to engage his enemy because + // all of the attack slots are taken by squad members fighting other enemies. + m_iMySlot = bits_SLOT_SQUAD_SPLIT; + return TRUE; + } + + CSquadMonster *pSquadLeader = MySquadLeader(); + + if ( !( iDesiredSlots ^ pSquadLeader->m_afSquadSlots ) ) + { + // none of the desired slots are available. + return FALSE; + } + + iSquadSlots = pSquadLeader->m_afSquadSlots; + + for ( i = 0; i < NUM_SLOTS; i++ ) + { + iMask = 1<m_afSquadSlots |= iMask; + m_iMySlot = iMask; +// ALERT ( at_aiconsole, "Took slot %d - %d\n", i, m_hSquadLeader->m_afSquadSlots ); + return TRUE; + } + } + } + + return FALSE; +} + +//========================================================= +// VacateSlot +//========================================================= +void CSquadMonster :: VacateSlot() +{ + if ( m_iMySlot != bits_NO_SLOT && InSquad() ) + { +// ALERT ( at_aiconsole, "Vacated Slot %d - %d\n", m_iMySlot, m_hSquadLeader->m_afSquadSlots ); + MySquadLeader()->m_afSquadSlots &= ~m_iMySlot; + m_iMySlot = bits_NO_SLOT; + } +} + +//========================================================= +// ScheduleChange +//========================================================= +void CSquadMonster :: ScheduleChange ( void ) +{ + VacateSlot(); +} + +//========================================================= +// Killed +//========================================================= +void CSquadMonster :: Killed( entvars_t *pevAttacker, int iGib ) +{ + VacateSlot(); + + if ( InSquad() ) + { + MySquadLeader()->SquadRemove( this ); + } + + CBaseMonster :: Killed ( pevAttacker, iGib ); +} + +// These functions are still awaiting conversion to CSquadMonster + + +//========================================================= +// +// SquadRemove(), remove pRemove from my squad. +// If I am pRemove, promote m_pSquadNext to leader +// +//========================================================= +void CSquadMonster :: SquadRemove( CSquadMonster *pRemove ) +{ + ASSERT( pRemove!=NULL ); + ASSERT( this->IsLeader() ); + ASSERT( pRemove->m_hSquadLeader == this ); + + // If I'm the leader, get rid of my squad + if (pRemove == MySquadLeader()) + { + for (int i = 0; i < MAX_SQUAD_MEMBERS-1;i++) + { + CSquadMonster *pMember = MySquadMember(i); + if (pMember) + { + pMember->m_hSquadLeader = NULL; + m_hSquadMember[i] = NULL; + } + } + } + else + { + CSquadMonster *pSquadLeader = MySquadLeader(); + if (pSquadLeader) + { + for (int i = 0; i < MAX_SQUAD_MEMBERS-1;i++) + { + if (pSquadLeader->m_hSquadMember[i] == this) + { + pSquadLeader->m_hSquadMember[i] = NULL; + break; + } + } + } + } + + pRemove->m_hSquadLeader = NULL; +} + +//========================================================= +// +// SquadAdd(), add pAdd to my squad +// +//========================================================= +BOOL CSquadMonster :: SquadAdd( CSquadMonster *pAdd ) +{ + ASSERT( pAdd!=NULL ); + ASSERT( !pAdd->InSquad() ); + ASSERT( this->IsLeader() ); + + for (int i = 0; i < MAX_SQUAD_MEMBERS-1; i++) + { + if (m_hSquadMember[i] == NULL) + { + m_hSquadMember[i] = pAdd; + pAdd->m_hSquadLeader = this; + return TRUE; + } + } + return FALSE; + // should complain here +} + + +//========================================================= +// +// SquadPasteEnemyInfo - called by squad members that have +// current info on the enemy so that it can be stored for +// members who don't have current info. +// +//========================================================= +void CSquadMonster :: SquadPasteEnemyInfo ( void ) +{ + CSquadMonster *pSquadLeader = MySquadLeader( ); + if (pSquadLeader) + pSquadLeader->m_vecEnemyLKP = m_vecEnemyLKP; +} + +//========================================================= +// +// SquadCopyEnemyInfo - called by squad members who don't +// have current info on the enemy. Reads from the same fields +// in the leader's data that other squad members write to, +// so the most recent data is always available here. +// +//========================================================= +void CSquadMonster :: SquadCopyEnemyInfo ( void ) +{ + CSquadMonster *pSquadLeader = MySquadLeader( ); + if (pSquadLeader) + m_vecEnemyLKP = pSquadLeader->m_vecEnemyLKP; +} + +//========================================================= +// +// SquadMakeEnemy - makes everyone in the squad angry at +// the same entity. +// +//========================================================= +void CSquadMonster :: SquadMakeEnemy ( CBaseEntity *pEnemy ) +{ + if (!InSquad()) + return; + + if ( !pEnemy ) + { + ALERT ( at_debug, "ERROR: SquadMakeEnemy() - pEnemy is NULL!\n" ); + return; + } + + CSquadMonster *pSquadLeader = MySquadLeader( ); + for (int i = 0; i < MAX_SQUAD_MEMBERS; i++) + { + CSquadMonster *pMember = pSquadLeader->MySquadMember(i); + if (pMember) + { + // reset members who aren't activly engaged in fighting + if (pMember->m_hEnemy != pEnemy && !pMember->HasConditions( bits_COND_SEE_ENEMY)) + { + if ( pMember->m_hEnemy != NULL) + { + // remember their current enemy + pMember->PushEnemy( pMember->m_hEnemy, pMember->m_vecEnemyLKP ); + } + // give them a new enemy + pMember->m_hEnemy = pEnemy; + pMember->m_vecEnemyLKP = pEnemy->pev->origin; + pMember->SetConditions ( bits_COND_NEW_ENEMY ); + } + } + } +} + + +//========================================================= +// +// SquadCount(), return the number of members of this squad +// callable from leaders & followers +// +//========================================================= +int CSquadMonster :: SquadCount( void ) +{ + if (!InSquad()) + return 0; + + CSquadMonster *pSquadLeader = MySquadLeader(); + int squadCount = 0; + for (int i = 0; i < MAX_SQUAD_MEMBERS; i++) + { + if (pSquadLeader->MySquadMember(i) != NULL) + squadCount++; + } + + return squadCount; +} + + +//========================================================= +// +// SquadRecruit(), get some monsters of my classification and +// link them as a group. returns the group size +// +//========================================================= +int CSquadMonster :: SquadRecruit( int searchRadius, int maxMembers ) +{ + int squadCount; + int iMyClass = Classify();// cache this monster's class + + + // Don't recruit if I'm already in a group + if ( InSquad() ) + return 0; + + if ( maxMembers < 2 ) + return 0; + + // I am my own leader + m_hSquadLeader = this; + squadCount = 1; + + CBaseEntity *pEntity = NULL; + + if ( !FStringNull( pev->netname ) ) + { + // I have a netname, so unconditionally recruit everyone else with that name. + pEntity = UTIL_FindEntityByString( pEntity, "netname", STRING( pev->netname ) ); + while ( pEntity ) + { + CSquadMonster *pRecruit = pEntity->MySquadMonsterPointer(); + + if ( pRecruit ) + { + if ( !pRecruit->InSquad() && pRecruit->Classify() == iMyClass && pRecruit != this ) + { + // minimum protection here against user error.in worldcraft. + if (!SquadAdd( pRecruit )) + break; + squadCount++; + } + } + + pEntity = UTIL_FindEntityByString( pEntity, "netname", STRING( pev->netname ) ); + } + } + else + { + while ((pEntity = UTIL_FindEntityInSphere( pEntity, pev->origin, searchRadius )) != NULL) + { + CSquadMonster *pRecruit = pEntity->MySquadMonsterPointer( ); + + if ( pRecruit && pRecruit != this && pRecruit->IsAlive() && !pRecruit->m_pCine ) + { + // Can we recruit this guy? + if ( !pRecruit->InSquad() && pRecruit->Classify() == iMyClass && + ( (iMyClass != CLASS_ALIEN_MONSTER) || FStrEq(STRING(pev->classname), STRING(pRecruit->pev->classname))) && + FStringNull( pRecruit->pev->netname ) ) + { + TraceResult tr; + UTIL_TraceLine( pev->origin + pev->view_ofs, pRecruit->pev->origin + pev->view_ofs, ignore_monsters, pRecruit->edict(), &tr );// try to hit recruit with a traceline. + if ( tr.flFraction == 1.0 ) + { + if (!SquadAdd( pRecruit )) + break; + + squadCount++; + } + } + } + } + } + + // no single member squads + if (squadCount == 1) + { + m_hSquadLeader = NULL; + } + + return squadCount; +} + +//========================================================= +// CheckEnemy +//========================================================= +int CSquadMonster :: CheckEnemy ( CBaseEntity *pEnemy ) +{ + int iUpdatedLKP; + + iUpdatedLKP = CBaseMonster :: CheckEnemy ( m_hEnemy ); + + // communicate with squad members about the enemy IF this individual has the same enemy as the squad leader. + if ( InSquad() && (CBaseEntity *)m_hEnemy == MySquadLeader()->m_hEnemy ) + { + if ( iUpdatedLKP ) + { + // have new enemy information, so paste to the squad. + SquadPasteEnemyInfo(); + } + else + { + // enemy unseen, copy from the squad knowledge. + SquadCopyEnemyInfo(); + } + } + + return iUpdatedLKP; +} + +//========================================================= +// StartMonster +//========================================================= +void CSquadMonster :: StartMonster( void ) +{ + CBaseMonster :: StartMonster(); + + if ( ( m_afCapability & bits_CAP_SQUAD ) && !InSquad() ) + { + if ( !FStringNull( pev->netname ) ) + { + // if I have a groupname, I can only recruit if I'm flagged as leader + if ( !( pev->spawnflags & SF_SQUADMONSTER_LEADER ) ) + { + return; + } + } + + // try to form squads now. + int iSquadSize = SquadRecruit( 1024, 4 ); + + if ( iSquadSize ) + { + ALERT ( at_aiconsole, "Squad of %d %s formed\n", iSquadSize, STRING( pev->classname ) ); + } + + if ( IsLeader() && FClassnameIs ( pev, "monster_human_grunt" ) ) + { + SetBodygroup( 1, 1 ); // UNDONE: truly ugly hack + pev->skin = 0; + } + + } +} + +BOOL CSquadMonster :: NoFriendlyFire( void ) +{ + return NoFriendlyFire( FALSE ); //default: don't like the player +} + +//========================================================= +// NoFriendlyFire - checks for possibility of friendly fire +// +// Builds a large box in front of the grunt and checks to see +// if any squad members are in that box. +// +// Can now, also, check whether the player is in the box. LRC +//========================================================= +BOOL CSquadMonster :: NoFriendlyFire( BOOL playerAlly ) +{ + if ( !playerAlly && !InSquad() ) + { + return TRUE; + } + + VPlane backPlane; + VPlane leftPlane; + VPlane rightPlane; + + Vector vecLeftSide; + Vector vecRightSide; + Vector v_left; + + //!!!BUGBUG - to fix this, the planes must be aligned to where the monster will be firing its gun, not the direction it is facing!!! + + if ( m_hEnemy != NULL ) + { + UTIL_MakeVectors ( UTIL_VecToAngles( m_hEnemy->Center() - pev->origin ) ); + } + else + { + // if there's no enemy, pretend there's a friendly in the way, so the grunt won't shoot. + return FALSE; + } + + //UTIL_MakeVectors ( pev->angles ); + + vecLeftSide = pev->origin - ( gpGlobals->v_right * ( pev->size.x * 1.5 ) ); + vecRightSide = pev->origin + ( gpGlobals->v_right * ( pev->size.x * 1.5 ) ); + v_left = gpGlobals->v_right * -1; + + leftPlane.Init ( gpGlobals->v_right, vecLeftSide ); + rightPlane.Init ( v_left, vecRightSide ); + backPlane.Init ( gpGlobals->v_forward, pev->origin ); + +/* + ALERT ( at_console, "LeftPlane: %f %f %f : %f\n", leftPlane.m_vecNormal.x, leftPlane.m_vecNormal.y, leftPlane.m_vecNormal.z, leftPlane.m_flDist ); + ALERT ( at_console, "RightPlane: %f %f %f : %f\n", rightPlane.m_vecNormal.x, rightPlane.m_vecNormal.y, rightPlane.m_vecNormal.z, rightPlane.m_flDist ); + ALERT ( at_console, "BackPlane: %f %f %f : %f\n", backPlane.m_vecNormal.x, backPlane.m_vecNormal.y, backPlane.m_vecNormal.z, backPlane.m_flDist ); +*/ + + CSquadMonster *pSquadLeader = MySquadLeader(); + for (int i = 0; i < MAX_SQUAD_MEMBERS; i++) + { + CSquadMonster *pMember = pSquadLeader->MySquadMember(i); + if (pMember && pMember != this) + { + + if ( backPlane.GetPointSide ( pMember->pev->origin ) && + leftPlane.GetPointSide ( pMember->pev->origin ) && + rightPlane.GetPointSide ( pMember->pev->origin) ) + { + // this guy is in the check volume! Don't shoot! + return FALSE; + } + } + } + + if (playerAlly) + { + edict_t *pentPlayer = FIND_CLIENT_IN_PVS( edict() ); + if (!FNullEnt(pentPlayer) && + backPlane.GetPointSide ( pentPlayer->v.origin ) && + leftPlane.GetPointSide ( pentPlayer->v.origin ) && + rightPlane.GetPointSide ( pentPlayer->v.origin ) ) + { + // the player is in the check volume! Don't shoot! + return FALSE; + } + } + + return TRUE; +} + +//========================================================= +// GetIdealState - surveys the Conditions information available +// and finds the best new state for a monster. +//========================================================= +MONSTERSTATE CSquadMonster :: GetIdealState ( void ) +{ + int iConditions; + + iConditions = IScheduleFlags(); + + // If no schedule conditions, the new ideal state is probably the reason we're in here. + switch ( m_MonsterState ) + { + case MONSTERSTATE_IDLE: + case MONSTERSTATE_ALERT: + if ( HasConditions ( bits_COND_NEW_ENEMY ) && InSquad() ) + { + SquadMakeEnemy ( m_hEnemy ); + } + break; + } + + return CBaseMonster :: GetIdealState(); +} + +//========================================================= +// FValidateCover - determines whether or not the chosen +// cover location is a good one to move to. (currently based +// on proximity to others in the squad) +//========================================================= +BOOL CSquadMonster :: FValidateCover ( const Vector &vecCoverLocation ) +{ + if ( !InSquad() ) + { + return TRUE; + } + + if (SquadMemberInRange( vecCoverLocation, 128 )) + { + // another squad member is too close to this piece of cover. + return FALSE; + } + + return TRUE; +} + +//========================================================= +// SquadEnemySplit- returns TRUE if not all squad members +// are fighting the same enemy. +//========================================================= +BOOL CSquadMonster :: SquadEnemySplit ( void ) +{ + if (!InSquad()) + return FALSE; + + CSquadMonster *pSquadLeader = MySquadLeader(); + CBaseEntity *pEnemy = pSquadLeader->m_hEnemy; + + for (int i = 0; i < MAX_SQUAD_MEMBERS; i++) + { + CSquadMonster *pMember = pSquadLeader->MySquadMember(i); + if (pMember != NULL && pMember->m_hEnemy != NULL && pMember->m_hEnemy != pEnemy) + { + return TRUE; + } + } + return FALSE; +} + +//========================================================= +// FValidateCover - determines whether or not the chosen +// cover location is a good one to move to. (currently based +// on proximity to others in the squad) +//========================================================= +BOOL CSquadMonster :: SquadMemberInRange ( const Vector &vecLocation, float flDist ) +{ + if (!InSquad()) + return FALSE; + + CSquadMonster *pSquadLeader = MySquadLeader(); + + for (int i = 0; i < MAX_SQUAD_MEMBERS; i++) + { + CSquadMonster *pSquadMember = pSquadLeader->MySquadMember(i); + if (pSquadMember && (vecLocation - pSquadMember->pev->origin ).Length2D() <= flDist) + return TRUE; + } + return FALSE; +} + + +extern Schedule_t slChaseEnemyFailed[]; + +Schedule_t *CSquadMonster::GetScheduleOfType( int iType ) +{ + switch ( iType ) + { + + case SCHED_CHASE_ENEMY_FAILED: + { + return &slChaseEnemyFailed[ 0 ]; + } + + default: + return CBaseMonster::GetScheduleOfType( iType ); + } +} + diff --git a/server/monsters/squadmonster.h b/server/monsters/squadmonster.h new file mode 100644 index 00000000..8519bdd7 --- /dev/null +++ b/server/monsters/squadmonster.h @@ -0,0 +1,121 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// CSquadMonster - all the extra data for monsters that +// form squads. +//========================================================= + +#define SF_SQUADMONSTER_LEADER 32 + + +#define bits_NO_SLOT 0 + +// HUMAN GRUNT SLOTS +#define bits_SLOT_HGRUNT_ENGAGE1 ( 1 << 0 ) +#define bits_SLOT_HGRUNT_ENGAGE2 ( 1 << 1 ) +#define bits_SLOTS_HGRUNT_ENGAGE ( bits_SLOT_HGRUNT_ENGAGE1 | bits_SLOT_HGRUNT_ENGAGE2 ) + +#define bits_SLOT_HGRUNT_GRENADE1 ( 1 << 2 ) +#define bits_SLOT_HGRUNT_GRENADE2 ( 1 << 3 ) +#define bits_SLOTS_HGRUNT_GRENADE ( bits_SLOT_HGRUNT_GRENADE1 | bits_SLOT_HGRUNT_GRENADE2 ) + +// ALIEN GRUNT SLOTS +#define bits_SLOT_AGRUNT_HORNET1 ( 1 << 4 ) +#define bits_SLOT_AGRUNT_HORNET2 ( 1 << 5 ) +#define bits_SLOT_AGRUNT_CHASE ( 1 << 6 ) +#define bits_SLOTS_AGRUNT_HORNET ( bits_SLOT_AGRUNT_HORNET1 | bits_SLOT_AGRUNT_HORNET2 ) + +// HOUNDEYE SLOTS +#define bits_SLOT_HOUND_ATTACK1 ( 1 << 7 ) +#define bits_SLOT_HOUND_ATTACK2 ( 1 << 8 ) +#define bits_SLOT_HOUND_ATTACK3 ( 1 << 9 ) +#define bits_SLOTS_HOUND_ATTACK ( bits_SLOT_HOUND_ATTACK1 | bits_SLOT_HOUND_ATTACK2 | bits_SLOT_HOUND_ATTACK3 ) + +// global slots +#define bits_SLOT_SQUAD_SPLIT ( 1 << 10 )// squad members don't all have the same enemy + +#define NUM_SLOTS 11// update this every time you add/remove a slot. + +#define MAX_SQUAD_MEMBERS 5 + +//========================================================= +// CSquadMonster - for any monster that forms squads. +//========================================================= +class CSquadMonster : public CBaseMonster +{ +public: + // squad leader info + EHANDLE m_hSquadLeader; // who is my leader + EHANDLE m_hSquadMember[MAX_SQUAD_MEMBERS-1]; // valid only for leader + int m_afSquadSlots; + float m_flLastEnemySightTime; // last time anyone in the squad saw the enemy + BOOL m_fEnemyEluded; + + // squad member info + int m_iMySlot;// this is the behaviour slot that the monster currently holds in the squad. + + int CheckEnemy ( CBaseEntity *pEnemy ); + void StartMonster ( void ); + void VacateSlot( void ); + void ScheduleChange( void ); + void Killed( entvars_t *pevAttacker, int iGib ); + BOOL OccupySlot( int iDesiredSlot ); + BOOL NoFriendlyFire( void ); + BOOL NoFriendlyFire( BOOL playerAlly ); + + // squad functions still left in base class + CSquadMonster *MySquadLeader( ) + { + CSquadMonster *pSquadLeader = (CSquadMonster *)((CBaseEntity *)m_hSquadLeader); + if (pSquadLeader != NULL) + return pSquadLeader; + return this; + } + CSquadMonster *MySquadMember( int i ) + { + if (i >= MAX_SQUAD_MEMBERS-1) + return this; + else + return (CSquadMonster *)((CBaseEntity *)m_hSquadMember[i]); + } + int InSquad ( void ) { return m_hSquadLeader != NULL; } + int IsLeader ( void ) { return m_hSquadLeader == this; } + int SquadJoin ( int searchRadius ); + int SquadRecruit ( int searchRadius, int maxMembers ); + int SquadCount( void ); + void SquadRemove( CSquadMonster *pRemove ); + void SquadUnlink( void ); + BOOL SquadAdd( CSquadMonster *pAdd ); + void SquadDisband( void ); + void SquadAddConditions ( int iConditions ); + void SquadMakeEnemy ( CBaseEntity *pEnemy ); + void SquadPasteEnemyInfo ( void ); + void SquadCopyEnemyInfo ( void ); + BOOL SquadEnemySplit ( void ); + BOOL SquadMemberInRange( const Vector &vecLocation, float flDist ); + + virtual CSquadMonster *MySquadMonsterPointer( void ) { return this; } + + static TYPEDESCRIPTION m_SaveData[]; + + int Save( CSave &save ); + int Restore( CRestore &restore ); + + BOOL FValidateCover ( const Vector &vecCoverLocation ); + + MONSTERSTATE GetIdealState ( void ); + Schedule_t *GetScheduleOfType ( int iType ); +}; + diff --git a/server/monsters/talkmonster.cpp b/server/monsters/talkmonster.cpp new file mode 100644 index 00000000..472684da --- /dev/null +++ b/server/monsters/talkmonster.cpp @@ -0,0 +1,1557 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "talkmonster.h" +#include "defaultai.h" +#include "scripted.h" +#include "soundent.h" +#include "animation.h" + +//========================================================= +// Talking monster base class +// Used for scientists and barneys +//========================================================= +float CTalkMonster::g_talkWaitTime = 0; // time delay until it's ok to speak: used so that two NPCs don't talk at once + +// NOTE: m_voicePitch & m_szGrp should be fixed up by precache each save/restore + +TYPEDESCRIPTION CTalkMonster::m_SaveData[] = +{ + DEFINE_FIELD( CTalkMonster, m_bitsSaid, FIELD_INTEGER ), + DEFINE_FIELD( CTalkMonster, m_nSpeak, FIELD_INTEGER ), + + // Recalc'ed in Precache() + // DEFINE_FIELD( CTalkMonster, m_voicePitch, FIELD_INTEGER ), + // DEFINE_FIELD( CTalkMonster, m_szGrp, FIELD_??? ), + DEFINE_FIELD( CTalkMonster, m_useTime, FIELD_TIME ), + DEFINE_FIELD( CTalkMonster, m_iszUse, FIELD_STRING ), + DEFINE_FIELD( CTalkMonster, m_iszUnUse, FIELD_STRING ), + DEFINE_FIELD( CTalkMonster, m_iszDecline, FIELD_STRING ), //LRC + DEFINE_FIELD( CTalkMonster, m_iszSpeakAs, FIELD_STRING ), //LRC + DEFINE_FIELD( CTalkMonster, m_flLastSaidSmelled, FIELD_TIME ), + DEFINE_FIELD( CTalkMonster, m_flStopTalkTime, FIELD_TIME ), + DEFINE_FIELD( CTalkMonster, m_hTalkTarget, FIELD_EHANDLE ), +}; + +IMPLEMENT_SAVERESTORE( CTalkMonster, CBaseMonster ); + +// array of friend names +char *CTalkMonster::m_szFriends[TLK_CFRIENDS] = +{ + "monster_barney", + "monster_scientist", + "monster_sitting_scientist", +}; + + +//========================================================= +// AI Schedules Specific to talking monsters +//========================================================= + +Task_t tlIdleResponse[] = +{ + { TASK_SET_ACTIVITY, (float)ACT_IDLE },// Stop and listen + { TASK_WAIT, (float)0.5 },// Wait until sure it's me they are talking to + { TASK_TLK_EYECONTACT, (float)0 },// Wait until speaker is done + { TASK_TLK_RESPOND, (float)0 },// Wait and then say my response + { TASK_TLK_IDEALYAW, (float)0 },// look at who I'm talking to + { TASK_FACE_IDEAL, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_SIGNAL3 }, + { TASK_TLK_EYECONTACT, (float)0 },// Wait until speaker is done +}; + +Schedule_t slIdleResponse[] = +{ + { + tlIdleResponse, + ARRAYSIZE ( tlIdleResponse ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE, + 0, + "Idle Response" + + }, +}; + +Task_t tlIdleSpeak[] = +{ + { TASK_TLK_SPEAK, (float)0 },// question or remark + { TASK_TLK_IDEALYAW, (float)0 },// look at who I'm talking to + { TASK_FACE_IDEAL, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_SIGNAL3 }, + { TASK_TLK_EYECONTACT, (float)0 }, + { TASK_WAIT_RANDOM, (float)0.5 }, +}; + +Schedule_t slIdleSpeak[] = +{ + { + tlIdleSpeak, + ARRAYSIZE ( tlIdleSpeak ), + bits_COND_NEW_ENEMY | + bits_COND_CLIENT_PUSH | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE, + 0, + "Idle Speak" + }, +}; + +Task_t tlIdleSpeakWait[] = +{ + { TASK_SET_ACTIVITY, (float)ACT_SIGNAL3 },// Stop and talk + { TASK_TLK_SPEAK, (float)0 },// question or remark + { TASK_TLK_EYECONTACT, (float)0 },// + { TASK_WAIT, (float)2 },// wait - used when sci is in 'use' mode to keep head turned +}; + +Schedule_t slIdleSpeakWait[] = +{ + { + tlIdleSpeakWait, + ARRAYSIZE ( tlIdleSpeakWait ), + bits_COND_NEW_ENEMY | + bits_COND_CLIENT_PUSH | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE, + 0, + "Idle Speak Wait" + }, +}; + +Task_t tlIdleHello[] = +{ + { TASK_SET_ACTIVITY, (float)ACT_SIGNAL3 },// Stop and talk + { TASK_TLK_HELLO, (float)0 },// Try to say hello to player + { TASK_TLK_EYECONTACT, (float)0 }, + { TASK_WAIT, (float)0.5 },// wait a bit + { TASK_TLK_HELLO, (float)0 },// Try to say hello to player + { TASK_TLK_EYECONTACT, (float)0 }, + { TASK_WAIT, (float)0.5 },// wait a bit + { TASK_TLK_HELLO, (float)0 },// Try to say hello to player + { TASK_TLK_EYECONTACT, (float)0 }, + { TASK_WAIT, (float)0.5 },// wait a bit + { TASK_TLK_HELLO, (float)0 },// Try to say hello to player + { TASK_TLK_EYECONTACT, (float)0 }, + { TASK_WAIT, (float)0.5 },// wait a bit + +}; + +Schedule_t slIdleHello[] = +{ + { + tlIdleHello, + ARRAYSIZE ( tlIdleHello ), + bits_COND_NEW_ENEMY | + bits_COND_CLIENT_PUSH | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_PROVOKED, + + bits_SOUND_COMBAT, + "Idle Hello" + }, +}; + +Task_t tlIdleStopShooting[] = +{ + { TASK_TLK_STOPSHOOTING, (float)0 },// tell player to stop shooting friend + // { TASK_TLK_EYECONTACT, (float)0 },// look at the player +}; + +Schedule_t slIdleStopShooting[] = +{ + { + tlIdleStopShooting, + ARRAYSIZE ( tlIdleStopShooting ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND, + 0, + "Idle Stop Shooting" + }, +}; + +Task_t tlMoveAway[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_MOVE_AWAY_FAIL }, + { TASK_STORE_LASTPOSITION, (float)0 }, + { TASK_MOVE_AWAY_PATH, (float)100 }, + { TASK_WALK_PATH_FOR_UNITS, (float)100 }, + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_PLAYER, (float)0.5 }, +}; + +Schedule_t slMoveAway[] = +{ + { + tlMoveAway, + ARRAYSIZE ( tlMoveAway ), + 0, + 0, + "MoveAway" + }, +}; + + +Task_t tlMoveAwayFail[] = +{ + { TASK_STOP_MOVING, (float)0 }, + { TASK_FACE_PLAYER, (float)0.5 }, +}; + +Schedule_t slMoveAwayFail[] = +{ + { + tlMoveAwayFail, + ARRAYSIZE ( tlMoveAwayFail ), + 0, + 0, + "MoveAwayFail" + }, +}; + + + +Task_t tlMoveAwayFollow[] = +{ + { TASK_SET_FAIL_SCHEDULE, (float)SCHED_TARGET_FACE }, + { TASK_STORE_LASTPOSITION, (float)0 }, + { TASK_MOVE_AWAY_PATH, (float)100 }, + { TASK_WALK_PATH_FOR_UNITS, (float)100 }, + { TASK_STOP_MOVING, (float)0 }, + { TASK_SET_SCHEDULE, (float)SCHED_TARGET_FACE }, +}; + +Schedule_t slMoveAwayFollow[] = +{ + { + tlMoveAwayFollow, + ARRAYSIZE ( tlMoveAwayFollow ), + 0, + 0, + "MoveAwayFollow" + }, +}; + +Task_t tlTlkIdleWatchClient[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_TLK_LOOK_AT_CLIENT, (float)6 }, +}; + +Task_t tlTlkIdleWatchClientStare[] = +{ + { TASK_STOP_MOVING, 0 }, + { TASK_SET_ACTIVITY, (float)ACT_IDLE }, + { TASK_TLK_CLIENT_STARE, (float)6 }, + { TASK_TLK_STARE, (float)0 }, + { TASK_TLK_IDEALYAW, (float)0 },// look at who I'm talking to + { TASK_FACE_IDEAL, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_SIGNAL3 }, + { TASK_TLK_EYECONTACT, (float)0 }, +}; + +Schedule_t slTlkIdleWatchClient[] = +{ + { + tlTlkIdleWatchClient, + ARRAYSIZE ( tlTlkIdleWatchClient ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_SMELL | + bits_COND_CLIENT_PUSH | + bits_COND_CLIENT_UNSEEN | + bits_COND_PROVOKED, + + bits_SOUND_COMBAT |// sound flags - change these, and you'll break the talking code. + //bits_SOUND_PLAYER | + //bits_SOUND_WORLD | + + bits_SOUND_DANGER | + bits_SOUND_MEAT |// scents + bits_SOUND_CARCASS | + bits_SOUND_GARBAGE, + "TlkIdleWatchClient" + }, + + { + tlTlkIdleWatchClientStare, + ARRAYSIZE ( tlTlkIdleWatchClientStare ), + bits_COND_NEW_ENEMY | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE | + bits_COND_HEAR_SOUND | + bits_COND_SMELL | + bits_COND_CLIENT_PUSH | + bits_COND_CLIENT_UNSEEN | + bits_COND_PROVOKED, + + bits_SOUND_COMBAT |// sound flags - change these, and you'll break the talking code. + //bits_SOUND_PLAYER | + //bits_SOUND_WORLD | + + bits_SOUND_DANGER | + bits_SOUND_MEAT |// scents + bits_SOUND_CARCASS | + bits_SOUND_GARBAGE, + "TlkIdleWatchClientStare" + }, +}; + + +Task_t tlTlkIdleEyecontact[] = +{ + { TASK_TLK_IDEALYAW, (float)0 },// look at who I'm talking to + { TASK_FACE_IDEAL, (float)0 }, + { TASK_SET_ACTIVITY, (float)ACT_SIGNAL3 }, + { TASK_TLK_EYECONTACT, (float)0 },// Wait until speaker is done +}; + +Schedule_t slTlkIdleEyecontact[] = +{ + { + tlTlkIdleEyecontact, + ARRAYSIZE ( tlTlkIdleEyecontact ), + bits_COND_NEW_ENEMY | + bits_COND_CLIENT_PUSH | + bits_COND_LIGHT_DAMAGE | + bits_COND_HEAVY_DAMAGE, + 0, + "TlkIdleEyecontact" + }, +}; + + +DEFINE_CUSTOM_SCHEDULES( CTalkMonster ) +{ + slIdleResponse, + slIdleSpeak, + slIdleHello, + slIdleSpeakWait, + slIdleStopShooting, + slMoveAway, + slMoveAwayFollow, + slMoveAwayFail, + slTlkIdleWatchClient, + &slTlkIdleWatchClient[ 1 ], + slTlkIdleEyecontact, +}; + +IMPLEMENT_CUSTOM_SCHEDULES( CTalkMonster, CBaseMonster ); + + +void CTalkMonster :: SetActivity ( Activity newActivity ) +{ + if (newActivity == ACT_IDLE && IsTalking() ) + newActivity = ACT_SIGNAL3; + + if ( newActivity == ACT_SIGNAL3 && (LookupActivity ( ACT_SIGNAL3 ) == ACTIVITY_NOT_AVAILABLE)) + newActivity = ACT_IDLE; + + CBaseMonster::SetActivity( newActivity ); +} + + +void CTalkMonster :: StartTask( Task_t *pTask ) +{ + switch ( pTask->iTask ) + { + case TASK_TLK_SPEAK: + // ask question or make statement + FIdleSpeak(); + TaskComplete(); + break; + + case TASK_TLK_RESPOND: + // respond to question + IdleRespond(); + TaskComplete(); + break; + + case TASK_TLK_HELLO: + // greet player + FIdleHello(); + TaskComplete(); + break; + + + case TASK_TLK_STARE: + // let the player know I know he's staring at me. + FIdleStare(); + TaskComplete(); + break; + + case TASK_FACE_PLAYER: + case TASK_TLK_LOOK_AT_CLIENT: + case TASK_TLK_CLIENT_STARE: + // track head to the client for a while. + m_flWaitFinished = gpGlobals->time + pTask->flData; + break; + + case TASK_TLK_EYECONTACT: + break; + + case TASK_TLK_IDEALYAW: + if (m_hTalkTarget != NULL) + { + pev->yaw_speed = 60; + float yaw = VecToYaw(m_hTalkTarget->pev->origin - pev->origin) - pev->angles.y; + + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + + if (yaw < 0) + { + pev->ideal_yaw = min( yaw + 45, 0 ) + pev->angles.y; + } + else + { + pev->ideal_yaw = max( yaw - 45, 0 ) + pev->angles.y; + } + } + TaskComplete(); + break; + + case TASK_TLK_HEADRESET: + // reset head position after looking at something + m_hTalkTarget = NULL; + TaskComplete(); + break; + + case TASK_TLK_STOPSHOOTING: + // tell player to stop shooting + PlaySentence( m_szGrp[TLK_NOSHOOT], RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_NORM ); + TaskComplete(); + break; + + case TASK_CANT_FOLLOW: + StopFollowing( FALSE ); + PlaySentence( m_szGrp[TLK_STOP], RANDOM_FLOAT(2, 2.5), VOL_NORM, ATTN_NORM ); + TaskComplete(); + break; + + case TASK_WALK_PATH_FOR_UNITS: + m_movementActivity = ACT_WALK; + break; + + case TASK_MOVE_AWAY_PATH: + { + Vector dir = pev->angles; + dir.y = pev->ideal_yaw + 180; + Vector move; + + UTIL_MakeVectorsPrivate( dir, move, NULL, NULL ); + dir = pev->origin + move * pTask->flData; + if ( MoveToLocation( ACT_WALK, 2, dir ) ) + { + TaskComplete(); + } + else if ( FindCover( pev->origin, pev->view_ofs, 0, CoverRadius() ) ) + { + // then try for plain ole cover + m_flMoveWaitFinished = gpGlobals->time + 2; + TaskComplete(); + } + else + { + // nowhere to go? + TaskFail(); + } + } + break; + + case TASK_PLAY_SCRIPT: + m_hTalkTarget = NULL; + CBaseMonster::StartTask( pTask ); + break; + + default: + CBaseMonster::StartTask( pTask ); + } +} + + +void CTalkMonster :: RunTask( Task_t *pTask ) +{ + switch( pTask->iTask ) + { + case TASK_TLK_CLIENT_STARE: + case TASK_TLK_LOOK_AT_CLIENT: + + edict_t *pPlayer; + + // track head to the client for a while. + if ( m_MonsterState == MONSTERSTATE_IDLE && + !IsMoving() && + !IsTalking() ) + { + // Get edict for one player + pPlayer = g_engfuncs.pfnPEntityOfEntIndex( 1 ); + + if ( pPlayer ) + { + IdleHeadTurn( pPlayer->v.origin ); + } + } + else + { + // started moving or talking + TaskFail(); + return; + } + + if ( pTask->iTask == TASK_TLK_CLIENT_STARE ) + { + // fail out if the player looks away or moves away. + if ( ( pPlayer->v.origin - pev->origin ).Length2D() > TLK_STARE_DIST ) + { + // player moved away. + TaskFail(); + } + + UTIL_MakeVectors( pPlayer->v.angles ); + if ( UTIL_DotPoints( pPlayer->v.origin, pev->origin, gpGlobals->v_forward ) < m_flFieldOfView ) + { + // player looked away + TaskFail(); + } + } + + if ( gpGlobals->time > m_flWaitFinished ) + { + TaskComplete(); + } + break; + + case TASK_FACE_PLAYER: + { + // Get edict for one player + edict_t *pPlayer = g_engfuncs.pfnPEntityOfEntIndex( 1 ); + + if ( pPlayer ) + { + MakeIdealYaw ( pPlayer->v.origin ); + ChangeYaw ( pev->yaw_speed ); + IdleHeadTurn( pPlayer->v.origin ); + if ( gpGlobals->time > m_flWaitFinished && FlYawDiff() < 10 ) + { + TaskComplete(); + } + } + else + { + TaskFail(); + } + } + break; + + case TASK_TLK_EYECONTACT: + if (!IsMoving() && IsTalking() && m_hTalkTarget != NULL) + { + // ALERT( at_console, "waiting %f\n", m_flStopTalkTime - gpGlobals->time ); + IdleHeadTurn( m_hTalkTarget->pev->origin ); + } + else + { + TaskComplete(); + } + break; + + case TASK_WALK_PATH_FOR_UNITS: + { + float distance; + + distance = (m_vecLastPosition - pev->origin).Length2D(); + + // Walk path until far enough away + if ( distance > pTask->flData || MovementIsComplete() ) + { + TaskComplete(); + RouteClear(); // Stop moving + } + } + break; + case TASK_WAIT_FOR_MOVEMENT: + if (IsTalking() && m_hTalkTarget != NULL) + { + // ALERT(at_console, "walking, talking\n"); + IdleHeadTurn( m_hTalkTarget->pev->origin ); + } + else + { + IdleHeadTurn( pev->origin ); + // override so that during walk, a scientist may talk and greet player + FIdleHello(); + if (RANDOM_LONG(0,m_nSpeak * 20) == 0) + { + FIdleSpeak(); + } + } + + CBaseMonster::RunTask( pTask ); + if (TaskIsComplete()) + IdleHeadTurn( pev->origin ); + break; + + default: + if (IsTalking() && m_hTalkTarget != NULL) + { + IdleHeadTurn( m_hTalkTarget->pev->origin ); + } + else + { + SetBoneController( 0, 0 ); + } + CBaseMonster::RunTask( pTask ); + } +} + + +void CTalkMonster :: Killed( entvars_t *pevAttacker, int iGib ) +{ + // If a client killed me (unless I was already Barnacle'd), make everyone else mad/afraid of him + if ( (pevAttacker->flags & FL_CLIENT) && m_MonsterState != MONSTERSTATE_PRONE ) + { + AlertFriends(); + LimitFollowers( CBaseEntity::Instance(pevAttacker), 0 ); + } + + m_hTargetEnt = NULL; + // Don't finish that sentence + StopTalking(); + SetUse( NULL ); + CBaseMonster::Killed( pevAttacker, iGib ); +} + + + +CBaseEntity *CTalkMonster::EnumFriends( CBaseEntity *pPrevious, int listNumber, BOOL bTrace ) +{ + CBaseEntity *pFriend = pPrevious; + char *pszFriend; + TraceResult tr; + Vector vecCheck; + + pszFriend = m_szFriends[ FriendNumber(listNumber) ]; + while (pFriend = UTIL_FindEntityByClassname( pFriend, pszFriend )) + { + if (pFriend == this || !pFriend->IsAlive()) + // don't talk to self or dead people + continue; + if ( bTrace ) + { + vecCheck = pFriend->pev->origin; + vecCheck.z = pFriend->pev->absmax.z; + + UTIL_TraceLine( pev->origin, vecCheck, ignore_monsters, ENT(pev), &tr); + } + else + tr.flFraction = 1.0; + + if (tr.flFraction == 1.0) + { + return pFriend; + } + } + + return NULL; +} + + +void CTalkMonster::AlertFriends( void ) +{ + CBaseEntity *pFriend = NULL; + int i; + + // for each friend in this bsp... + for ( i = 0; i < TLK_CFRIENDS; i++ ) + { + while (pFriend = EnumFriends( pFriend, i, TRUE )) + { + CBaseMonster *pMonster = pFriend->MyMonsterPointer(); + if ( pMonster->IsAlive() ) + { + // don't provoke a friend that's playing a death animation. They're a goner + pMonster->m_afMemory |= bits_MEMORY_PROVOKED; + } + } + } +} + + + +void CTalkMonster::ShutUpFriends( void ) +{ + CBaseEntity *pFriend = NULL; + int i; + + // for each friend in this bsp... + for ( i = 0; i < TLK_CFRIENDS; i++ ) + { + while (pFriend = EnumFriends( pFriend, i, TRUE )) + { + CBaseMonster *pMonster = pFriend->MyMonsterPointer(); + if ( pMonster ) + { + pMonster->SentenceStop(); + } + } + } +} + + +// UNDONE: Keep a follow time in each follower, make a list of followers in this function and do LRU +// UNDONE: Check this in Restore to keep restored monsters from joining a full list of followers +void CTalkMonster::LimitFollowers( CBaseEntity *pPlayer, int maxFollowers ) +{ + CBaseEntity *pFriend = NULL; + int i, count; + + count = 0; + // for each friend in this bsp... + for ( i = 0; i < TLK_CFRIENDS; i++ ) + { + while (pFriend = EnumFriends( pFriend, i, FALSE )) + { + CBaseMonster *pMonster = pFriend->MyMonsterPointer(); + if ( pMonster ) + { + if ( pMonster->m_hTargetEnt == pPlayer ) + { + count++; + if ( count > maxFollowers ) + pMonster->StopFollowing( TRUE ); + } + } + } + } +} + + +float CTalkMonster::TargetDistance( void ) +{ + // If we lose the player, or he dies, return a really large distance + if ( m_hTargetEnt == NULL || !m_hTargetEnt->IsAlive() ) + return 1e6; + + return (m_hTargetEnt->pev->origin - pev->origin).Length(); +} + + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +//========================================================= +void CTalkMonster :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case SCRIPT_EVENT_SENTENCE_RND1: // Play a named sentence group 25% of the time + if (RANDOM_LONG(0,99) < 75) + break; + // fall through... + case SCRIPT_EVENT_SENTENCE: // Play a named sentence group + ShutUpFriends(); + PlaySentence( pEvent->options, RANDOM_FLOAT(2.8, 3.4), VOL_NORM, ATTN_IDLE ); + //ALERT(at_console, "script event speak\n"); + break; + + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + +// monsters derived from ctalkmonster should call this in precache() + +void CTalkMonster :: TalkInit( void ) +{ + // every new talking monster must reset this global, otherwise + // when a level is loaded, nobody will talk (time is reset to 0) + + CTalkMonster::g_talkWaitTime = 0; + + if (m_iszSpeakAs) //LRC: changing voice groups for monsters + { + char szBuf[64]; + strcpy(szBuf,STRING(m_iszSpeakAs)); + strcat(szBuf,"_"); + char *szAssign = &(szBuf[strlen(szBuf)]); + + //LRC - this is pretty dodgy; test with save/restore. + strcpy(szAssign,"ANSWER"); + m_szGrp[TLK_ANSWER] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"QUESTION"); + m_szGrp[TLK_QUESTION] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"IDLE"); + m_szGrp[TLK_IDLE] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"STARE"); + m_szGrp[TLK_STARE] = STRING(ALLOC_STRING(szBuf)); + if (pev->spawnflags & SF_MONSTER_PREDISASTER) //LRC + strcpy(szAssign,"PFOLLOW"); + else + strcpy(szAssign,"OK"); + m_szGrp[TLK_USE] = STRING(ALLOC_STRING(szBuf)); + if (pev->spawnflags & SF_MONSTER_PREDISASTER) //LRC + strcpy(szAssign,"PWAIT"); + else + strcpy(szAssign,"WAIT"); + m_szGrp[TLK_UNUSE] = STRING(ALLOC_STRING(szBuf)); + if (pev->spawnflags & SF_MONSTER_PREDISASTER) //LRC + strcpy(szAssign,"POK"); + else + strcpy(szAssign,"NOTOK"); + m_szGrp[TLK_DECLINE] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"STOP"); + m_szGrp[TLK_STOP] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"NOSHOOT"); + m_szGrp[TLK_NOSHOOT] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"HELLO"); + m_szGrp[TLK_HELLO] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"PLHURT1"); + m_szGrp[TLK_PLHURT1] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"PLHURT2"); + m_szGrp[TLK_PLHURT2] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"PLHURT3"); + m_szGrp[TLK_PLHURT3] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"PHELLO"); + m_szGrp[TLK_PHELLO] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"PIDLE"); + m_szGrp[TLK_PIDLE] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"PQUESTION"); + m_szGrp[TLK_PQUESTION] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"SMELL"); + m_szGrp[TLK_SMELL] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"WOUND"); + m_szGrp[TLK_WOUND] = STRING(ALLOC_STRING(szBuf)); + strcpy(szAssign,"MORTAL"); + m_szGrp[TLK_MORTAL] = STRING(ALLOC_STRING(szBuf)); + } + + m_voicePitch = 100; +} + +//========================================================= +// FindNearestFriend +// Scan for nearest, visible friend. If fPlayer is true, look for +// nearest player +//========================================================= +CBaseEntity *CTalkMonster :: FindNearestFriend(BOOL fPlayer) +{ + CBaseEntity *pFriend = NULL; + CBaseEntity *pNearest = NULL; + float range = 10000000.0; + TraceResult tr; + Vector vecStart = pev->origin; + Vector vecCheck; + int i; + char *pszFriend; + int cfriends; + + vecStart.z = pev->absmax.z; + + if (fPlayer) + cfriends = 1; + else + cfriends = TLK_CFRIENDS; + + // for each type of friend... + + for (i = cfriends-1; i > -1; i--) + { + if (fPlayer) + pszFriend = "player"; + else + pszFriend = m_szFriends[FriendNumber(i)]; + + if (!pszFriend) + continue; + + // for each friend in this bsp... + while (pFriend = UTIL_FindEntityByClassname( pFriend, pszFriend )) + { + if (pFriend == this || !pFriend->IsAlive()) + // don't talk to self or dead people + continue; + + CBaseMonster *pMonster = pFriend->MyMonsterPointer(); + + // If not a monster for some reason, or in a script, or prone + if ( !pMonster || pMonster->m_MonsterState == MONSTERSTATE_SCRIPT || pMonster->m_MonsterState == MONSTERSTATE_PRONE ) + continue; + + vecCheck = pFriend->pev->origin; + vecCheck.z = pFriend->pev->absmax.z; + + // if closer than previous friend, and in range, see if he's visible + + if (range > (vecStart - vecCheck).Length()) + { + UTIL_TraceLine(vecStart, vecCheck, ignore_monsters, ENT(pev), &tr); + + if (tr.flFraction == 1.0) + { + // visible and in range, this is the new nearest scientist + if ((vecStart - vecCheck).Length() < TALKRANGE_MIN) + { + pNearest = pFriend; + range = (vecStart - vecCheck).Length(); + } + } + } + } + } + return pNearest; +} + +int CTalkMonster :: GetVoicePitch( void ) +{ + return m_voicePitch + RANDOM_LONG(0,3); +} + + +void CTalkMonster :: Touch( CBaseEntity *pOther ) +{ + // Did the player touch me? + if ( pOther->IsPlayer() ) + { + // Ignore if pissed at player + if ( m_afMemory & bits_MEMORY_PROVOKED ) + return; + + // Stay put during speech + if ( IsTalking() ) + return; + + // Heuristic for determining if the player is pushing me away + float speed = fabs(pOther->pev->velocity.x) + fabs(pOther->pev->velocity.y); + if ( speed > 50 ) + { + SetConditions( bits_COND_CLIENT_PUSH ); + MakeIdealYaw( pOther->pev->origin ); + } + } +} + + + +//========================================================= +// IdleRespond +// Respond to a previous question +//========================================================= +void CTalkMonster :: IdleRespond( void ) +{ + int pitch = GetVoicePitch(); + + // play response + PlaySentence( m_szGrp[TLK_ANSWER], RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); +} + +int CTalkMonster :: FOkToSpeak( void ) +{ + // if in the grip of a barnacle, don't speak + if ( m_MonsterState == MONSTERSTATE_PRONE || m_IdealMonsterState == MONSTERSTATE_PRONE ) + { + return FALSE; + } + + // if not alive, certainly don't speak + if ( pev->deadflag != DEAD_NO ) + { + return FALSE; + } + + // if someone else is talking, don't speak + if (gpGlobals->time <= CTalkMonster::g_talkWaitTime) + return FALSE; + + //monster generic can speak always + if ( pev->spawnflags & SF_MONSTER_GAG && !FClassnameIs(pev, "monster_generic") ) + return FALSE; + + if ( m_MonsterState == MONSTERSTATE_PRONE ) + return FALSE; + + // if player is not in pvs, don't speak + if (!IsAlive() || (FNullEnt(FIND_CLIENT_IN_PVS(edict())) && !HaveCamerasInPVS( edict() ))) + return FALSE; + + // don't talk if you're in combat + if (m_hEnemy != NULL && FVisible( m_hEnemy )) + return FALSE; + + return TRUE; +} + + +int CTalkMonster::CanPlaySentence( BOOL fDisregardState ) +{ + if ( fDisregardState ) + return CBaseMonster::CanPlaySentence( fDisregardState ); + return FOkToSpeak(); +} + +//========================================================= +// FIdleStare +//========================================================= +int CTalkMonster :: FIdleStare( void ) +{ + if (!FOkToSpeak()) + return FALSE; + + PlaySentence( m_szGrp[TLK_STARE], RANDOM_FLOAT(5, 7.5), VOL_NORM, ATTN_IDLE ); + + m_hTalkTarget = FindNearestFriend( TRUE ); + return TRUE; +} + +//========================================================= +// IdleHello +// Try to greet player first time he's seen +//========================================================= +int CTalkMonster :: FIdleHello( void ) +{ + if (!FOkToSpeak()) + return FALSE; + + // if this is first time scientist has seen player, greet him + if (!FBitSet(m_bitsSaid, bit_saidHelloPlayer)) + { + // get a player + CBaseEntity *pPlayer = FindNearestFriend(TRUE); + + if (pPlayer) + { + if (FInViewCone(pPlayer) && FVisible(pPlayer)) + { + m_hTalkTarget = pPlayer; + + if (FBitSet(pev->spawnflags, SF_MONSTER_PREDISASTER)) + PlaySentence( m_szGrp[TLK_PHELLO], RANDOM_FLOAT(3, 3.5), VOL_NORM, ATTN_IDLE ); + else + PlaySentence( m_szGrp[TLK_HELLO], RANDOM_FLOAT(3, 3.5), VOL_NORM, ATTN_IDLE ); + + SetBits(m_bitsSaid, bit_saidHelloPlayer); + + return TRUE; + } + } + } + return FALSE; +} + + +// turn head towards supplied origin +void CTalkMonster :: IdleHeadTurn( Vector &vecFriend ) +{ + // turn head in desired direction only if ent has a turnable head + if (m_afCapability & bits_CAP_TURN_HEAD) + { + float yaw = VecToYaw(vecFriend - pev->origin) - pev->angles.y; + + if (yaw > 180) yaw -= 360; + if (yaw < -180) yaw += 360; + + // turn towards vector + SetBoneController( 0, yaw ); + } +} + +//========================================================= +// FIdleSpeak +// ask question of nearby friend, or make statement +//========================================================= +int CTalkMonster :: FIdleSpeak ( void ) +{ + // try to start a conversation, or make statement + int pitch; + const char *szIdleGroup; + const char *szQuestionGroup; + float duration; + + if (!FOkToSpeak()) + return FALSE; + + // set idle groups based on pre/post disaster + if (FBitSet(pev->spawnflags, SF_MONSTER_PREDISASTER)) + { + szIdleGroup = m_szGrp[TLK_PIDLE]; + szQuestionGroup = m_szGrp[TLK_PQUESTION]; + // set global min delay for next conversation + duration = RANDOM_FLOAT(4.8, 5.2); + } + else + { + szIdleGroup = m_szGrp[TLK_IDLE]; + szQuestionGroup = m_szGrp[TLK_QUESTION]; + // set global min delay for next conversation + duration = RANDOM_FLOAT(2.8, 3.2); + + } + + pitch = GetVoicePitch(); + + // player using this entity is alive and wounded? + CBaseEntity *pTarget = m_hTargetEnt; + + if ( pTarget != NULL ) + { + if ( pTarget->IsPlayer() ) + { + if ( pTarget->IsAlive() ) + { + m_hTalkTarget = m_hTargetEnt; + if (!FBitSet(m_bitsSaid, bit_saidDamageHeavy) && + (m_hTargetEnt->pev->health <= m_hTargetEnt->pev->max_health / 8)) + { + //EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, m_szGrp[TLK_PLHURT3], 1.0, ATTN_IDLE, 0, pitch); + PlaySentence( m_szGrp[TLK_PLHURT3], duration, VOL_NORM, ATTN_IDLE ); + SetBits(m_bitsSaid, bit_saidDamageHeavy); + return TRUE; + } + else if (!FBitSet(m_bitsSaid, bit_saidDamageMedium) && + (m_hTargetEnt->pev->health <= m_hTargetEnt->pev->max_health / 4)) + { + //EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, m_szGrp[TLK_PLHURT2], 1.0, ATTN_IDLE, 0, pitch); + PlaySentence( m_szGrp[TLK_PLHURT2], duration, VOL_NORM, ATTN_IDLE ); + SetBits(m_bitsSaid, bit_saidDamageMedium); + return TRUE; + } + else if (!FBitSet(m_bitsSaid, bit_saidDamageLight) && + (m_hTargetEnt->pev->health <= m_hTargetEnt->pev->max_health / 2)) + { + //EMIT_SOUND_DYN(ENT(pev), CHAN_VOICE, m_szGrp[TLK_PLHURT1], 1.0, ATTN_IDLE, 0, pitch); + PlaySentence( m_szGrp[TLK_PLHURT1], duration, VOL_NORM, ATTN_IDLE ); + SetBits(m_bitsSaid, bit_saidDamageLight); + return TRUE; + } + } + else + { + //!!!KELLY - here's a cool spot to have the talkmonster talk about the dead player if we want. + // "Oh dear, Gordon Freeman is dead!" -Scientist + // "Damn, I can't do this without you." -Barney + } + } + } + + // if there is a friend nearby to speak to, play sentence, set friend's response time, return + CBaseEntity *pFriend = FindNearestFriend(FALSE); + + if (pFriend && !(pFriend->IsMoving()) && (RANDOM_LONG(0,99) < 75)) + { + PlaySentence( szQuestionGroup, duration, VOL_NORM, ATTN_IDLE ); + //SENTENCEG_PlayRndSz( ENT(pev), szQuestionGroup, 1.0, ATTN_IDLE, 0, pitch ); + + // force friend to answer + CTalkMonster *pTalkMonster = (CTalkMonster *)pFriend; + m_hTalkTarget = pFriend; + pTalkMonster->SetAnswerQuestion( this ); // UNDONE: This is EVIL!!! + pTalkMonster->m_flStopTalkTime = m_flStopTalkTime; + + m_nSpeak++; + return TRUE; + } + + // otherwise, play an idle statement, try to face client when making a statement. + if ( RANDOM_LONG(0,1) ) + { + //SENTENCEG_PlayRndSz( ENT(pev), szIdleGroup, 1.0, ATTN_IDLE, 0, pitch ); + CBaseEntity *pFriend = FindNearestFriend(TRUE); + + if ( pFriend ) + { + m_hTalkTarget = pFriend; + PlaySentence( szIdleGroup, duration, VOL_NORM, ATTN_IDLE ); + m_nSpeak++; + return TRUE; + } + } + + // didn't speak + Talk( 0 ); + CTalkMonster::g_talkWaitTime = 0; + return FALSE; +} + +void CTalkMonster::PlayScriptedSentence( const char *pszSentence, float duration, float volume, float attenuation, BOOL bConcurrent, CBaseEntity *pListener ) +{ + if ( !bConcurrent ) + ShutUpFriends(); + + ClearConditions( bits_COND_CLIENT_PUSH ); // Forget about moving! I've got something to say! + m_useTime = gpGlobals->time + duration; + PlaySentence( pszSentence, duration, volume, attenuation ); + + m_hTalkTarget = pListener; +} + +void CTalkMonster::PlaySentence( const char *pszSentence, float duration, float volume, float attenuation ) +{ + if ( !pszSentence ) + return; + + Talk ( duration ); + + CTalkMonster::g_talkWaitTime = gpGlobals->time + duration + 2.0; + if ( pszSentence[0] == '!' ) + EMIT_SOUND_DYN( edict(), CHAN_VOICE, pszSentence, volume, attenuation, 0, GetVoicePitch()); + else + SENTENCEG_PlayRndSz( edict(), pszSentence, volume, attenuation, 0, GetVoicePitch() ); + + // If you say anything, don't greet the player - you may have already spoken to them + SetBits(m_bitsSaid, bit_saidHelloPlayer); +} + +//========================================================= +// Talk - set a timer that tells us when the monster is done +// talking. +//========================================================= +void CTalkMonster :: Talk( float flDuration ) +{ + if ( flDuration <= 0 ) + { + // no duration :( + m_flStopTalkTime = gpGlobals->time + 3; + } + else + { + m_flStopTalkTime = gpGlobals->time + flDuration; + } +} + +// Prepare this talking monster to answer question +void CTalkMonster :: SetAnswerQuestion( CTalkMonster *pSpeaker ) +{ + if ( !m_pCine ) + ChangeSchedule( slIdleResponse ); + m_hTalkTarget = (CBaseMonster *)pSpeaker; +} + +int CTalkMonster :: TakeDamage( entvars_t* pevInflictor, entvars_t* pevAttacker, float flDamage, int bitsDamageType) +{ + if ( IsAlive() ) + { + // if player damaged this entity, have other friends talk about it + if (pevAttacker && m_MonsterState != MONSTERSTATE_PRONE && FBitSet(pevAttacker->flags, FL_CLIENT)) + { + CBaseEntity *pFriend = FindNearestFriend(FALSE); + + if (pFriend && pFriend->IsAlive()) + { + // only if not dead or dying! + CTalkMonster *pTalkMonster = (CTalkMonster *)pFriend; + pTalkMonster->ChangeSchedule( slIdleStopShooting ); + } + } + } + return CBaseMonster::TakeDamage(pevInflictor, pevAttacker, flDamage, bitsDamageType); +} + + +Schedule_t* CTalkMonster :: GetScheduleOfType ( int Type ) +{ + switch( Type ) + { + case SCHED_MOVE_AWAY: + return slMoveAway; + + case SCHED_MOVE_AWAY_FOLLOW: + return slMoveAwayFollow; + + case SCHED_MOVE_AWAY_FAIL: + return slMoveAwayFail; + + case SCHED_TARGET_FACE: + // speak during 'use' + if (RANDOM_LONG(0,99) < 2) + //ALERT ( at_console, "target chase speak\n" ); + return slIdleSpeakWait; + else + return slIdleStand; + + case SCHED_IDLE_STAND: + { + // if never seen player, try to greet him + if (!FBitSet(m_bitsSaid, bit_saidHelloPlayer)) + { + return slIdleHello; + } + + // sustained light wounds? + if (!FBitSet(m_bitsSaid, bit_saidWoundLight) && (pev->health <= (pev->max_health * 0.75))) + { + //SENTENCEG_PlayRndSz( ENT(pev), m_szGrp[TLK_WOUND], 1.0, ATTN_IDLE, 0, GetVoicePitch() ); + //CTalkMonster::g_talkWaitTime = gpGlobals->time + RANDOM_FLOAT(2.8, 3.2); + PlaySentence( m_szGrp[TLK_WOUND], RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); + SetBits(m_bitsSaid, bit_saidWoundLight); + return slIdleStand; + } + // sustained heavy wounds? + else if (!FBitSet(m_bitsSaid, bit_saidWoundHeavy) && (pev->health <= (pev->max_health * 0.5))) + { + //SENTENCEG_PlayRndSz( ENT(pev), m_szGrp[TLK_MORTAL], 1.0, ATTN_IDLE, 0, GetVoicePitch() ); + //CTalkMonster::g_talkWaitTime = gpGlobals->time + RANDOM_FLOAT(2.8, 3.2); + PlaySentence( m_szGrp[TLK_MORTAL], RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); + SetBits(m_bitsSaid, bit_saidWoundHeavy); + return slIdleStand; + } + + // talk about world + if (FOkToSpeak() && RANDOM_LONG(0,m_nSpeak * 2) == 0) + { + //ALERT ( at_console, "standing idle speak\n" ); + return slIdleSpeak; + } + + if ( !IsTalking() && HasConditions ( bits_COND_SEE_CLIENT ) && RANDOM_LONG( 0, 6 ) == 0 ) + { + edict_t *pPlayer = g_engfuncs.pfnPEntityOfEntIndex( 1 ); + + if ( pPlayer ) + { + // watch the client. + UTIL_MakeVectors ( pPlayer->v.angles ); + if ( ( pPlayer->v.origin - pev->origin ).Length2D() < TLK_STARE_DIST && + UTIL_DotPoints( pPlayer->v.origin, pev->origin, gpGlobals->v_forward ) >= m_flFieldOfView ) + { + // go into the special STARE schedule if the player is close, and looking at me too. + return &slTlkIdleWatchClient[ 1 ]; + } + + return slTlkIdleWatchClient; + } + } + else + { + if (IsTalking()) + // look at who we're talking to + return slTlkIdleEyecontact; + else + // regular standing idle + return slIdleStand; + } + + + // NOTE - caller must first CTalkMonster::GetScheduleOfType, + // then check result and decide what to return ie: if sci gets back + // slIdleStand, return slIdleSciStand + } + break; + } + + return CBaseMonster::GetScheduleOfType( Type ); +} + +//========================================================= +// IsTalking - am I saying a sentence right now? +//========================================================= +BOOL CTalkMonster :: IsTalking( void ) +{ + if ( m_flStopTalkTime > gpGlobals->time ) + { + return TRUE; + } + + return FALSE; +} + +//========================================================= +// If there's a player around, watch him. +//========================================================= +void CTalkMonster :: PrescheduleThink ( void ) +{ + if ( !HasConditions ( bits_COND_SEE_CLIENT ) ) + { + SetConditions ( bits_COND_CLIENT_UNSEEN ); + } +} + +// try to smell something +void CTalkMonster :: TrySmellTalk( void ) +{ + if ( !FOkToSpeak() ) + return; + + // clear smell bits periodically + if ( gpGlobals->time > m_flLastSaidSmelled ) + { +// ALERT ( at_aiconsole, "Clear smell bits\n" ); + ClearBits(m_bitsSaid, bit_saidSmelled); + } + // smelled something? + if (!FBitSet(m_bitsSaid, bit_saidSmelled) && HasConditions ( bits_COND_SMELL )) + { + PlaySentence( m_szGrp[TLK_SMELL], RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); + m_flLastSaidSmelled = gpGlobals->time + 60;// don't talk about the stinky for a while. + SetBits(m_bitsSaid, bit_saidSmelled); + } +} + + + +int CTalkMonster::IRelationship( CBaseEntity *pTarget ) +{ + if ( pTarget->IsPlayer() ) + if ( m_afMemory & bits_MEMORY_PROVOKED ) + return R_HT; + return CBaseMonster::IRelationship( pTarget ); +} + + +void CTalkMonster::StopFollowing( BOOL clearSchedule ) +{ + if ( IsFollowing() ) + { + if ( !(m_afMemory & bits_MEMORY_PROVOKED) ) + { + PlaySentence( m_szGrp[TLK_UNUSE], RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); + m_hTalkTarget = m_hTargetEnt; + } + + if ( m_movementGoal == MOVEGOAL_TARGETENT ) + RouteClear(); // Stop him from walking toward the player + m_hTargetEnt = NULL; + if ( clearSchedule ) + ClearSchedule(); + if ( m_hEnemy != NULL ) + m_IdealMonsterState = MONSTERSTATE_COMBAT; + } +} + + +void CTalkMonster::StartFollowing( CBaseEntity *pLeader ) +{ + if ( m_pCine ) + m_pCine->CancelScript(); + + if ( m_hEnemy != NULL ) + m_IdealMonsterState = MONSTERSTATE_ALERT; + + m_hTargetEnt = pLeader; + PlaySentence( m_szGrp[TLK_USE], RANDOM_FLOAT(2.8, 3.2), VOL_NORM, ATTN_IDLE ); + m_hTalkTarget = m_hTargetEnt; + ClearConditions( bits_COND_CLIENT_PUSH ); + ClearSchedule(); +} + +//LRC- redefined, now returns true if following would be physically possible +BOOL CTalkMonster::CanFollow( void ) +{ + if ( m_MonsterState == MONSTERSTATE_SCRIPT ) + { + if ( !m_pCine->CanInterrupt() ) + return FALSE; + } + + if ( !IsAlive() ) + return FALSE; + + return TRUE; +} + + +//LRC- rewritten +void CTalkMonster :: FollowerUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + // Don't allow use during a scripted_sentence + if ( m_useTime > gpGlobals->time ) + return; + + //ALERT(at_console,"Talkmonster was Used: "); + + // CanFollow is now true if the monster could physically follow anyone + if ( pCaller != NULL && pCaller->IsPlayer() && CanFollow() ) + { + if ( !IsFollowing() ) + { + // Pre-disaster followers can't be used unless they've got a master to override their behaviour... + if (IsLockedByMaster() || (pev->spawnflags & SF_MONSTER_PREDISASTER && !m_sMaster)) + { + //ALERT(at_console,"Decline\n"); + DeclineFollowing(); + } + else + { + LimitFollowers( pCaller , 1 ); + if ( m_afMemory & bits_MEMORY_PROVOKED ) + { + //ALERT(at_console,"Fail\n"); + ALERT( at_aiconsole, "I'm not following you, you evil person!\n" ); + } + else + { + //ALERT(at_console,"Start\n"); + StartFollowing( pCaller ); + SetBits(m_bitsSaid, bit_saidHelloPlayer); // Don't say hi after you've started following + } + } + } + else + { + //ALERT(at_console,"Stop\n"); + StopFollowing( TRUE ); + } + } +} + +void CTalkMonster::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "UseSentence")) + { + m_iszUse = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "UnUseSentence")) + { + m_iszUnUse = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "RefusalSentence")) //LRC + { + m_iszDecline = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "SpeakAs")) //LRC + { + m_iszSpeakAs = ALLOC_STRING(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else + CBaseMonster::KeyValue( pkvd ); +} + + +void CTalkMonster::Precache( void ) +{ + if ( m_iszUse ) + m_szGrp[TLK_USE] = STRING( m_iszUse ); + if ( m_iszUnUse ) + m_szGrp[TLK_UNUSE] = STRING( m_iszUnUse ); + if ( m_iszDecline ) //LRC + m_szGrp[TLK_DECLINE] = STRING( m_iszDecline ); +} + diff --git a/server/monsters/talkmonster.h b/server/monsters/talkmonster.h new file mode 100644 index 00000000..a6736868 --- /dev/null +++ b/server/monsters/talkmonster.h @@ -0,0 +1,186 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +#ifndef TALKMONSTER_H +#define TALKMONSTER_H + +#ifndef MONSTERS_H +#include "monsters.h" +#endif + +//========================================================= +// Talking monster base class +// Used for scientists and barneys +//========================================================= + +#define TALKRANGE_MIN 500.0 // don't talk to anyone farther away than this + +#define TLK_STARE_DIST 128 // anyone closer than this and looking at me is probably staring at me. + +#define bit_saidDamageLight (1<<0) // bits so we don't repeat key sentences +#define bit_saidDamageMedium (1<<1) +#define bit_saidDamageHeavy (1<<2) +#define bit_saidHelloPlayer (1<<3) +#define bit_saidWoundLight (1<<4) +#define bit_saidWoundHeavy (1<<5) +#define bit_saidHeard (1<<6) +#define bit_saidSmelled (1<<7) + +#define TLK_CFRIENDS 3 + +typedef enum +{ + TLK_ANSWER = 0, + TLK_QUESTION, + TLK_IDLE, + TLK_STARE, + TLK_USE, + TLK_UNUSE, + TLK_DECLINE, //LRC- refuse to accompany + TLK_STOP, + TLK_NOSHOOT, + TLK_HELLO, + TLK_PHELLO, + TLK_PIDLE, + TLK_PQUESTION, + TLK_PLHURT1, + TLK_PLHURT2, + TLK_PLHURT3, + TLK_SMELL, + TLK_WOUND, + TLK_MORTAL, + + TLK_CGROUPS, // MUST be last entry +} TALKGROUPNAMES; + + +enum +{ + SCHED_CANT_FOLLOW = LAST_COMMON_SCHEDULE + 1, + SCHED_MOVE_AWAY, // Try to get out of the player's way + SCHED_MOVE_AWAY_FOLLOW, // same, but follow afterward + SCHED_MOVE_AWAY_FAIL, // Turn back toward player + + LAST_TALKMONSTER_SCHEDULE, // MUST be last +}; + +enum +{ + TASK_CANT_FOLLOW = LAST_COMMON_TASK + 1, + TASK_MOVE_AWAY_PATH, + TASK_WALK_PATH_FOR_UNITS, + + TASK_TLK_RESPOND, // say my response + TASK_TLK_SPEAK, // question or remark + TASK_TLK_HELLO, // Try to say hello to player + TASK_TLK_HEADRESET, // reset head position + TASK_TLK_STOPSHOOTING, // tell player to stop shooting friend + TASK_TLK_STARE, // let the player know I know he's staring at me. + TASK_TLK_LOOK_AT_CLIENT,// faces player if not moving and not talking and in idle. + TASK_TLK_CLIENT_STARE, // same as look at client, but says something if the player stares. + TASK_TLK_EYECONTACT, // maintain eyecontact with person who I'm talking to + TASK_TLK_IDEALYAW, // set ideal yaw to face who I'm talking to + TASK_FACE_PLAYER, // Face the player + + LAST_TALKMONSTER_TASK, // MUST be last +}; + +class CTalkMonster : public CBaseMonster +{ +public: + void TalkInit( void ); + CBaseEntity *FindNearestFriend(BOOL fPlayer); + float TargetDistance( void ); + void StopTalking( void ) { SentenceStop(); } + + // Base Monster functions + void Precache( void ); + int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType); + void Touch( CBaseEntity *pOther ); + void Killed( entvars_t *pevAttacker, int iGib ); + int IRelationship ( CBaseEntity *pTarget ); + virtual int CanPlaySentence( BOOL fDisregardState ); + virtual void PlaySentence( const char *pszSentence, float duration, float volume, float attenuation ); + void PlayScriptedSentence( const char *pszSentence, float duration, float volume, float attenuation, BOOL bConcurrent, CBaseEntity *pListener ); + void KeyValue( KeyValueData *pkvd ); + + // AI functions + void SetActivity ( Activity newActivity ); + Schedule_t *GetScheduleOfType ( int Type ); + void StartTask( Task_t *pTask ); + void RunTask( Task_t *pTask ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + void PrescheduleThink( void ); + + + // Conversations / communication + int GetVoicePitch( void ); + void IdleRespond( void ); + int FIdleSpeak( void ); + int FIdleStare( void ); + int FIdleHello( void ); + void IdleHeadTurn( Vector &vecFriend ); + int FOkToSpeak( void ); + void TrySmellTalk( void ); + CBaseEntity *EnumFriends( CBaseEntity *pentPrevious, int listNumber, BOOL bTrace ); + void AlertFriends( void ); + void ShutUpFriends( void ); + BOOL IsTalking( void ); + void Talk( float flDuration ); + // For following + BOOL CanFollow( void ); + BOOL IsFollowing( void ) { return m_hTargetEnt != NULL && m_hTargetEnt->IsPlayer(); } + void StopFollowing( BOOL clearSchedule ); + void StartFollowing( CBaseEntity *pLeader ); + virtual void DeclineFollowing( void ) {} + void LimitFollowers( CBaseEntity *pPlayer, int maxFollowers ); + + void EXPORT FollowerUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + virtual void SetAnswerQuestion( CTalkMonster *pSpeaker ); + virtual int FriendNumber( int arrayNumber ) { return arrayNumber; } + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + static TYPEDESCRIPTION m_SaveData[]; + + + static char *m_szFriends[TLK_CFRIENDS]; // array of friend names + static float g_talkWaitTime; + + int m_bitsSaid; // set bits for sentences we don't want repeated + int m_nSpeak; // number of times initiated talking + int m_voicePitch; // pitch of voice for this head + const char *m_szGrp[TLK_CGROUPS]; // sentence group names + float m_useTime; // Don't allow +USE until this time + int m_iszUse; // Custom +USE sentence group (follow) + int m_iszUnUse; // Custom +USE sentence group (stop following) + int m_iszDecline; // Custom +USE sentence group (refuse to follow) LRC + int m_iszSpeakAs; // Change the prefix for all this monster's speeches LRC + + float m_flLastSaidSmelled;// last time we talked about something that stinks + float m_flStopTalkTime;// when in the future that I'll be done saying this sentence. + + EHANDLE m_hTalkTarget; // who to look at while talking + CUSTOM_SCHEDULES; +}; + + +// Clients can push talkmonsters out of their way +#define bits_COND_CLIENT_PUSH ( bits_COND_SPECIAL1 ) +// Don't see a client right now. +#define bits_COND_CLIENT_UNSEEN ( bits_COND_SPECIAL2 ) + + +#endif //TALKMONSTER_H diff --git a/server/monsters/turret.cpp b/server/monsters/turret.cpp new file mode 100644 index 00000000..9e49241b --- /dev/null +++ b/server/monsters/turret.cpp @@ -0,0 +1,1335 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +/* + +===== turret.cpp ======================================================== + +*/ + +// +// TODO: +// Take advantage of new monster fields like m_hEnemy and get rid of that OFFSET() stuff +// Revisit enemy validation stuff, maybe it's not necessary with the newest monster code +// + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "baseweapon.h" +#include "basebeams.h" +#include "defaults.h" + +extern Vector VecBModelOrigin( entvars_t* pevBModel ); + +#define TURRET_SHOTS 2 +#define TURRET_RANGE (100 * 12) +#define TURRET_SPREAD Vector( 0, 0, 0 ) +#define TURRET_TURNRATE 30 //angles per 0.1 second +#define TURRET_MAXWAIT 15 // seconds turret will stay active w/o a target +#define TURRET_MAXSPIN 5 // seconds turret barrel will spin w/o a target +#define TURRET_MACHINE_VOLUME 0.5 + +typedef enum +{ + TURRET_ANIM_NONE = 0, + TURRET_ANIM_FIRE, + TURRET_ANIM_SPIN, + TURRET_ANIM_DEPLOY, + TURRET_ANIM_RETIRE, + TURRET_ANIM_DIE, +} TURRET_ANIM; + +class CBaseTurret : public CBaseMonster +{ +public: + void Spawn(void); + virtual void Precache(void); + void KeyValue( KeyValueData *pkvd ); + void EXPORT TurretUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ); + + virtual void TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType); + virtual int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ); + virtual int Classify(void); + + int BloodColor( void ) { return DONT_BLEED; } + void GibMonster( void ) {} // UNDONE: Throw turret gibs? + + // Think functions + + void EXPORT ActiveThink(void); + void EXPORT SearchThink(void); + void EXPORT AutoSearchThink(void); + void EXPORT TurretDeath(void); + + virtual void EXPORT SpinDownCall(void) { m_iSpin = 0; } + virtual void EXPORT SpinUpCall(void) { m_iSpin = 1; } + + // void SpinDown(void); + // float EXPORT SpinDownCall( void ) { return SpinDown(); } + + // virtual float SpinDown(void) { return 0;} + // virtual float Retire(void) { return 0;} + + void EXPORT Deploy(void); + void EXPORT Retire(void); + + void EXPORT Initialize(void); + + virtual void Ping(void); + virtual void EyeOn(void); + virtual void EyeOff(void); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static TYPEDESCRIPTION m_SaveData[]; + + // other functions + void SetTurretAnim(TURRET_ANIM anim); + int MoveTurret(void); + virtual void Shoot(Vector &vecSrc, Vector &vecDirToEnemy) { }; + + float m_flMaxSpin; // Max time to spin the barrel w/o a target + int m_iSpin; + + CSprite *m_pEyeGlow; + int m_eyeBrightness; + + int m_iDeployHeight; + int m_iRetractHeight; + int m_iMinPitch; + + int m_iBaseTurnRate; // angles per second + float m_fTurnRate; // actual turn rate + int m_iOrientation; // 0 = floor, 1 = Ceiling + int m_iOn; + virtual STATE getState() { if (m_iOn) { return STATE_ON; } else { return STATE_OFF; } } + + int m_fBeserk; // Sometimes this bitch will just freak out + int m_iAutoStart; // true if the turret auto deploys when a target + // enters its range + + Vector m_vecLastSight; + float m_flLastSight; // Last time we saw a target + float m_flMaxWait; // Max time to seach w/o a target + int m_iSearchSpeed; // Not Used! + + // movement + float m_flStartYaw; + Vector m_vecCurAngles; + Vector m_vecGoalAngles; + + + float m_flPingTime; // Time until the next ping, used when searching + float m_flSpinUpTime; // Amount of time until the barrel should spin down when searching +}; + + +TYPEDESCRIPTION CBaseTurret::m_SaveData[] = +{ + DEFINE_FIELD( CBaseTurret, m_flMaxSpin, FIELD_FLOAT ), + DEFINE_FIELD( CBaseTurret, m_iSpin, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseTurret, m_pEyeGlow, FIELD_CLASSPTR ), + DEFINE_FIELD( CBaseTurret, m_eyeBrightness, FIELD_INTEGER ), + DEFINE_FIELD( CBaseTurret, m_iDeployHeight, FIELD_INTEGER ), + DEFINE_FIELD( CBaseTurret, m_iRetractHeight, FIELD_INTEGER ), + DEFINE_FIELD( CBaseTurret, m_iMinPitch, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseTurret, m_iBaseTurnRate, FIELD_INTEGER ), + DEFINE_FIELD( CBaseTurret, m_fTurnRate, FIELD_FLOAT ), + DEFINE_FIELD( CBaseTurret, m_iOrientation, FIELD_INTEGER ), + DEFINE_FIELD( CBaseTurret, m_iOn, FIELD_INTEGER ), + DEFINE_FIELD( CBaseTurret, m_fBeserk, FIELD_INTEGER ), + DEFINE_FIELD( CBaseTurret, m_iAutoStart, FIELD_INTEGER ), + + + DEFINE_FIELD( CBaseTurret, m_vecLastSight, FIELD_POSITION_VECTOR ), + DEFINE_FIELD( CBaseTurret, m_flLastSight, FIELD_TIME ), + DEFINE_FIELD( CBaseTurret, m_flMaxWait, FIELD_FLOAT ), + DEFINE_FIELD( CBaseTurret, m_iSearchSpeed, FIELD_INTEGER ), + + DEFINE_FIELD( CBaseTurret, m_flStartYaw, FIELD_FLOAT ), + DEFINE_FIELD( CBaseTurret, m_vecCurAngles, FIELD_VECTOR ), + DEFINE_FIELD( CBaseTurret, m_vecGoalAngles, FIELD_VECTOR ), + + DEFINE_FIELD( CBaseTurret, m_flPingTime, FIELD_TIME ), + DEFINE_FIELD( CBaseTurret, m_flSpinUpTime, FIELD_TIME ), +}; + +IMPLEMENT_SAVERESTORE( CBaseTurret, CBaseMonster ); + +class CTurret : public CBaseTurret +{ +public: + void Spawn(void); + void Precache(void); + // Think functions + void SpinUpCall(void); + void SpinDownCall(void); + + virtual int Save( CSave &save ); + virtual int Restore( CRestore &restore ); + + static TYPEDESCRIPTION m_SaveData[]; + + // other functions + void Shoot(Vector &vecSrc, Vector &vecDirToEnemy); + +private: + int m_iStartSpin; + +}; +TYPEDESCRIPTION CTurret::m_SaveData[] = +{ + DEFINE_FIELD( CTurret, m_iStartSpin, FIELD_INTEGER ), +}; + +IMPLEMENT_SAVERESTORE( CTurret, CBaseTurret ); + + +class CMiniTurret : public CBaseTurret +{ +public: + void Spawn( ); + void Precache(void); + // other functions + void Shoot(Vector &vecSrc, Vector &vecDirToEnemy); +}; + + +LINK_ENTITY_TO_CLASS( monster_turret, CTurret ); +LINK_ENTITY_TO_CLASS( monster_miniturret, CMiniTurret ); + +void CBaseTurret::KeyValue( KeyValueData *pkvd ) +{ + if (FStrEq(pkvd->szKeyName, "maxsleep")) + { + m_flMaxWait = atof(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "orientation")) + { + m_iOrientation = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + + } + else if (FStrEq(pkvd->szKeyName, "searchspeed")) + { + m_iSearchSpeed = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + + } + else if (FStrEq(pkvd->szKeyName, "turnrate")) + { + m_iBaseTurnRate = atoi(pkvd->szValue); + pkvd->fHandled = TRUE; + } + else if (FStrEq(pkvd->szKeyName, "style") || + FStrEq(pkvd->szKeyName, "height") || + FStrEq(pkvd->szKeyName, "value1") || + FStrEq(pkvd->szKeyName, "value2") || + FStrEq(pkvd->szKeyName, "value3")) + pkvd->fHandled = TRUE; + else + CBaseMonster::KeyValue( pkvd ); +} + + +void CBaseTurret::Spawn() +{ + Precache( ); + SetNextThink( 1 ); + pev->movetype = MOVETYPE_FLY; + pev->sequence = 0; + pev->frame = 0; + pev->solid = SOLID_SLIDEBOX; + pev->takedamage = DAMAGE_AIM; + + SetBits (pev->flags, FL_MONSTER); + SetUse(&CBaseTurret:: TurretUse ); + + if (( pev->spawnflags & SF_MONSTER_TURRET_AUTOACTIVATE ) + && !( pev->spawnflags & SF_MONSTER_TURRET_STARTINACTIVE )) + { + m_iAutoStart = TRUE; + } + + if (m_iOrientation == 1) + { + pev->idealpitch = 180; + pev->angles.x = 180; + } + + ResetSequenceInfo( ); + SetBoneController( 0, 0 ); + SetBoneController( 1, 0 ); + m_flFieldOfView = VIEW_FIELD_FULL; + // m_flSightRange = TURRET_RANGE; +} + + +void CBaseTurret::Precache( ) +{ + PRECACHE_SOUND ("turret/tu_fire1.wav"); + PRECACHE_SOUND ("turret/tu_ping.wav"); + PRECACHE_SOUND ("turret/tu_active2.wav"); + PRECACHE_SOUND ("turret/tu_die.wav"); + PRECACHE_SOUND ("turret/tu_die2.wav"); + PRECACHE_SOUND ("turret/tu_die3.wav"); + // PRECACHE_SOUND ("turret/tu_retract.wav"); // just use deploy sound to save memory + PRECACHE_SOUND ("turret/tu_deploy.wav"); + PRECACHE_SOUND ("turret/tu_spinup.wav"); + PRECACHE_SOUND ("turret/tu_spindown.wav"); + PRECACHE_SOUND ("turret/tu_search.wav"); + PRECACHE_SOUND ("turret/tu_alert.wav"); +} + +#define TURRET_GLOW_SPRITE "sprites/flare3.spr" + +void CTurret::Spawn() +{ + Precache( ); + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/turret.mdl"); + if (!pev->health) + pev->health = TURRET_HEALTH; + m_HackedGunPos = Vector( 0, 0, 12.75 ); + m_flMaxSpin = TURRET_MAXSPIN; + pev->view_ofs.z = 12.75; + + CBaseTurret::Spawn( ); + + m_iRetractHeight = 16; + m_iDeployHeight = 32; + m_iMinPitch = -15; + UTIL_SetSize(pev, Vector(-32, -32, -m_iRetractHeight), Vector(32, 32, m_iRetractHeight)); + + SetThink(&CTurret::Initialize); + + m_pEyeGlow = CSprite::SpriteCreate( TURRET_GLOW_SPRITE, pev->origin, FALSE ); + m_pEyeGlow->SetTransparency( kRenderGlow, 255, 0, 0, 0, kRenderFxNoDissipation ); + m_pEyeGlow->SetParent( this, 2 ); + m_eyeBrightness = 0; + + SetNextThink( 0.3 ); +} + +void CTurret::Precache() +{ + CBaseTurret::Precache( ); + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL ("models/turret.mdl"); + PRECACHE_MODEL (TURRET_GLOW_SPRITE); +} + +void CMiniTurret::Spawn() +{ + Precache( ); + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/miniturret.mdl"); + if (!pev->health) + pev->health = MINITURRET_HEALTH; + m_HackedGunPos = Vector( 0, 0, 12.75 ); + m_flMaxSpin = 0; + pev->view_ofs.z = 12.75; + + CBaseTurret::Spawn( ); + m_iRetractHeight = 16; + m_iDeployHeight = 32; + m_iMinPitch = -15; + UTIL_SetSize(pev, Vector(-16, -16, -m_iRetractHeight), Vector(16, 16, m_iRetractHeight)); + + SetThink(&CMiniTurret::Initialize); + SetNextThink( 0.3 ); +} + + +void CMiniTurret::Precache() +{ + CBaseTurret::Precache( ); + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL ("models/miniturret.mdl"); + PRECACHE_SOUND("weapons/hks1.wav"); + PRECACHE_SOUND("weapons/hks2.wav"); + PRECACHE_SOUND("weapons/hks3.wav"); +} + +void CBaseTurret::Initialize(void) +{ + m_iOn = 0; + m_fBeserk = 0; + m_iSpin = 0; + + SetBoneController( 0, 0 ); + SetBoneController( 1, 0 ); + + if (m_iBaseTurnRate == 0) m_iBaseTurnRate = TURRET_TURNRATE; + if (m_flMaxWait == 0) m_flMaxWait = TURRET_MAXWAIT; + m_flStartYaw = pev->angles.y; + if (m_iOrientation == 1) + { +// pev->idealpitch = 180; //This is moved to CBaseTurret::Spawn for fix old bug in original HL. G-Cont. +// pev->angles.x = 180; + pev->view_ofs.z = -pev->view_ofs.z; + pev->effects |= EF_INVLIGHT; + pev->angles.y = pev->angles.y + 180; + if (pev->angles.y > 360) + pev->angles.y = pev->angles.y - 360; + } + + m_vecGoalAngles.x = 0; + + if (m_iAutoStart) + { + m_flLastSight = gpGlobals->time + m_flMaxWait; + SetThink(&CBaseTurret::AutoSearchThink); + SetNextThink( 0.1 ); + } + else + SetThink( NULL ); +} + +void CBaseTurret::TurretUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value ) +{ + if (useType == USE_TOGGLE) + { + if(m_iOn) useType = USE_OFF; + else useType = USE_ON; + } + if (useType == USE_ON) + { + SetNextThink( 0.1 ); // turn on delay + + if ( pev->spawnflags & SF_MONSTER_TURRET_AUTOACTIVATE ) + m_iAutoStart = TRUE; + SetThink( Deploy ); + } + else if(useType == USE_OFF) + { + m_hEnemy = NULL; + SetNextThink( 0.1 ); + m_iAutoStart = FALSE;// switching off a turret disables autostart + SetThink( Retire ); + } +} + + +void CBaseTurret::Ping( void ) +{ + // make the pinging noise every second while searching + if (m_flPingTime == 0) + m_flPingTime = gpGlobals->time + 1; + else if (m_flPingTime <= gpGlobals->time) + { + m_flPingTime = gpGlobals->time + 1; + EMIT_SOUND(ENT(pev), CHAN_ITEM, "turret/tu_ping.wav", 1, ATTN_NORM); + EyeOn( ); + } + else if (m_eyeBrightness > 0) + { + EyeOff( ); + } +} + + +void CBaseTurret::EyeOn( ) +{ + if (m_pEyeGlow) + { + if (m_eyeBrightness != 255) + { + m_eyeBrightness = 255; + } + m_pEyeGlow->SetBrightness( m_eyeBrightness ); + } +} + + +void CBaseTurret::EyeOff( ) +{ + if (m_pEyeGlow) + { + if (m_eyeBrightness > 0) + { + m_eyeBrightness = max( 0, m_eyeBrightness - 30 ); + m_pEyeGlow->SetBrightness( m_eyeBrightness ); + } + } +} + + +void CBaseTurret::ActiveThink(void) +{ + int fAttack = 0; + Vector vecDirToEnemy; + + SetNextThink( 0.1 ); + StudioFrameAdvance( ); + + if ((!m_iOn) || (m_hEnemy == NULL)) + { + m_hEnemy = NULL; + m_flLastSight = gpGlobals->time + m_flMaxWait; + SetThink(&CBaseTurret::SearchThink); + return; + } + + // if it's dead, look for something new + if ( !m_hEnemy->IsAlive() ) + { + if (!m_flLastSight) + { + m_flLastSight = gpGlobals->time + 0.5; // continue-shooting timeout + } + else + { + if (gpGlobals->time > m_flLastSight) + { + m_hEnemy = NULL; + m_flLastSight = gpGlobals->time + m_flMaxWait; + SetThink(&CBaseTurret::SearchThink); + return; + } + } + } + + Vector vecMid = pev->origin + pev->view_ofs; + Vector vecMidEnemy = m_hEnemy->BodyTarget( vecMid ); + + // Look for our current enemy + int fEnemyVisible = FBoxVisible(pev, m_hEnemy->pev, vecMidEnemy ); + + vecDirToEnemy = vecMidEnemy - vecMid; // calculate dir and dist to enemy + float flDistToEnemy = vecDirToEnemy.Length(); + + Vector vec = UTIL_VecToAngles(vecMidEnemy - vecMid); + + // Current enmey is not visible. + if (!fEnemyVisible || (flDistToEnemy > TURRET_RANGE)) + { + if (!m_flLastSight) + m_flLastSight = gpGlobals->time + 0.5; + else + { + // Should we look for a new target? + if (gpGlobals->time > m_flLastSight) + { + m_hEnemy = NULL; + m_flLastSight = gpGlobals->time + m_flMaxWait; + SetThink(&CBaseTurret::SearchThink); + return; + } + } + fEnemyVisible = 0; + } + else + { + m_vecLastSight = vecMidEnemy; + } + + UTIL_MakeAimVectors(m_vecCurAngles); + + /* + ALERT( at_console, "%.0f %.0f : %.2f %.2f %.2f\n", + m_vecCurAngles.x, m_vecCurAngles.y, + gpGlobals->v_forward.x, gpGlobals->v_forward.y, gpGlobals->v_forward.z ); + */ + + Vector vecLOS = vecDirToEnemy; //vecMid - m_vecLastSight; + vecLOS = vecLOS.Normalize(); + + // Is the Gun looking at the target + if (DotProduct(vecLOS, gpGlobals->v_forward) <= 0.866) // 30 degree slop + fAttack = FALSE; + else + fAttack = TRUE; + + // fire the gun + if (m_iSpin && ((fAttack) || (m_fBeserk))) + { + Vector vecSrc, vecAng; + GetAttachment( 0, vecSrc, vecAng ); + SetTurretAnim(TURRET_ANIM_FIRE); + Shoot(vecSrc, gpGlobals->v_forward ); + } + else + { + SetTurretAnim(TURRET_ANIM_SPIN); + } + + //move the gun + if (m_fBeserk) + { + if (RANDOM_LONG(0,9) == 0) + { + m_vecGoalAngles.y = RANDOM_FLOAT(0,360); + m_vecGoalAngles.x = RANDOM_FLOAT(0,90) - 90 * m_iOrientation; + TakeDamage(pev,pev,1, DMG_GENERIC); // don't beserk forever + return; + } + } + else if (fEnemyVisible) + { + if (vec.y > 360) + vec.y -= 360; + + if (vec.y < 0) + vec.y += 360; + + //ALERT(at_console, "[%.2f]", vec.x); + + if (vec.x < -180) + vec.x += 360; + + if (vec.x > 180) + vec.x -= 360; + + // now all numbers should be in [1...360] + // pin to turret limitations to [-90...15] + + if (m_iOrientation == 0) + { + if (vec.x > 90) + vec.x = 90; + else if (vec.x < m_iMinPitch) + vec.x = m_iMinPitch; + } + else + { + if (vec.x < -90) + vec.x = -90; + else if (vec.x > -m_iMinPitch) + vec.x = -m_iMinPitch; + } + + // ALERT(at_console, "->[%.2f]\n", vec.x); + + m_vecGoalAngles.y = vec.y; + m_vecGoalAngles.x = vec.x; + + } + + SpinUpCall(); + MoveTurret(); +} + + +void CTurret::Shoot(Vector &vecSrc, Vector &vecDirToEnemy) +{ + FireBullets( 1, vecSrc, vecDirToEnemy, TURRET_SPREAD, TURRET_RANGE, BULLET_12MM, 1 ); + EMIT_SOUND(ENT(pev), CHAN_WEAPON, "turret/tu_fire1.wav", 1, 0.6); + pev->effects = pev->effects | EF_MUZZLEFLASH; +} + + +void CMiniTurret::Shoot(Vector &vecSrc, Vector &vecDirToEnemy) +{ + FireBullets( 1, vecSrc, vecDirToEnemy, TURRET_SPREAD, TURRET_RANGE, BULLET_9MM, 1 ); + + switch(RANDOM_LONG(0,2)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/hks1.wav", 1, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/hks2.wav", 1, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/hks3.wav", 1, ATTN_NORM); break; + } + pev->effects = pev->effects | EF_MUZZLEFLASH; +} + + +void CBaseTurret::Deploy(void) +{ + SetNextThink( 0.1 ); + StudioFrameAdvance( ); + + if (pev->sequence != TURRET_ANIM_DEPLOY) + { + m_iOn = 1; + SetTurretAnim(TURRET_ANIM_DEPLOY); + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_deploy.wav", TURRET_MACHINE_VOLUME, ATTN_NORM); + UTIL_FireTargets( pev->target, this, this, USE_ON ); + } + + if (m_fSequenceFinished) + { + pev->maxs.z = m_iDeployHeight; + pev->mins.z = -m_iDeployHeight; + UTIL_SetSize(pev, pev->mins, pev->maxs); + + m_vecCurAngles.x = 0; + + if (m_iOrientation == 1) + { + m_vecCurAngles.y = UTIL_AngleMod( pev->angles.y + 180 ); + } + else + { + m_vecCurAngles.y = UTIL_AngleMod( pev->angles.y ); + } + + SetTurretAnim(TURRET_ANIM_SPIN); + pev->framerate = 0; + SetThink(&CBaseTurret::SearchThink); + } + + m_flLastSight = gpGlobals->time + m_flMaxWait; +} + +void CBaseTurret::Retire(void) +{ + // make the turret level + m_vecGoalAngles.x = 0; + m_vecGoalAngles.y = m_flStartYaw; + + SetNextThink( 0.1 ); + + StudioFrameAdvance( ); + + EyeOff( ); + + if (!MoveTurret()) + { + if (m_iSpin) + { + SpinDownCall(); + } + else if (pev->sequence != TURRET_ANIM_RETIRE) + { + SetTurretAnim(TURRET_ANIM_RETIRE); + EMIT_SOUND_DYN(ENT(pev), CHAN_BODY, "turret/tu_deploy.wav", TURRET_MACHINE_VOLUME, ATTN_NORM, 0, 120); + UTIL_FireTargets( pev->target, this, this, USE_OFF ); + } + else if (m_fSequenceFinished) + { + m_iOn = 0; + m_flLastSight = 0; + SetTurretAnim(TURRET_ANIM_NONE); + pev->maxs.z = m_iRetractHeight; + pev->mins.z = -m_iRetractHeight; + UTIL_SetSize(pev, pev->mins, pev->maxs); + if (m_iAutoStart) + { + SetThink(&CBaseTurret::AutoSearchThink); + SetNextThink( 0.1 ); + } + else + SetThink( NULL ); + } + } + else + { + SetTurretAnim(TURRET_ANIM_SPIN); + } +} + + +void CTurret::SpinUpCall(void) +{ + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + // Are we already spun up? If not start the two stage process. + if (!m_iSpin) + { + SetTurretAnim( TURRET_ANIM_SPIN ); + // for the first pass, spin up the the barrel + if (!m_iStartSpin) + { + SetNextThink( 1.0 ); // spinup delay + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_spinup.wav", TURRET_MACHINE_VOLUME, ATTN_NORM); + m_iStartSpin = 1; + pev->framerate = 0.1; + } + // after the barrel is spun up, turn on the hum + else if (pev->framerate >= 1.0) + { + SetNextThink( 0.1 ); // retarget delay + EMIT_SOUND(ENT(pev), CHAN_STATIC, "turret/tu_active2.wav", TURRET_MACHINE_VOLUME, ATTN_NORM); + SetThink(&CTurret::ActiveThink); + m_iStartSpin = 0; + m_iSpin = 1; + } + else + { + pev->framerate += 0.075; + } + } + + if (m_iSpin) + { + SetThink(&CTurret::ActiveThink); + } +} + + +void CTurret::SpinDownCall(void) +{ + if (m_iSpin) + { + SetTurretAnim( TURRET_ANIM_SPIN ); + if (pev->framerate == 1.0) + { + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "turret/tu_active2.wav", 0, 0, SND_STOP, 100); + EMIT_SOUND(ENT(pev), CHAN_ITEM, "turret/tu_spindown.wav", TURRET_MACHINE_VOLUME, ATTN_NORM); + } + pev->framerate -= 0.02; + if (pev->framerate <= 0) + { + pev->framerate = 0; + m_iSpin = 0; + } + } +} + + +void CBaseTurret::SetTurretAnim(TURRET_ANIM anim) +{ + if (pev->sequence != anim) + { + switch(anim) + { + case TURRET_ANIM_FIRE: + case TURRET_ANIM_SPIN: + if (pev->sequence != TURRET_ANIM_FIRE && pev->sequence != TURRET_ANIM_SPIN) + { + pev->frame = 0; + } + break; + default: + pev->frame = 0; + break; + } + + pev->sequence = anim; + ResetSequenceInfo( ); + + switch(anim) + { + case TURRET_ANIM_RETIRE: + pev->frame = 255; + pev->framerate = -1.0; + break; + case TURRET_ANIM_DIE: + pev->framerate = 1.0; + break; + } + //ALERT(at_console, "Turret anim #%d\n", anim); + } +} + + +// +// This search function will sit with the turret deployed and look for a new target. +// After a set amount of time, the barrel will spin down. After m_flMaxWait, the turret will +// retact. +// +void CBaseTurret::SearchThink(void) +{ + // ensure rethink + SetTurretAnim(TURRET_ANIM_SPIN); + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + if (m_flSpinUpTime == 0 && m_flMaxSpin) + m_flSpinUpTime = gpGlobals->time + m_flMaxSpin; + + Ping( ); + + // If we have a target and we're still healthy + if (m_hEnemy != NULL) + { + if (!m_hEnemy->IsAlive() ) + m_hEnemy = NULL;// Dead enemy forces a search for new one + } + + + // Acquire Target + if (m_hEnemy == NULL) + { + Look(TURRET_RANGE); + m_hEnemy = BestVisibleEnemy(); + } + + // If we've found a target, spin up the barrel and start to attack + if (m_hEnemy != NULL) + { + m_flLastSight = 0; + m_flSpinUpTime = 0; + SetThink(&CBaseTurret::ActiveThink); + } + else + { + // Are we out of time, do we need to retract? + if (gpGlobals->time > m_flLastSight) + { + //Before we retrace, make sure that we are spun down. + m_flLastSight = 0; + m_flSpinUpTime = 0; + SetThink(&CBaseTurret::Retire); + } + // should we stop the spin? + else if ((m_flSpinUpTime) && (gpGlobals->time > m_flSpinUpTime)) + { + SpinDownCall(); + } + + // generic hunt for new victims + m_vecGoalAngles.y = (m_vecGoalAngles.y + 0.1 * m_fTurnRate); + if (m_vecGoalAngles.y >= 360) + m_vecGoalAngles.y -= 360; + MoveTurret(); + } +} + + +// +// This think function will deploy the turret when something comes into range. This is for +// automatically activated turrets. +// +void CBaseTurret::AutoSearchThink(void) +{ + // ensure rethink + StudioFrameAdvance( ); + SetNextThink( 0.3 ); + + // If we have a target and we're still healthy + + if (m_hEnemy != NULL) + { + if (!m_hEnemy->IsAlive() ) + m_hEnemy = NULL;// Dead enemy forces a search for new one + } + + // Acquire Target + + if (m_hEnemy == NULL) + { + Look( TURRET_RANGE ); + m_hEnemy = BestVisibleEnemy(); + } + + if (m_hEnemy != NULL) + { + SetThink(&CBaseTurret::Deploy); + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_alert.wav", TURRET_MACHINE_VOLUME, ATTN_NORM); + } +} + + +void CBaseTurret :: TurretDeath( void ) +{ + BOOL iActive = FALSE; + + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + if (pev->deadflag != DEAD_DEAD) + { + pev->deadflag = DEAD_DEAD; + + float flRndSound = RANDOM_FLOAT ( 0 , 1 ); + + if ( flRndSound <= 0.33 ) + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_die.wav", 1.0, ATTN_NORM); + else if ( flRndSound <= 0.66 ) + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_die2.wav", 1.0, ATTN_NORM); + else + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_die3.wav", 1.0, ATTN_NORM); + + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "turret/tu_active2.wav", 0, 0, SND_STOP, 100); + + if (m_iOrientation == 0) + m_vecGoalAngles.x = -15; + else + m_vecGoalAngles.x = -90; + + SetTurretAnim(TURRET_ANIM_DIE); + + EyeOn( ); + } + + EyeOff( ); + + if (pev->dmgtime + RANDOM_FLOAT( 0, 2 ) > gpGlobals->time) + { + // lots of smoke + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( RANDOM_FLOAT( pev->absmin.x, pev->absmax.x ) ); + WRITE_COORD( RANDOM_FLOAT( pev->absmin.y, pev->absmax.y ) ); + WRITE_COORD( pev->origin.z - m_iOrientation * 64 ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( 25 ); // scale * 10 + WRITE_BYTE( 10 - m_iOrientation * 5); // framerate + MESSAGE_END(); + } + + if (pev->dmgtime + RANDOM_FLOAT( 0, 5 ) > gpGlobals->time) + { + Vector vecSrc = Vector( RANDOM_FLOAT( pev->absmin.x, pev->absmax.x ), RANDOM_FLOAT( pev->absmin.y, pev->absmax.y ), 0 ); + if (m_iOrientation == 0) + vecSrc = vecSrc + Vector( 0, 0, RANDOM_FLOAT( pev->origin.z, pev->absmax.z ) ); + else + vecSrc = vecSrc + Vector( 0, 0, RANDOM_FLOAT( pev->absmin.z, pev->origin.z ) ); + + UTIL_Sparks( vecSrc ); + } + + if (m_fSequenceFinished && !MoveTurret( ) && pev->dmgtime + 5 < gpGlobals->time) + { + pev->framerate = 0; + SetThink( NULL ); + } +} + + + +void CBaseTurret :: TraceAttack( entvars_t *pevAttacker, float flDamage, Vector vecDir, TraceResult *ptr, int bitsDamageType) +{ + if ( ptr->iHitgroup == 10 ) + { + // hit armor + if ( pev->dmgtime != gpGlobals->time || (RANDOM_LONG(0,10) < 1) ) + { + UTIL_Ricochet( ptr->vecEndPos, RANDOM_FLOAT( 1, 2) ); + pev->dmgtime = gpGlobals->time; + } + + flDamage = 0.1;// don't hurt the monster much, but allow bits_COND_LIGHT_DAMAGE to be generated + } + + if ( !pev->takedamage ) + return; + + AddMultiDamage( pevAttacker, this, flDamage, bitsDamageType ); +} + +// take damage. bitsDamageType indicates type of damage sustained, ie: DMG_BULLET + +int CBaseTurret::TakeDamage(entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType) +{ + if ( !pev->takedamage ) + return 0; + + if (!m_iOn) + flDamage /= 10.0; + + pev->health -= flDamage; + if (pev->health <= 0) + { + pev->health = 0; + pev->takedamage = DAMAGE_NO; + pev->dmgtime = gpGlobals->time; + + ClearBits (pev->flags, FL_MONSTER); // why are they set in the first place??? + + SetUse(NULL); + SetThink(&CBaseTurret::TurretDeath); + UTIL_FireTargets( pev->target, this, this, USE_ON ); + SetNextThink( 0.1 ); + + return 0; + } + + if (pev->health <= 10) + { + if (m_iOn && (1 || RANDOM_LONG(0, 0x7FFF) > 800)) + { + m_fBeserk = 1; + SetThink(&CBaseTurret::SearchThink); + } + } + + return 1; +} + +int CBaseTurret::MoveTurret(void) +{ + int state = 0; + // any x movement? + + if (m_vecCurAngles.x != m_vecGoalAngles.x) + { + float flDir = m_vecGoalAngles.x > m_vecCurAngles.x ? 1 : -1 ; + + m_vecCurAngles.x += 0.1 * m_fTurnRate * flDir; + + // if we started below the goal, and now we're past, peg to goal + if (flDir == 1) + { + if (m_vecCurAngles.x > m_vecGoalAngles.x) + m_vecCurAngles.x = m_vecGoalAngles.x; + } + else + { + if (m_vecCurAngles.x < m_vecGoalAngles.x) + m_vecCurAngles.x = m_vecGoalAngles.x; + } + + if (m_iOrientation == 0) + SetBoneController(1, -m_vecCurAngles.x); + else + SetBoneController(1, m_vecCurAngles.x); + state = 1; + } + + if (m_vecCurAngles.y != m_vecGoalAngles.y) + { + float flDir = m_vecGoalAngles.y > m_vecCurAngles.y ? 1 : -1 ; + float flDist = fabs(m_vecGoalAngles.y - m_vecCurAngles.y); + + if (flDist > 180) + { + flDist = 360 - flDist; + flDir = -flDir; + } + if (flDist > 30) + { + if (m_fTurnRate < m_iBaseTurnRate * 10) + { + m_fTurnRate += m_iBaseTurnRate; + } + } + else if (m_fTurnRate > 45) + { + m_fTurnRate -= m_iBaseTurnRate; + } + else + { + m_fTurnRate += m_iBaseTurnRate; + } + + m_vecCurAngles.y += 0.1 * m_fTurnRate * flDir; + + if (m_vecCurAngles.y < 0) + m_vecCurAngles.y += 360; + else if (m_vecCurAngles.y >= 360) + m_vecCurAngles.y -= 360; + + if (flDist < (0.05 * m_iBaseTurnRate)) + m_vecCurAngles.y = m_vecGoalAngles.y; + + //ALERT(at_console, "%.2f -> %.2f\n", m_vecCurAngles.y, y); + if (m_iOrientation == 0) + SetBoneController(0, m_vecCurAngles.y - pev->angles.y ); + else + SetBoneController(0, pev->angles.y - 180 - m_vecCurAngles.y ); + state = 1; + } + + if (!state) + m_fTurnRate = m_iBaseTurnRate; + + //ALERT(at_console, "(%.2f, %.2f)->(%.2f, %.2f)\n", m_vecCurAngles.x, + // m_vecCurAngles.y, m_vecGoalAngles.x, m_vecGoalAngles.y); + return state; +} + +// +// ID as a machine +// +int CBaseTurret::Classify ( void ) +{ + if (m_iClass) return m_iClass; + if (m_iOn || m_iAutoStart) + return CLASS_MACHINE; + return CLASS_NONE; +} + + + + +//========================================================= +// Sentry gun - smallest turret, placed near grunt entrenchments +//========================================================= +class CSentry : public CBaseTurret +{ +public: + void Spawn( ); + void Precache(void); + // other functions + void Shoot(Vector &vecSrc, Vector &vecDirToEnemy); + int TakeDamage(entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType); + void EXPORT SentryTouch( CBaseEntity *pOther ); + void EXPORT SentryDeath( void ); + +}; + +LINK_ENTITY_TO_CLASS( monster_sentry, CSentry ); + +void CSentry::Precache() +{ + CBaseTurret::Precache( ); + if (pev->model) + PRECACHE_MODEL((char*)STRING(pev->model)); //LRC + else + PRECACHE_MODEL ("models/sentry.mdl"); +} + +void CSentry::Spawn() +{ + Precache( ); + if (pev->model) + SET_MODEL(ENT(pev), STRING(pev->model)); //LRC + else + SET_MODEL(ENT(pev), "models/sentry.mdl"); + if (!pev->health) //LRC + pev->health = SENTRY_HEALTH; + m_HackedGunPos = Vector( 0, 0, 48 ); + pev->view_ofs.z = 48; + //m_flMaxWait = 1E6; + if (m_flMaxWait == 0) m_flMaxWait = TURRET_MAXWAIT;//G-Cont. now sentry can deployed + m_flMaxSpin = 1E6; + + CBaseTurret::Spawn(); + m_iRetractHeight = 64; + m_iDeployHeight = 64; + m_iMinPitch = -60; + UTIL_SetSize(pev, Vector(-16, -16, -m_iRetractHeight), Vector(16, 16, m_iRetractHeight)); + DROP_TO_FLOOR ( ENT(pev) ); + + SetTouch(&CSentry::SentryTouch); + SetThink(&CSentry::Initialize); + SetNextThink( 0.3 ); +} + +void CSentry::Shoot(Vector &vecSrc, Vector &vecDirToEnemy) +{ + FireBullets( 1, vecSrc, vecDirToEnemy, TURRET_SPREAD, TURRET_RANGE, BULLET_MP5, 1 ); + + switch(RANDOM_LONG(0,2)) + { + case 0: EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/hks1.wav", 1, ATTN_NORM); break; + case 1: EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/hks2.wav", 1, ATTN_NORM); break; + case 2: EMIT_SOUND(ENT(pev), CHAN_WEAPON, "weapons/hks3.wav", 1, ATTN_NORM); break; + } + pev->effects = pev->effects | EF_MUZZLEFLASH; +} + +int CSentry::TakeDamage(entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType) +{ + if ( !pev->takedamage ) + return 0; + + if (!m_iOn) + { + SetThink(&CSentry:: Deploy ); + SetUse( NULL ); + SetNextThink( 0.1 ); + } + + pev->health -= flDamage; + if (pev->health <= 0) + { + pev->health = 0; + pev->takedamage = DAMAGE_NO; + pev->dmgtime = gpGlobals->time; + + ClearBits (pev->flags, FL_MONSTER); // why are they set in the first place??? + + SetUse(NULL); + SetThink(&CSentry::SentryDeath); + UTIL_FireTargets( pev->target, this, this, USE_ON ); + SetNextThink( 0.1 ); + + return 0; + } + + return 1; +} + + +void CSentry::SentryTouch( CBaseEntity *pOther ) +{ + if ( pOther && (pOther->IsPlayer() || (pOther->pev->flags & FL_MONSTER)) ) + { + TakeDamage(pOther->pev, pOther->pev, 0, 0 ); + } +} + + +void CSentry :: SentryDeath( void ) +{ + BOOL iActive = FALSE; + + StudioFrameAdvance( ); + SetNextThink( 0.1 ); + + if (pev->deadflag != DEAD_DEAD) + { + pev->deadflag = DEAD_DEAD; + + float flRndSound = RANDOM_FLOAT ( 0 , 1 ); + + if ( flRndSound <= 0.33 ) + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_die.wav", 1.0, ATTN_NORM); + else if ( flRndSound <= 0.66 ) + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_die2.wav", 1.0, ATTN_NORM); + else + EMIT_SOUND(ENT(pev), CHAN_BODY, "turret/tu_die3.wav", 1.0, ATTN_NORM); + + EMIT_SOUND_DYN(ENT(pev), CHAN_STATIC, "turret/tu_active2.wav", 0, 0, SND_STOP, 100); + + SetBoneController( 0, 0 ); + SetBoneController( 1, 0 ); + + SetTurretAnim(TURRET_ANIM_DIE); + + pev->solid = SOLID_NOT; + pev->angles.y = UTIL_AngleMod( pev->angles.y + RANDOM_LONG( 0, 2 ) * 120 ); + + EyeOn( ); + } + + EyeOff( ); + + Vector vecSrc, vecAng; + GetAttachment( 1, vecSrc, vecAng ); + + if (pev->dmgtime + RANDOM_FLOAT( 0, 2 ) > gpGlobals->time) + { + // lots of smoke + MESSAGE_BEGIN( MSG_BROADCAST, SVC_TEMPENTITY ); + WRITE_BYTE( TE_SMOKE ); + WRITE_COORD( vecSrc.x + RANDOM_FLOAT( -16, 16 ) ); + WRITE_COORD( vecSrc.y + RANDOM_FLOAT( -16, 16 ) ); + WRITE_COORD( vecSrc.z - 32 ); + WRITE_SHORT( g_sModelIndexSmoke ); + WRITE_BYTE( 15 ); // scale * 10 + WRITE_BYTE( 8 ); // framerate + MESSAGE_END(); + } + + if (pev->dmgtime + RANDOM_FLOAT( 0, 8 ) > gpGlobals->time) + { + UTIL_Sparks( vecSrc ); + } + + if (m_fSequenceFinished && pev->dmgtime + 5 < gpGlobals->time) + { + pev->framerate = 0; + SetThink( NULL ); + } +} + diff --git a/server/monsters/zombie.cpp b/server/monsters/zombie.cpp new file mode 100644 index 00000000..9d2658f3 --- /dev/null +++ b/server/monsters/zombie.cpp @@ -0,0 +1,381 @@ +/*** +* +* Copyright (c) 1996-2002, Valve LLC. All rights reserved. +* +* This product contains software technology licensed from Id +* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc. +* All Rights Reserved. +* +* This source code contains proprietary and confidential information of +* Valve LLC and its suppliers. Access to this code is restricted to +* persons who have executed a written SDK license with Valve. Any access, +* use or distribution of this code by or to any unlicensed person is illegal. +* +****/ +//========================================================= +// Zombie +//========================================================= + +// UNDONE: Don't flinch every time you get hit + +#include "extdll.h" +#include "utils.h" +#include "cbase.h" +#include "monsters.h" +#include "schedule.h" +#include "defaults.h" + + +//========================================================= +// Monster's Anim Events Go Here +//========================================================= +#define ZOMBIE_AE_ATTACK_RIGHT 0x01 +#define ZOMBIE_AE_ATTACK_LEFT 0x02 +#define ZOMBIE_AE_ATTACK_BOTH 0x03 +#define ZOMBIE_AE_CRAB1 0x04 +#define ZOMBIE_AE_CRAB2 0x05 +#define ZOMBIE_AE_CRAB3 0x06 + +#define ZOMBIE_FLINCH_DELAY 2 // at most one flinch every n secs +#define ZOMBIE_CRAB "monster_headcrab" // headcrab jumps from zombie + + +class CZombie : public CBaseMonster +{ +public: + void Spawn( void ); + void Precache( void ); + void SetYawSpeed( void ); + int Classify ( void ); + void HandleAnimEvent( MonsterEvent_t *pEvent ); + int IgnoreConditions ( void ); + void SpawnCrab( void ); + + float m_flNextFlinch; + + void PainSound( void ); + void AlertSound( void ); + void IdleSound( void ); + void AttackSound( void ); + + static const char *pAttackSounds[]; + static const char *pIdleSounds[]; + static const char *pAlertSounds[]; + static const char *pPainSounds[]; + static const char *pAttackHitSounds[]; + static const char *pAttackMissSounds[]; + + // No range attacks + BOOL CheckRangeAttack1 ( float flDot, float flDist ) { return FALSE; } + BOOL CheckRangeAttack2 ( float flDot, float flDist ) { return FALSE; } + int TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ); +}; + +LINK_ENTITY_TO_CLASS( monster_zombie, CZombie ); + +const char *CZombie::pAttackHitSounds[] = +{ + "zombie/claw_strike1.wav", + "zombie/claw_strike2.wav", + "zombie/claw_strike3.wav", +}; + +const char *CZombie::pAttackMissSounds[] = +{ + "zombie/claw_miss1.wav", + "zombie/claw_miss2.wav", +}; + +const char *CZombie::pAttackSounds[] = +{ + "zombie/zo_attack1.wav", + "zombie/zo_attack2.wav", +}; + +const char *CZombie::pIdleSounds[] = +{ + "zombie/zo_idle1.wav", + "zombie/zo_idle2.wav", + "zombie/zo_idle3.wav", + "zombie/zo_idle4.wav", +}; + +const char *CZombie::pAlertSounds[] = +{ + "zombie/zo_alert10.wav", + "zombie/zo_alert20.wav", + "zombie/zo_alert30.wav", +}; + +const char *CZombie::pPainSounds[] = +{ + "zombie/zo_pain1.wav", + "zombie/zo_pain2.wav", +}; + +//========================================================= +// Classify - indicates this monster's place in the +// relationship table. +//========================================================= +int CZombie :: Classify ( void ) +{ + return m_iClass?m_iClass:CLASS_ALIEN_MONSTER; +} + +//========================================================= +// SetYawSpeed - allows each sequence to have a different +// turn rate associated with it. +//========================================================= +void CZombie :: SetYawSpeed ( void ) +{ + pev->yaw_speed = 120; +} + +int CZombie :: TakeDamage( entvars_t *pevInflictor, entvars_t *pevAttacker, float flDamage, int bitsDamageType ) +{ + // Take 30% damage from bullets + if ( bitsDamageType == DMG_BULLET ) + { + Vector vecDir = pev->origin - (pevInflictor->absmin + pevInflictor->absmax) * 0.5; + vecDir = vecDir.Normalize(); + float flForce = DamageForce( flDamage ); + pev->velocity = pev->velocity + vecDir * flForce; + flDamage *= 0.3; + } + + // HACK HACK -- until we fix this. + if ( IsAlive() ) + PainSound(); + return CBaseMonster::TakeDamage( pevInflictor, pevAttacker, flDamage, bitsDamageType ); +} + +void CZombie :: PainSound( void ) +{ + int pitch = 95 + RANDOM_LONG(0,9); + + if (RANDOM_LONG(0,5) < 2) + EMIT_SOUND_DYN ( ENT(pev), CHAN_VOICE, pPainSounds[ RANDOM_LONG(0,ARRAYSIZE(pPainSounds)-1) ], 1.0, ATTN_NORM, 0, pitch ); +} + +void CZombie :: AlertSound( void ) +{ + int pitch = 95 + RANDOM_LONG(0,9); + + EMIT_SOUND_DYN ( ENT(pev), CHAN_VOICE, pAlertSounds[ RANDOM_LONG(0,ARRAYSIZE(pAlertSounds)-1) ], 1.0, ATTN_NORM, 0, pitch ); +} + +void CZombie :: IdleSound( void ) +{ + int pitch = 95 + RANDOM_LONG(0,9); + + // Play a random idle sound + EMIT_SOUND_DYN ( ENT(pev), CHAN_VOICE, pIdleSounds[ RANDOM_LONG(0,ARRAYSIZE(pIdleSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); +} + +void CZombie :: AttackSound( void ) +{ + // Play a random attack sound + EMIT_SOUND_DYN ( ENT(pev), CHAN_VOICE, pAttackSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); +} + + +//========================================================= +// HandleAnimEvent - catches the monster-specific messages +// that occur when tagged animation frames are played. +//========================================================= +void CZombie :: HandleAnimEvent( MonsterEvent_t *pEvent ) +{ + switch( pEvent->event ) + { + case ZOMBIE_AE_ATTACK_RIGHT: + { + // do stuff for this event. + CBaseEntity *pHurt = CheckTraceHullAttack( 70, ZOMBIE_ONE_SLASH, DMG_SLASH ); + if ( pHurt ) + { + if ( pHurt->pev->flags & (FL_MONSTER|FL_CLIENT) ) + { + pHurt->pev->punchangle.z = -18; + pHurt->pev->punchangle.x = 5; + pHurt->pev->velocity = pHurt->pev->velocity - gpGlobals->v_right * 100; + } + // Play a random attack hit sound + EMIT_SOUND_DYN ( ENT(pev), CHAN_WEAPON, pAttackHitSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackHitSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); + } + else // Play a random attack miss sound + EMIT_SOUND_DYN ( ENT(pev), CHAN_WEAPON, pAttackMissSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackMissSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); + + if (RANDOM_LONG(0,1)) + AttackSound(); + } + break; + + case ZOMBIE_AE_ATTACK_LEFT: + { + // do stuff for this event. + CBaseEntity *pHurt = CheckTraceHullAttack( 70, ZOMBIE_ONE_SLASH, DMG_SLASH ); + if ( pHurt ) + { + if ( pHurt->pev->flags & (FL_MONSTER|FL_CLIENT) ) + { + pHurt->pev->punchangle.z = 18; + pHurt->pev->punchangle.x = 5; + pHurt->pev->velocity = pHurt->pev->velocity + gpGlobals->v_right * 100; + } + EMIT_SOUND_DYN ( ENT(pev), CHAN_WEAPON, pAttackHitSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackHitSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); + } + else + EMIT_SOUND_DYN ( ENT(pev), CHAN_WEAPON, pAttackMissSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackMissSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); + + if (RANDOM_LONG(0,1)) + AttackSound(); + } + break; + + case ZOMBIE_AE_ATTACK_BOTH: + { + // do stuff for this event. + CBaseEntity *pHurt = CheckTraceHullAttack( 70, ZOMBIE_BOTH_SLASH, DMG_SLASH ); + if ( pHurt ) + { + if ( pHurt->pev->flags & (FL_MONSTER|FL_CLIENT) ) + { + pHurt->pev->punchangle.x = 5; + pHurt->pev->velocity = pHurt->pev->velocity + gpGlobals->v_forward * -100; + } + EMIT_SOUND_DYN ( ENT(pev), CHAN_WEAPON, pAttackHitSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackHitSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); + } + else + EMIT_SOUND_DYN ( ENT(pev), CHAN_WEAPON, pAttackMissSounds[ RANDOM_LONG(0,ARRAYSIZE(pAttackMissSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + RANDOM_LONG(-5,5) ); + + if (RANDOM_LONG(0,1)) + AttackSound(); + } + break; + + case ZOMBIE_AE_CRAB1: + { + pev->body = 1; // set the head to a skull + SpawnCrab(); + } + break; + + case ZOMBIE_AE_CRAB2: + { + pev->body = 1; // set the head to a skull + SpawnCrab(); + } + break; + + case ZOMBIE_AE_CRAB3: + { + pev->body = 1; // set the head to a skull + SpawnCrab(); + } + break; + + default: + CBaseMonster::HandleAnimEvent( pEvent ); + break; + } +} + +//========================================================= +// Spawn +//========================================================= +void CZombie :: Spawn() +{ + Precache(); + + if (pev->model) UTIL_SetModel(ENT(pev), pev->model); + else + { + if(pev->button == 0) UTIL_SetModel(ENT(pev), "models/monsters/zombie_sci.mdl"); + else if(pev->button == 1) UTIL_SetModel(ENT(pev), "models/monsters/zombie_bar.mdl"); + } + UTIL_SetSize( pev, VEC_HUMAN_HULL_MIN, VEC_HUMAN_HULL_MAX ); + + pev->solid = SOLID_SLIDEBOX; + pev->movetype = MOVETYPE_STEP; + m_bloodColor = BLOOD_COLOR_GREEN; + if (pev->health == 0) + pev->health = ZOMBIE_HEALTH; + pev->view_ofs = VEC_VIEW;// position of the eyes relative to monster's origin. + m_flFieldOfView = 0.5;// indicates the width of this monster's forward view cone ( as a dotproduct result ) + m_MonsterState = MONSTERSTATE_NONE; + m_afCapability = bits_CAP_DOORS_GROUP; + + MonsterInit(); +} + +//========================================================= +// Precache - precaches all resources this monster needs +//========================================================= +void CZombie :: Precache() +{ + int i; + + if (pev->model) UTIL_PrecacheModel(pev->model); //LRC + else + { + if(pev->button) UTIL_PrecacheModel("models/monsters/zombie_bar.mdl"); + else UTIL_PrecacheModel("models/monsters/zombie_sci.mdl"); + } + UTIL_PrecacheEntity( ZOMBIE_CRAB ); + for ( i = 0; i < ARRAYSIZE( pAttackHitSounds ); i++ ) + PRECACHE_SOUND((char *)pAttackHitSounds[i]); + + for ( i = 0; i < ARRAYSIZE( pAttackMissSounds ); i++ ) + PRECACHE_SOUND((char *)pAttackMissSounds[i]); + + for ( i = 0; i < ARRAYSIZE( pAttackSounds ); i++ ) + PRECACHE_SOUND((char *)pAttackSounds[i]); + + for ( i = 0; i < ARRAYSIZE( pIdleSounds ); i++ ) + PRECACHE_SOUND((char *)pIdleSounds[i]); + + for ( i = 0; i < ARRAYSIZE( pAlertSounds ); i++ ) + PRECACHE_SOUND((char *)pAlertSounds[i]); + + for ( i = 0; i < ARRAYSIZE( pPainSounds ); i++ ) + PRECACHE_SOUND((char *)pPainSounds[i]); +} + +//========================================================= +// AI Schedules Specific to this monster +//========================================================= + + + +int CZombie::IgnoreConditions ( void ) +{ + int iIgnore = CBaseMonster::IgnoreConditions(); + + if ((m_Activity == ACT_MELEE_ATTACK1) || (m_Activity == ACT_MELEE_ATTACK1)) + { + if (m_flNextFlinch >= gpGlobals->time) + iIgnore |= (bits_COND_LIGHT_DAMAGE|bits_COND_HEAVY_DAMAGE); + } + + if ((m_Activity == ACT_SMALL_FLINCH) || (m_Activity == ACT_BIG_FLINCH)) + { + if (m_flNextFlinch < gpGlobals->time) + m_flNextFlinch = gpGlobals->time + ZOMBIE_FLINCH_DELAY; + } + + return iIgnore; + +} + +//========================================================= +// Spawn Headcrab - headcrab jumps from zombie +//========================================================= +void CZombie :: SpawnCrab( void ) +{ + vec3_t vecSrc, vecAng; + GetAttachment( 0, vecSrc, vecAng ); + CBaseEntity *pCrab = CBaseEntity::Create( ZOMBIE_CRAB, vecSrc, vecAng, edict() ); // create the crab + pCrab->pev->spawnflags |= SF_MONSTER_FALL_TO_GROUND; // make the crab fall + pCrab->pev->velocity = pev->movedir * 30; + pCrab->pev->velocity.z += 30; +} diff --git a/sv_dll/server.def b/server/server.def similarity index 100% rename from sv_dll/server.def rename to server/server.def diff --git a/sv_dll/server.dsp b/server/server.dsp similarity index 95% rename from sv_dll/server.dsp rename to server/server.dsp index 64bdf073..e49b187a 100644 --- a/sv_dll/server.dsp +++ b/server/server.dsp @@ -87,7 +87,7 @@ SOURCE="$(InputPath)" # ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 # ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 # ADD BASE RSC /l 0x409 /d "NDEBUG" -# ADD RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo diff --git a/sv_dll/weapon_generic.cpp b/server/weapon_generic.cpp similarity index 100% rename from sv_dll/weapon_generic.cpp rename to server/weapon_generic.cpp diff --git a/todo.log b/todo.log index e86883e7..0548912e 100644 --- a/todo.log +++ b/todo.log @@ -25,7 +25,7 @@ Beta 13.12.08 4. release crash OK 5. timers set OK 6. AI_ -> FL_ OK -7. SVC_SOUND +7. SVC_SOUND entity_state_t невидима для пользователя diff --git a/vsound/s_main.c b/vsound/s_main.c index 3ff1e3b2..23ed20aa 100644 --- a/vsound/s_main.c +++ b/vsound/s_main.c @@ -453,9 +453,9 @@ void S_StartSound( const vec3_t pos, int entnum, int channel, sound_t handle, fl // Allocate a playSound ps = S_AllocPlaySound(); - if(!ps) + if( !ps ) { - if( sfx->name[0] == '#' ) MsgDev(D_ERROR, "dropped sound %s\n", &sfx->name[1] ); + if( sfx->name[0] == '#' ) MsgDev( D_ERROR, "dropped sound %s\n", &sfx->name[1] ); else MsgDev( D_ERROR, "dropped sound \"sound/%s\"\n", sfx->name ); return; } @@ -492,7 +492,7 @@ S_StartLocalSound menu sound ================= */ -bool S_StartLocalSound( const char *name ) +bool S_StartLocalSound( const char *name, float volume, const float *origin ) { sound_t sfxHandle; @@ -500,7 +500,7 @@ bool S_StartLocalSound( const char *name ) return false; sfxHandle = S_RegisterSound( name ); - S_StartSound( NULL, al_state.clientnum, CHAN_AUTO, sfxHandle, 1.0f, ATTN_NONE, PITCH_NORM, false ); + S_StartSound( origin, al_state.clientnum, CHAN_AUTO, sfxHandle, volume, ATTN_NONE, PITCH_NORM, false ); return true; } @@ -710,10 +710,10 @@ void S_PlaySound_f( void ) { if( Cmd_Argc() == 1 ) { - Msg("Usage: playsound \n"); + Msg( "Usage: playsound \n" ); return; } - S_StartLocalSound(Cmd_Argv(1)); + S_StartLocalSound( Cmd_Argv( 1 ), 1.0f, NULL ); } /* @@ -727,13 +727,13 @@ void S_Music_f( void ) if( c == 2 ) S_StartBackgroundTrack( Cmd_Argv(1), Cmd_Argv(1) ); else if( c == 3 ) S_StartBackgroundTrack( Cmd_Argv(1), Cmd_Argv(2) ); - else Msg("Usage: music [loopfile]\n"); + else Msg( "Usage: music [loopfile]\n" ); } /* - ================= - S_StopSound_f - ================= +================= +S_StopSound_f +================= */ void S_StopSound_f( void ) { diff --git a/vsound/sound.h b/vsound/sound.h index a1880d12..13fccc06 100644 --- a/vsound/sound.h +++ b/vsound/sound.h @@ -176,7 +176,7 @@ void S_StreamRawSamples( int samples, int rate, int width, int channels, const b bool S_AddLoopingSound( int entnum, sound_t handle, float volume, float attn ); void S_StartBackgroundTrack( const char *intro, const char *loop ); channel_t *S_PickChannel( int entNum, int entChannel ); -int S_StartLocalSound( const char *name ); +int S_StartLocalSound( const char *name, float volume, const float *org ); sfx_t *S_GetSfxByHandle( sound_t handle ); void S_StreamBackgroundTrack( void ); void S_StopBackgroundTrack( void ); diff --git a/xash.dsw b/xash.dsw index 4a00813c..2522351f 100644 --- a/xash.dsw +++ b/xash.dsw @@ -15,6 +15,18 @@ Package=<4> ############################################################################### +Project: "client"=".\client\client.dsp" - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + Project: "common"=".\common\common.dsp" - Package Owner=<4> Package=<5> @@ -63,7 +75,7 @@ Package=<4> ############################################################################### -Project: "server"=".\sv_dll\server.dsp" - Package Owner=<4> +Project: "server"=".\server\server.dsp" - Package Owner=<4> Package=<5> {{{